skip to Main Content

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


  1. 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)

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search