Thursday, July 21, 2022

GetX with GetX/Obx in Reactive state management

  GetX With GetX




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

void main(){
  runApp(GetMaterialApp(debugShowCheckedModeBanner: false,
                        home: Home(),));
}

class Home extends StatelessWidget {

  final Controller controller=Get.put(Controller());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Get X"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GetX<Controller>(
              builder: (_)=>Text("clicks:${controller.count} "),
              ),
            ElevatedButton(
              child: Text("Next Route"),
              onPressed: ()=>Get.to(Second()),
            ),
        ]),
        ),
     
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: ()=>controller.increment(),
      ),      
    );
  }
}

class Second extends StatelessWidget{
  final Controller ctrl=Get.find();

  @override
  Widget build(context){
    return Scaffold(
      body: Center(
        child: Text("${ctrl.count}"),
      ),
    );
  }
}

class Controller extends GetxController{
  var count=0.obs;
  void increment(){
    count++;
    // Automatically update because GetX is reactive
  }
}



No comments:

Post a Comment

Featured post

Compress Image With Show File Size & Resolution in Flutter

 Compress Image With Show File Size & Resolution  1.Multiple File Image Compress with file Size import 'dart:io' ; import 'p...

LightBlog