Flutter GetX 设置状态

Flutter GetX setState

bool download=false; setState(() {download=true;});download==true?Text("new"):("old");

如何在没有状态小部件的情况下使用 get X 编写此代码。

谢谢;

首先定义一个控制器class

class ControllerClass extends GetxController{
 RxBool download == false.obs;
}

在你的无状态 class 中,你可以用两种方式声明一个集合

像这样先注入一个控制器

 ControllerClass controller = Get.put(ControllerClass());

第一个:

    Obx(()=>controller.download.value == true?Text("new"):("old"))

在某些函数中

downloadCondition(){
   controller.download.value == // true or false;

}

第二个:

    GetBuilder<ControllerClass>(builder: (controller){ return 
     controller.download.value == true? 
    Text("new"):("old"));}

在某些函数中

downloadCondition(){
   controller.download.value == // true or false;
 controller.update();
}

首先尝试创建一个控制器:

 class ControllerName extends GetxController {
    final download = false.obs;
    
       yourFunc(){
        download(!download.value);
   }
    }

那时候我经常用obx

    // You can also put your controller above on stateful or inside depends on you
       // final controller = Get.put(ControllerName());
        class YourPageName extends StatelessWidget {

       YourPageName({Key? key}) : super(key: key);

     final controller = Get.put(ControllerName());

      @override
      Widget build(BuildContext context) {
        return Obx(()=>
            Scaffold(
           body: SafeArea(
             child: Column(
                 children:[
                      Text(controller.download.isFalse ? "Old" : "New"),
                      SizedBox(
                      height: 60,
                     width: 120,
                     child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                   primary: Colors.red,
                child: Center(
                   child: Text("Click")
                       ),
                     onPressed: ()=>controller.yourFunc(),
                         ),
                        ),
                 ]
                ),
            ),
          ),
          );
       }
        
        }