Skip to content

eamon831/getx_template

Repository files navigation

GetX Template

A robust, modular template for building scalable Flutter applications using the GetX package. This template promotes clean architecture, code reusability, and efficient state management.


Table of Contents


Getting Started

1. Change Package Name

Update the package name to match your project:

dart run change_app_package_name:main "com.getx.template"

2. Change App Name

Update the app name:

dart run rename_app:main all="GetX Template"

3. Change App Icon

Update the app icon (replace assets/images/app_logo.png):

dart run flutter_launcher_icons:main

4. Database Configuration

  • Offline Storage: Utilizes sqflite with a pre-configured db_helper.dart in the core folder.
  • Default DB File: assets/db/getx_template.db.
  • Steps to Update:
    1. Rename the file (e.g., my_app.db).
    2. Update the path in lib/app/core/db_helper/db_helper.dart.

Project Structure

Root Structure

assets/      - Static assets (images, fonts, database files).  
ios/         - iOS-specific files.  
android/     - Android-specific files.  
lib/         - Main directory for Dart code.  

lib Folder Structure

lib/  
  app/          - Main app logic.  
  flavor/       - Flavor configurations (dev, prod, etc.).  
  l10n/         - Localization files.  
  main_dev.dart - Entry point for development flavor.  
  main_prod.dart- Entry point for production flavor.  

lib/app Folder Structure

app/  
  bindings/       - Dependency injection (bindings).  
  core/           - Core logic, utilities, and reusable components.  
  entity/         - Data models/entities.  
  global_modal/   - App-wide modals.  
  global_widgets/ - Reusable widgets.  
  modules/        - Feature modules (e.g., Home, Login).  
  routes/         - Navigation routes.  
  services/       - API and backend services.  
  my_app.dart     - App initialization.  

lib/app/core Folder Structure

core/  
  base/           - Abstract classes (`BaseController`, `BaseView`).  
  core_model/     - Core models (e.g., LoggedUser, PageState).  
  db_helper/      - Database helper (`sqflite`).  
  session_manager/- Lightweight session management.  
  utils/          - Utility functions (e.g., validators).  
  values/         - App-wide constants (e.g., colors, themes).  
  widgets/        - Core widgets (e.g., buttons, inputs).  
  exporter.dart   - Centralized export file for core resources.  

Core Concepts

BaseController

The BaseController is a shared foundation for all controllers. It manages:

  1. State Management:
  • Page states (loading, success, error) via PageState.
  • Reactive updates with Rx variables for seamless UI synchronization.
  1. Utilities:
  • Internet checks with hasInternet().
  • Dynamic locale switching (changeLocale()).
  1. Error and Loader Management:
  • showLoader() and closeLoader() for loading overlays.
  • Centralized error handling with _handleException().
  1. Session Management:
  • Persistent preferences with SessionManager.
  1. Database Support:
  • Pre-configured DbHelper for SQLite.

BaseView

The BaseView class provides a consistent UI scaffold and integrates with BaseController.

  1. Dynamic UI Handling:
  • Observes PageState to render appropriate content (loading spinners, error views, etc.).
  1. Reusable Structure:
  • Predefined layout with customizable appBar(), body(), drawer(), and floatingActionButton().
  1. Error Handling and Debugging:
  • showErrorSnackBar() and showToast() for user feedback.
  • Debug tools for tracking current routes and states.

Working with Modules

Steps to Create a New Module

  1. Create Module Files:
  • Add a new folder under modules (e.g., home).
  • Create three files:
    • home_controller.dart
    • home_view.dart
    • home_binding.dart
  1. Set Up Routing:
  • Add the route to routes/app_pages.dart.
  1. Connect the Module:
  • Use HomeBinding to register dependencies.
  1. Implement Logic:
  • Extend BaseController for shared functionality.
  • Update UI in BaseView.

Examples

Binding

class HomeBinding extends Bindings {  
  @override  
  void dependencies() {  
    Get.lazyPut<HomeController>(() => HomeController());  
  }  
}  

Controller

class HomeController extends BaseController {  
  @override  
  Future<void> onInit() async {  
    super.onInit();  
    // Initialize logic here.  
  }  
}  

View

class HomeView extends BaseView<HomeController> {  
  @override  
  PreferredSizeWidget? appBar(BuildContext context) {  
    return AppBar(title: Text('Home'));  
  }  

  @override  
  Widget body(BuildContext context) {  
    return Center(child: Text('Welcome to Home'));  
  }  
}  

Why Use This Template?

  1. Scalable Architecture:
  • Modularized structure ensures maintainability and scalability.
  1. Reusable Components:
  • Shared utilities, widgets, and base classes minimize code duplication.
  1. State Management Simplified:
  • GetX integration for reactive programming and navigation.
  1. Customizable:
  • Easy to update package name, app name, icon, and database.
  1. Consistency:
  • Predefined patterns ensure uniformity across modules.

Notes

  • Core files in core/ are rarely modified—they handle reusable and common app logic.
  • Developers should focus on modules/, routes/, and services/ for feature development.
  • This template is ideal for medium to large Flutter projects.

Future Plan

  • create a CLI to generate modules and other files
  • add more examples

Happy coding! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors