GetBuilder Simple StateManager

 import "package:celloip/my_controller.dart";

import "package:flutter/material.dart";
import "package:get/get.dart";

void main() {
runApp(MyApplication(),);
}
class MyApplication extends StatelessWidget {
MyApplication({super.key}) : super();

@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "GetBuilder",
theme: ThemeData(
primarySwatch: Colors.indigo,
),
home: BuilderDemo(),
);
}
}
class BuilderDemo extends StatelessWidget {
BuilderDemo({super.key}) : super();

///If "init" property is not used in GetX<Type of Controller> then create the instance of controller as follows.
/// Creating the instance of MyController class
// MyController2 myController2 = Get.put(MyController2());

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GetBuilder"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GetBuilder<MyController2>(
init: MyController2(),
builder: (controller1) {
return Text("The Value is : ${controller1.count}", style: TextStyle(fontSize: 30.0,),);
}
),
SizedBox(height: 50.0,),
ElevatedButton(
onPressed: () {

/// If instance of controller not created at top
Get.find<MyController2>().increment();
//Get.find() will automatically find(search) the instance if it is created
},
child: Text("Increment"),
),
SizedBox(height: 50.0,),
ElevatedButton(
onPressed: () {

/// If instance of controller not created at top
Get.find<MyController2>().decrement();
//Get.find() will automatically find(search) the instance if it is created
},
child: Text("Decrement"),
),
],
),
),
);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter