Skip to content

Sumat-Dev/flutter-getx-mvc-architecture-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Flutter GetX MVC Architecture Guide

A comprehensive Flutter project demonstrating MVC (Model-View-Controller) architecture with GetX state management.

πŸ—οΈ Project Structure

lib/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ bindings/           # Dependency injection bindings
β”‚   β”‚   └── initial_binding.dart
β”‚   β”œβ”€β”€ data/               # Data layer
β”‚   β”‚   β”œβ”€β”€ models/         # Data models
β”‚   β”‚   β”‚   β”œβ”€β”€ user.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ product.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ order.dart
β”‚   β”‚   β”‚   └── profile.dart
β”‚   β”‚   β”œβ”€β”€ providers/      # API providers
β”‚   β”‚   β”‚   └── api_provider.dart
β”‚   β”‚   └── repositories/   # Data repositories
β”‚   β”‚       β”œβ”€β”€ auth_repository.dart
β”‚   β”‚       β”œβ”€β”€ product_repository.dart
β”‚   β”‚       β”œβ”€β”€ order_repository.dart
β”‚   β”‚       └── profile_repository.dart
β”‚   β”œβ”€β”€ middlewares/        # Route middlewares
β”‚   β”‚   └── auth_middleware.dart
β”‚   β”œβ”€β”€ modules/            # Feature modules
β”‚   β”‚   β”œβ”€β”€ auth/           # Authentication module
β”‚   β”‚   β”‚   β”œβ”€β”€ login_binding.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ login_controller.dart
β”‚   β”‚   β”‚   └── login_view.dart
β”‚   β”‚   β”œβ”€β”€ home/           # Home module
β”‚   β”‚   β”‚   β”œβ”€β”€ home_binding.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ home_controller.dart
β”‚   β”‚   β”‚   └── home_view.dart
β”‚   β”‚   β”œβ”€β”€ products/       # Products module
β”‚   β”‚   β”‚   β”œβ”€β”€ products_binding.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ products_controller.dart
β”‚   β”‚   β”‚   └── products_view.dart
β”‚   β”‚   β”œβ”€β”€ orders/         # Orders module
β”‚   β”‚   β”‚   β”œβ”€β”€ orders_binding.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ orders_controller.dart
β”‚   β”‚   β”‚   └── orders_view.dart
β”‚   β”‚   └── profile/        # Profile module
β”‚   β”‚       β”œβ”€β”€ profile_binding.dart
β”‚   β”‚       β”œβ”€β”€ profile_controller.dart
β”‚   β”‚       └── profile_view.dart
β”‚   β”œβ”€β”€ routes/             # App routing
β”‚   β”‚   β”œβ”€β”€ app_pages.dart
β”‚   β”‚   └── app_routes.dart
β”‚   β”œβ”€β”€ services/           # Business logic services
β”‚   β”‚   └── auth_service.dart
β”‚   β”œβ”€β”€ theme/              # App theming
β”‚   β”‚   └── app_theme.dart
β”‚   └── utils/              # Utility functions
β”‚       └── validators.dart
└── main.dart               # App entry point

🎯 Architecture Overview

MVC Pattern with GetX

This project follows the Model-View-Controller pattern enhanced with GetX for state management:

  • Model: Data models and repositories in the data/ folder
  • View: UI components in the modules/*/views/ folders
  • Controller: Business logic in the modules/*/controllers/ folders

Key Components

1. Bindings (app/bindings/)

  • Dependency injection setup for each module
  • Manages the lifecycle of controllers and services

2. Data Layer (app/data/)

  • Models: Data structures with JSON serialization
  • Providers: HTTP client for API communication
  • Repositories: Data access abstraction layer

3. Modules (app/modules/)

  • Feature-based organization
  • Each module contains its own MVC components
  • Self-contained with bindings, controllers, and views

4. Services (app/services/)

  • Business logic that spans multiple modules
  • Authentication, notifications, etc.

5. Routes (app/routes/)

  • Centralized routing configuration
  • Route guards and middleware support

πŸ“± Features

Authentication Module

  • User login/logout
  • Route protection with middleware
  • Session management

Home Module

  • Dashboard with navigation
  • Bottom navigation bar
  • Module overview

Products Module

  • Product listing with search
  • Category filtering
  • Product details

Orders Module

  • Order management
  • Status tracking
  • Order history

Profile Module

  • User profile management
  • Editable form fields
  • Avatar handling

πŸ› οΈ Dependencies

Core Dependencies

  • get: ^4.6.6 - State management and routing
  • http: ^1.1.0 - HTTP client for API calls

Dev Dependencies

  • very_good_analysis: ^9.0.0 - Comprehensive code analysis and linting

πŸ“š Usage Examples

Creating a New Module

1.Create the module folder structure:

   lib/app/modules/new_feature/
   β”œβ”€β”€ new_feature_binding.dart
   β”œβ”€β”€ new_feature_controller.dart
   └── new_feature_view.dart

2.Add the binding:

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

3.Create the controller:

class NewFeatureController extends GetxController {
  final _data = ''.obs;
  
  String get data => _data.value;
  
  void updateData(String value) => _data.value = value;
}

4.Build the view:

class NewFeatureView extends GetView<NewFeatureController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('New Feature')),
      body: Center(
        child: Obx(() => Text(controller.data)),
      ),
    );
  }
}

5.Add to routes:

GetPage(
  name: '/new-feature',
  page: () => NewFeatureView(),
  binding: NewFeatureBinding(),
),

Using Controllers

class ExampleController extends GetxController {
  final _count = 0.obs;
  
  int get count => _count.value;
  
  void increment() => _count.value++;
  void decrement() => _count.value--;
}

Reactive UI with Obx

Obx(() => Text('Count: ${controller.count}'))

πŸ“– Best Practices

1. Module Organization

  • Keep modules self-contained
  • Use consistent naming conventions
  • Separate concerns (UI, logic, data)

2. State Management

  • Use .obs for reactive variables
  • Keep controllers focused and lightweight
  • Avoid business logic in views

3. Error Handling

  • Implement proper error handling in repositories
  • Show user-friendly error messages
  • Log errors for debugging

4. Performance

  • Use Get.lazyPut() for lazy loading
  • Implement proper disposal in controllers
  • Optimize list views with ListView.builder

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Guide: MVC Architecture with GetX State Management

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages