Counter App using GetX

 import "package:flutter/material.dart";

import "package:get/get.dart";
import "package:getx_flutter/GetX_Counter_App/controller.dart";

///The choice between GetBuilder, GetX, or Obx depends on your needs and
///the complexity of your application. If you only need to update a small portion of your widgets,
///GetBuilder can be a good choice. If you want to automatically update widgets
///based on state changes, GetX or Obx can be better options.

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

@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "GetX Counter App",
theme: ThemeData(
floatingActionButtonTheme: const FloatingActionButtonThemeData(
backgroundColor: Colors.brown,
),
primarySwatch: Colors.blue,
),
home: CounterDemo(),
);
}
}
class CounterDemo extends StatelessWidget {
CounterDemo({super.key}) : super();

final CounterController _controller = Get.put(CounterController());

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("GetX Counter App"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Obx(() {
return Text("Clicks : ${_controller.count}",
style: const TextStyle(fontSize: 50.0,),);
},),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller.increment();
},
backgroundColor: Colors.brown,
foregroundColor: Colors.brown,
child: const Icon(Icons.add, color: Colors.white,),
),
);
}
}
import "package:get/get.dart";

///GetxController comes with onInit() and onClose() methods.
///This allows us to completely avoid using StatefulWidget and write organized code.

class CounterController extends GetxController {
var count = 0.obs;

void increment() {
count++;
//update();
}

void decrement() {
count--;
//update();
}
}

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