How to pass bool value from one class to another class in flutter

64 views Asked by At

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,
    );
  }
}
4

There are 4 answers

1
Tung Ha On BEST ANSWER

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:

class AppGlobalRxService {
  static RxBool isBottomSheetVisible = false.obs;
}

Usage in widget

Obx(() => Container(
      color: AppGlobalRxService.isBottomSheetVisible.value ? Colors.red : Colors.blue,
    ))

Updating the variable will trigger the rebuilding of the Obx in the UI.

AppGlobalRxService.isBottomSheetVisible.value = true

Note: Consider designing the most suitable file and service structure for your project. The code above is just an example.

2
Trieu Thanh Tung On

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)

2
Vandan Patel On

Update your ClassB code like, add constructure

class ClassB extends StatelessWidget {
  final bool isBottomSheetVisible;
  const ClassB({
    super.key,
    required this.isBottomSheetVisible,
  });
  @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,
      ),
    );
  }
}

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.

0
AmirHossein On

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.