bool download=false; setState(() {download=true;});download==true?Text("new"):("old");
How to write this code by using get X without stateful widget.
Thank you;
CodePudding user response:
first define a controller class
class ControllerClass extends GetxController{
RxBool download == false.obs;
}
in your stateless class you can state a set with two ways
inject a controller first like this
ControllerClass controller = Get.put(ControllerClass());
First:
Obx(()=>controller.download.value == true?Text("new"):("old"))
in some function
downloadCondition(){
controller.download.value == // true or false;
}
Second:
GetBuilder<ControllerClass>(builder: (controller){ return
controller.download.value == true?
Text("new"):("old"));}
in some function
downloadCondition(){
controller.download.value == // true or false;
controller.update();
}
CodePudding user response:
try this first create a controller:
class ControllerName extends GetxController {
final download = false.obs;
yourFunc(){
download(!download.value);
}
}
i usually used obx then
// 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(),
),
),
]
),
),
),
);
}
}