I need to pass the value isBottomBarVisible from Class A to Class B. If isBottomBarVisible is true, then align it to the top center; otherwise, align it to the center in Flutter.
class ClassA extends StatelessWidget {
bool isBottomBarVisible = false;
@override
Widget build(BuildContext context) {
// Create an instance of ClassA
return Container(
// Your UI widgets here
);
void _showBottomSheet() {
setState(() {
isBottomSheetVisible = true;
});
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
child: Center(
child: Text("This is a bottom sheet"),
),
);
},
).whenComplete(() {
setState(() {
isBottomSheetVisible = false;
});
});
}
}
}
class ClassB extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
// Your UI widgets here
child: AnimatedContainer(
duration: Duration(milliseconds: 500),
alignment: isKeyboardVisible || isBottomSheetVisible // how to get value
? Alignment.topCenter
: Alignment.center,
);
}
}
State management is your friend in this kind of situation. You can implement any state management you want (Bloc, GetX, etc...) to achieve this goal.
If you're not concerned about design patterns or anything similar, you can implement the simplest reactive widget in GetX (Obx with Rx).
Example:
Usage in widget
Updating the variable will trigger the rebuilding of the
Obxin the UI.Note: Consider designing the most suitable file and service structure for your project. The code above is just an example.