i have to pass value from class a isBottomBarVisible to class b. if the isBottomBarVisible true than show alignment top center other wise center in flutter.
i have to pass value from class a isBottomBarVisible to class b. if the isBottomBarVisible true than show alignment top center other wise 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;
});
});
}
}
}
another class B is
i have to pass value from class a isBottomBarVisible to class b. if the isBottomBarVisible true than show alignment top center other wise center in flutter.
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,
);
}
}
i have to pass value from class a isBottomBarVisible to class b. if the isBottomBarVisible true than show alignment top center other wise center in flutter.
4
Answers
if both class are in one file, you can use global variable to resolve your problem. if not, use parameter constructor or static variable(MaterialPageRoute)
Update your ClassB code like, add constructure
Now wherever you have called ClassB, like while navigating ClassA to ClassB you have to pass the value in ClassB like this –
ClassB(isBottomSheetVisible: isBottomSheetVisible).
So that your isBottomSheetVisible value from the A pass to the B.
The only way to solve your issue is by using state managers;
there are many state managers like Bloc, Riverpod, Getx, and so many…
I suggest you use flutter-bloc(cubit) as state manager cuz it is simple and well-organized, but if you want to use an easy and more accessible state manager then go for getx.
Let me know if you have any questions.
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
Obx
in the UI.Note: Consider designing the most suitable file and service structure for your project. The code above is just an example.