New project using GetX

  • Steps

    1. open the android studio

    2.Create new project

    Note that in pubspec.yaml :
    
                            dependencies:
                                get: ^4.6.1
                            

    3. click on 'get dependency' link to install getX library

    4. run the project

    5. working files

    
                            //packages
                            1. pubspec.yaml
    
                            //controllers
                            2. controllers folder
    
                            //bindings  
                            3. bindings folder
    
                            //models 
                            4. models folder 
    
                            //views
                            5. views folder
    
                            //widgets (includes)
                            6. widgets folder
    
                            //routes
                            7. routes folder
    
                            //main layout 
                            8. main.dart 
    
    
                            
                            

    Let us create the files

    1. create new controller

    1) create home_controller.dart under lib/controllers folder

    2) import getX package class

    3) extend the class from GetxController class

    
                            // controllers/home_controller.dart
    
                            import 'package:get/get.dart';
                            class HomeController extends GetxController {
    
                            }
                            

    2. Create binding

    Bindings can be used to initialize your controllers, repositories, APIs, and whatever else you need without having to call them directly from the view page.

    1) create home_binding.dart under lib/bindings folder

    2) import your 'controller file (file name)' in the corresponding binding.dart

    3) import getX package class

    4) extend the class from Bindings class

    5) add your 'controller class (class name)' inside the dependencies() function

    
                          // bindings/home_binding.dart  
    
                          import 'package:<yourProjectName>/controllers/home_controller.dart';
                          import 'package:get/get.dart';
    
                          class HomeBinding extends Bindings {
                            @override
                            void dependencies() {
                              Get.lazyPut(() => HomeController());
                            }
                          }
    
                          

    1. replace with your project name in the pubspec.yaml

    3. Create view

    1) create home_page.dart under lib/views folder

    2) import your 'controller file (file name)'

    3) import getX package class

    4) extend your view class from StatelessWidget class

    5) inject your 'contoller class' (class name) into view class

    
                           // views/home_page.dart
    
                          import 'package:flutter/material.dart';
                          import 'package:<yourProjectName>/controllers/home_controller.dart';
                          import 'package:get/get.dart';
    
                          class HomePage extends StatelessWidget {
                            @override
                            Widget build(BuildContext context) {
                              return GetBuilder<HomeController>(
                                builder: (controller) => Scaffold(
                                  appBar: AppBar(
                                    title: const Text('Home'),
                                  ),
                                  body: const Center(
                                    child: Text('Hello World!'),
                                  ),
                                ),
                              );
                            }
                          }
    
                          

    1. replace with your project name in the pubspec.yaml

    2. If you don't need APP top bar, remove the code

    
                              appBar: AppBar(
                                    title: const Text('Home'),
                                  ),
                                

    4. create routes

    In GetX before navigating from one screen to another we require routes. So let's create routes.

    1) create app_pages.dart under lib/routes

    2) import your controller file (file name)

    3) import your binding file (file name)

    4) import getX package class

    5) define AppPages class

    6) add your route using GetPage()

    
                            // routes/app_pages.dart
                          
                            import 'package:<yourProjectName>/bindings/home_binding.dart';
                            import 'package:<yourProjectName>/views/home_page.dart';                      
                            import 'package:get/get.dart';
    
                            class AppPages {
                              static final List<GetPage> pages = [
                                GetPage(
                                  name: '/home',
                                  page: () => HomePage(),
                                  binding: HomeBinding(),
                                ),
                              ];
                            }
                          

    1. replace with your project name in the pubspec.yaml

    5. change main page

    Now change your MaterialApp widget with GetMaterialApp in main.dart and initialize your page.

    1) import your binding file (file name)

    2) import your view file (file name)

    3) import your route file (file name)

    4) import getX package class

    5) set the parameters of GetMaterialApp() 1) initialBinding, 2) home, and 3)getPages

    
                          import 'package:flutter/material.dart';
                          import 'package:<yourProjectName>/bindings/home_binding.dart';
                          import 'package:<yourProjectName>/views/home_page.dart';
                          import 'package:<yourProjectName>/routes/app_pages.dart';
                          import 'package:get/get.dart';
    
                          void main() {
                            runApp(const MyApp());
                          }
    
                          class MyApp extends StatelessWidget {
                            const MyApp({Key? key}) : super(key: key);
    
                            // This widget is the root of your application.
                            @override
                            Widget build(BuildContext context) {
                              return GetMaterialApp(
                                debugShowCheckedModeBanner: false,
                                title: 'Flutter Demo',
                                theme: ThemeData(primarySwatch: Colors.blue),
                                initialBinding: HomeBinding(),
                                home: HomePage(),
                                getPages: AppPages.pages,
                              );
                            }
                          }
    
                          
    Output