Getx-GetBuilder in Flutter
(Getx Tutorial {part-3})
Hi Coders,
In Last part of this Getx Series we learned about the Routes in Getx and now in this article we learn about GetBuilder.
If you didn't read my previous articles on this Getx series then check below
homepage_view.dart
import 'package:codemicros_getx_series/app/routes/app_routes.dart';
import 'package:codemicros_getx_series/app/views/home/homepage_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomepageView extends StatelessWidget {
const HomepageView({super.key});
@override
Widget build(BuildContext context) {
// HomepageController homepageController = Get.put(HomepageController());
return GetBuilder(
builder: (HomepageController homepageController) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"With Rx: ${homepageController.counter.value}",
style: const TextStyle(fontSize: 45.0),
),
Text(
"Without Rx: ${homepageController.counter2}",
style: const TextStyle(fontSize: 45.0),
),
ElevatedButton(
onPressed: () {
Get.toNamed(Routes.testing);
},
child: const Text("Go to Testing Screen"))
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
// with RX
ElevatedButton(
onPressed: () {
homepageController.updateCounterValue();
},
child: const Text("Add")),
//WITHOUT rX
ElevatedButton(
onPressed: () {
homepageController.updateCounter2Value();
},
child: const Text("Add without obx")),
],
),
);
},
);
}
}
homepage_controller.dart
import 'package:get/get.dart';
class HomepageController extends GetxController {
final RxInt counter = 0.obs;
int counter2 = 0;
updateCounterValue() {
counter.value = counter.value + 1;
update();
}
updateCounter2Value() {
counter2 = counter2 + 1;
update();
}
}