Search code examples
flutterflutter-getx

Flutter: How to listen to variable change on GetX


I want to change body of the widget depending on if the pressedBool is false or true.

Here is GetxController I wrote.

    import 'package:get/state_manager.dart';
    
    class PressedState extends GetxController{
      var pressedBool = true;
      changeStatus() {
        if(pressedBool){
          pressedBool = false;            
        }
        else {
          pressedBool = true;
        }
        update();
      }

}

Here is where GetX update and listen should work out to change body of the page:

final PressedState pressController = Get.put(PressedState());

return MaterialButton(
    onPressed: () {
      pressController.changeStatus();
    },
    child: 
      pressController.pressedBool
            ? Container(...) : Container(...)), ...

How can I make GetX to listen pressedBool variable ?


Solution

  • return MaterialButton(
        onPressed: () {
          pressController.changeStatus();
        },
        child:
          GetBuilder<PressedState>(
            init: PressedState() 
            builder: (pressController) {
              return pressController.pressedBool
                ? Container(...) : Container(...))
            }
          ),