GetX GetBuilder UniqueID
import "package:flutter/material.dart";
import "package:get/get.dart";
import "package:getx_flutter/GetX_GetBuilder_practice/controller2.dart";
void main() {
runApp(const MyApp(),);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "Reactive State Manager",
theme: ThemeData(
primarySwatch: Colors.brown,
),
home: AgainReact(),
);
}
}
class AgainReact extends StatelessWidget {
AgainReact({super.key});
SimpleReact2 simpleController = Get.put(SimpleReact2(),);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Reactive State Manager"),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GetBuilder<SimpleReact2>(
id: "first",
//init: SimpleReact(), ///Same as the "Get.put(SimpleReact(),);"
builder: (simpleController) {
return Text(
"${simpleController.count}",
style: const TextStyle(fontSize: 30.0,),
);
},),
GetBuilder<SimpleReact2>(
builder: (simpleController) {
return Text(
"${simpleController.count}",
style: const TextStyle(fontSize: 30.0,),
);
},
),
ElevatedButton(
onPressed: () {
///If we don't create controller instance initially then we have to find Controller and
///Get.find() will give same instance(of Controller) everytime and we can use throughout the app
var getController = Get.find<SimpleReact2>();
getController.increment();
// simpleController.increment();
},
child: const Text("Increment"),
),
],
),
),
);
}
}
///Note:
///When multiple GetBuilder is used at that time using "id:" property, we can update(call) specific
///GetBuilder using Unique id and that "id:" property value will be passed to the update(); method
import "package:get/get.dart";
class SimpleReact2 extends GetxController {
var count = 0;
@override
void onInit() {
print("onInit() is called...");
super.onInit();
}
void increment() {
count++;
update(["first"]);
// update(["second"]);
}
@override
void onClose() {
print("onClose() is called...");
super.onClose();
}
}
Comments
Post a Comment