Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter easy getx

people ask me how i manage state,dependency,routes etc when i work with flutter,here is the simple brief about getx which i used for dummy basic ecommerce concept based flutter app development .
here i cover

  • state management
  • route management
  • dependency management
  • internationalization
  • storage

getting started

dependencies

dependencies:
get: ^4.3.0

get_storage: ^2.0.3

installing

  • flutter pub add get

executing program

  • state management
class purchase extends getxcontroller {
  var products = <product>[].obs;
  .........
 

obs to observe the products that can be changed. similarly when any user will do add to cart it needs to be observed so

class democontroller extends getxcontroller {
  var cartitems = <product>[].obs;
  int get cartcount => cartitems.length;
   

for more check out the controller and home.dart and demo.dart

  • route management

initialize the routes in main.dart

  routes: {
          //routes for named navigation
          '/': (context) => homepage(),
          '/cart': (context) => demopage(),
        },
        
        -------
 

now you can called by named route using getx.

  iconbutton(
                      onpressed: () => get.tonamed('/cart',
                          arguments:
                              "home page to demo page -> passing arguments"), //sending arguments 
                      icon: icon(
                        icons.arrow_forward_ios_rounded,
                        color: colors.white,
                      ))
     ......
 

  • dependency management
    handle controller for dependency injection (controller).
 
  final var ctrl = get.put(democontroller());
  .......
 

after you add this once in your project you can find it out , you dont need to re connect it again and again just use get.find()

 
 final democontroller ctrl = get
      .find(); //getting the cart controller , you can show amount or anything
  .......
 

  • internationalization

change theme or language easily . here is the demo
main.dart

 
void main() async {
  await getstorage.init();
  runapp(myapp());
}


class myapp extends statelesswidget {
  // this widget is the root of your application.
  final democontroller ctrl = get.put(democontroller());
  @override
  widget build(buildcontext context) {
    return simplebuilder(builder: (_) {
      // for darkmode instant change
      return getmaterialapp(
        //for navigation dont forget to use getmaterialapp
        title: 'easy getx',
        theme: ctrl.theme,
        debugshowcheckedmodebanner: false,
        initialroute: '/',
        routes: {
          //routes for named navigation
          '/': (context) => homepage(),
          '/cart': (context) => demopage(),
        },
      );
    });
  }
}

 

in the controller end :

 
 bool get isdark => storage.read('darkmode') ?? false;
  themedata get theme => isdark ? themedata.dark() : themedata.light();
  void changetheme(bool val) => storage.write('darkmode', val);

  .......
 

to trigger it from anywhere [checkout the demopage.dart file]

switchlisttile(
              value: ctrl.isdark,
              title: text("touch to change thememode"),
              onchanged: ctrl.changetheme,
            ),
  .......
 

  • storage [alternative of shared preference] just need to write getstorage();
  final storage = getstorage();
  bool get isdark => storage.read('darkmode') ?? false;
  themedata get theme => isdark ? themedata.dark() : themedata.light();
  void changetheme(bool val) => storage.write('darkmode', val);

help

if you need any help regarding this

[email protected]

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.