GetX GetBuilder Counter App

 import "package:flutter/material.dart";

import "package:get/get.dart";
import "package:getx_flutter/GetX_count_controller.dart";

void main() {
runApp(const MyApp(),);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "GetBuilder in GetX Flutter",
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DemoBuilder(),
);
}
}
class DemoBuilder extends GetView<HomeController> {
DemoBuilder({super.key});

HomeController homeControl = Get.put(HomeController(),);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("GetBuilder in Getx Flutter", style: TextStyle(fontSize: 30.0,),),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.elliptical(300.0, 100.0,),
),
),
toolbarHeight: 300.0,
centerTitle: true,
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GetBuilder<HomeController>(
id: "First_update",
builder: (controller) {
return Center(child: Text(controller.count.toString(),
style: const TextStyle(fontSize: 80.0, fontWeight: FontWeight.bold,),),
);
},
),
const SizedBox(height: 80.0,),
ElevatedButton(
onPressed: () {
homeControl.increment();
},
child: const Text("Increment"),
),
const SizedBox(height: 40.0,),
ElevatedButton(
onPressed: () {
homeControl.decrement();
},
child: const Text("Decrement"),
),
],
),
),
);
}
}

///Notes:
///Getx GetBuilder With ID
// We will see how to update a certain widget in flutter using Getx GetBuilder.
//
// Your certain controller could be used in many different places.
// And the same controller could be found in many different places.
//
// If you use GetBuilder for that controller in the UI, then when you trigger update
// using the update() method, controller would update the state in every Ui,
// that is using the certain controller.
//
// This operation is quite expensive and unnecessary. You may use ID with GetBuilder and
// the same ID should be mentioned in the update() method. Because update() takes
// a list of IDs as a string.

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter