Workers GetX State Management

 import "package:flutter/material.dart";

import "package:get/get.dart";
import "package:getx_state_management/GetX_Workers/myController.dart";

/// Workers :
/// Workers generates specific callbacks whenever an event occurs. Different type of workers are there like
/// ever, everAll, once, debounce, interval
/// Best place to use workers is inside the onInit method (In Controller Class).

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: "Getx Workers",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: WorkersPractice(),
);
}
}

class WorkersPractice extends StatelessWidget {
WorkersPractice({super.key});

MyController myController = Get.put(MyController());

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"GetX Workers",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
myController.incrementCounter();
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(200.0, 50.0),
),
child: const Text(
"Increment",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.white),
),
),
const SizedBox(height: 40.0),
SizedBox(
width: 300.0,
child: TextFormField(
decoration: InputDecoration(
hintText: "Enter",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(11.0),
),
),
onChanged: (value) {
myController.incrementCounter();
},
),
),
],
),
),
);
}
}

import "package:get/get.dart";

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

void incrementCounter()
{
count++;
}

@override
void onInit()
{
/// called every time when the value of count variable changes
// ever(count, (_) { return print(count); },);

/// called every time when the value of any variable from the list changes
// everAll([count], (_) { return print(count); },);

/// called only once when the variable value changes
// once(count, (_) { return print(count); },);

/// called every time the user stops typing for 1 second
/// debounce() is generally used for searching purpose
// debounce(count,
// (_) { return print("When the user stop typing for 1 second the value of count will be printed");
// },
// time: const Duration(seconds: 1),
// );

/// Ignore all changes within 3 second.
/// Imagine that the user can earn coins by clicking on something,
/// if he clicked 300 times in the same minute,
/// he would have 300 coins, using interval,
/// you can set a time frame for 3 seconds,
/// and even then clicking 300 or a thousand times,
/// the maximum he would get in 1 minute would be 20 coins,
/// clicking 300 or 1 million times
interval(count, (_) { return print("Ignore all changes"); }, time: const Duration(seconds: 3),);

super.onInit();
}
}

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