Skip to content

πŸ—οΈ Complete Clean Architecture Flutter demo with BLoC, dependency injection, and comprehensive documentation

License

Notifications You must be signed in to change notification settings

CodeBlood004/clean_architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—οΈ Clean Architecture Flutter Demo

Flutter Dart License: MIT

A comprehensive Flutter application demonstrating Clean Architecture principles with BLoC state management. This project serves as a complete reference implementation for building scalable, maintainable, and testable Flutter applications.

🌟 Features

  • βœ… Clean Architecture implementation (Domain, Data, Presentation layers)
  • βœ… BLoC Pattern for state management
  • βœ… Dependency Injection with GetIt
  • βœ… Repository Pattern for data access
  • βœ… Model-View-BLoC architecture
  • βœ… Error Handling with custom exceptions and failures
  • βœ… Local Storage with SharedPreferences
  • βœ… HTTP API Integration (mock implementation)
  • βœ… Form Validation and input handling
  • βœ… Navigation Management with custom router
  • βœ… Theme System (Light/Dark mode support)
  • βœ… Responsive Design with consistent spacing
  • βœ… Code Documentation for all files and functions
  • βœ… Extension Methods for enhanced functionality
  • βœ… Custom Widgets and reusable components

πŸš€ Demo Credentials

For testing the application, use these dummy login credentials:

Email: test@example.com
Password: password

Note: These are the actual mock credentials hardcoded in the authentication system. Any other credentials will result in an "Invalid credentials" error.

πŸ“± Screenshots

Login Screen Home Screen

Login Screen Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  Home Screen

πŸ›οΈ Architecture Overview

This project follows Uncle Bob's Clean Architecture principles with three distinct layers:

πŸ”΅ Domain Layer (Business Logic)

  • Entities: Core business objects
  • Repositories: Abstract interfaces for data operations
  • Use Cases: Business logic implementation

🟑 Data Layer (Data Access)

  • Models: Data representations with JSON serialization
  • Repositories: Concrete implementations of domain interfaces
  • Data Sources: Remote (API) and Local (Cache) data sources
  • Services: External service integrations

🟒 Presentation Layer (UI)

  • Screens: UI pages and user interfaces
  • Widgets: Reusable UI components
  • BLoC: State management and business logic coordination

βš™οΈ Core Layer (Infrastructure)

  • Constants: App-wide constants and configuration
  • Utils: Helper functions and utilities
  • Routes: Navigation and routing logic
  • Errors: Exception handling and error management
  • DI: Dependency injection setup

πŸ“‚ Project Structure

lib/
β”œβ”€β”€ πŸ“ core/                          # Core infrastructure layer
β”‚   β”œβ”€β”€ πŸ“ constants/                 # App-wide constants
β”‚   β”‚   β”œβ”€β”€ app_colors.dart          # Color palette and theming
β”‚   β”‚   β”œβ”€β”€ app_constants.dart       # App configuration constants
β”‚   β”‚   β”œβ”€β”€ app_dimensions.dart      # UI spacing and sizing
β”‚   β”‚   β”œβ”€β”€ app_strings.dart         # Text constants and i18n prep
β”‚   β”‚   β”œβ”€β”€ app_themes.dart          # Material theme configuration
β”‚   β”‚   └── api_endpoints.dart       # API endpoint definitions
β”‚   β”œβ”€β”€ πŸ“ di/                       # Dependency injection
β”‚   β”‚   └── injection_container.dart # GetIt service locator setup
β”‚   β”œβ”€β”€ πŸ“ errors/                   # Error handling
β”‚   β”‚   β”œβ”€β”€ exceptions.dart          # Custom exception classes
β”‚   β”‚   └── failures.dart           # Domain failure classes
β”‚   β”œβ”€β”€ πŸ“ helpers/                  # Utility helpers
β”‚   β”‚   └── network_helper.dart     # Network connectivity utilities
β”‚   β”œβ”€β”€ πŸ“ routes/                   # Navigation and routing
β”‚   β”‚   β”œβ”€β”€ app_router.dart          # Route generation and handling
β”‚   β”‚   β”œβ”€β”€ app_routes.dart          # Route path constants
β”‚   β”‚   └── navigation_service.dart  # Global navigation service
β”‚   └── πŸ“ utils/                    # Utility functions
β”‚       β”œβ”€β”€ date_helper.dart         # Date formatting and utilities
β”‚       β”œβ”€β”€ string_extensions.dart   # String extension methods
β”‚       └── validators.dart          # Form validation utilities
β”œβ”€β”€ πŸ“ data/                         # Data access layer
β”‚   β”œβ”€β”€ πŸ“ datasources/             # Data source abstractions
β”‚   β”‚   β”œβ”€β”€ local_data_source.dart   # Local storage interface/impl
β”‚   β”‚   └── remote_data_source.dart  # API data source interface/impl
β”‚   β”œβ”€β”€ πŸ“ models/                   # Data models with JSON serialization
β”‚   β”‚   β”œβ”€β”€ api_response_model.dart  # Generic API response wrapper
β”‚   β”‚   β”œβ”€β”€ post_model.dart          # Post data model
β”‚   β”‚   └── user_model.dart          # User data model
β”‚   β”œβ”€β”€ πŸ“ repositories/             # Repository implementations
β”‚   β”‚   └── user_repository_impl.dart # User repository concrete impl
β”‚   └── πŸ“ services/                 # External service integrations
β”‚       β”œβ”€β”€ api_service.dart         # HTTP API service
β”‚       β”œβ”€β”€ local_storage_service.dart # Local storage service
β”‚       └── notification_service.dart # Push notification service
β”œβ”€β”€ πŸ“ domain/                       # Business logic layer
β”‚   β”œβ”€β”€ πŸ“ entities/                 # Core business entities
β”‚   β”‚   β”œβ”€β”€ post_entity.dart         # Post business entity
β”‚   β”‚   └── user_entity.dart         # User business entity
β”‚   └── πŸ“ repositories/             # Repository interfaces
β”‚       β”œβ”€β”€ post_repository.dart     # Post repository interface
β”‚       └── user_repository.dart     # User repository interface
β”œβ”€β”€ πŸ“ presentation/                 # UI and state management layer
β”‚   β”œβ”€β”€ πŸ“ bloc/                     # BLoC state management
β”‚   β”‚   β”œβ”€β”€ πŸ“ auth/                 # Authentication BLoC
β”‚   β”‚   β”‚   β”œβ”€β”€ auth_bloc.dart       # Auth business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ auth_event.dart      # Auth events
β”‚   β”‚   β”‚   └── auth_state.dart      # Auth states
β”‚   β”‚   └── πŸ“ posts/                # Posts BLoC
β”‚   β”‚       β”œβ”€β”€ posts_bloc.dart      # Posts business logic
β”‚   β”‚       β”œβ”€β”€ posts_event.dart     # Posts events
β”‚   β”‚       └── posts_state.dart     # Posts states
β”‚   β”œβ”€β”€ πŸ“ screens/                  # Application screens
β”‚   β”‚   β”œβ”€β”€ πŸ“ auth/                 # Authentication screens
β”‚   β”‚   β”‚   β”œβ”€β”€ login_screen.dart    # Login interface
β”‚   β”‚   β”‚   └── πŸ“ widgets/          # Auth-specific widgets
β”‚   β”‚   β”‚       └── login_form.dart  # Login form component
β”‚   β”‚   β”œβ”€β”€ πŸ“ home/                 # Home screen
β”‚   β”‚   β”‚   β”œβ”€β”€ home_screen.dart     # Main dashboard
β”‚   β”‚   β”‚   └── πŸ“ widgets/          # Home-specific widgets
β”‚   β”‚   β”‚       └── user_info_card.dart # User info component
β”‚   β”‚   β”œβ”€β”€ πŸ“ posts/                # Posts screens
β”‚   β”‚   β”‚   β”œβ”€β”€ posts_screen.dart    # Posts list screen
β”‚   β”‚   β”‚   └── πŸ“ widgets/          # Posts-specific widgets
β”‚   β”‚   β”‚       β”œβ”€β”€ post_card.dart   # Post display card
β”‚   β”‚   β”‚       └── post_item.dart   # Post list item
β”‚   β”‚   β”œβ”€β”€ πŸ“ profile/              # Profile screens
β”‚   β”‚   β”‚   └── profile_screen.dart  # User profile interface
β”‚   β”‚   └── πŸ“ splash/               # Splash screen
β”‚   β”‚       └── splash_screen.dart   # App initialization screen
β”‚   └── πŸ“ widgets/                  # Reusable UI components
β”‚       └── πŸ“ common/               # Common widgets
β”‚           β”œβ”€β”€ custom_app_bar.dart  # Reusable app bar
β”‚           β”œβ”€β”€ custom_button.dart   # Styled button component
β”‚           β”œβ”€β”€ custom_text_field.dart # Styled input field
β”‚           β”œβ”€β”€ error_widget.dart    # Error display component
β”‚           β”œβ”€β”€ loading_widget.dart  # Loading indicators
β”‚           └── skeleton_loader.dart # Skeleton loading animation
β”œβ”€β”€ main.dart                        # Application entry point
└── πŸ“ test/                         # Test files
    └── widget_test.dart            # Widget testing suite

πŸ› οΈ Getting Started

Prerequisites

  • Flutter SDK (3.0.0 or higher)
  • Dart SDK (2.17.0 or higher)
  • Android Studio / VS Code with Flutter extensions
  • Git

Installation

  1. Clone the repository

    git clone https://github.com/CodeBlood004/clean_architecture.git
    cd clean_architecture
  2. Install dependencies

    flutter pub get
  3. Run the application

    flutter run

Development Setup

  1. Install recommended VS Code extensions:

    • Flutter
    • Dart
    • Bloc
    • GitLens
  2. Configure your IDE:

    • Enable Dart formatting on save
    • Set up Flutter snippets
    • Configure debugging breakpoints

πŸ”§ Configuration

Environment Setup

The app is currently configured with mock data and services. To integrate with real APIs:

  1. Update API endpoints in /lib/core/constants/api_endpoints.dart
  2. Configure real HTTP client in /lib/data/services/api_service.dart
  3. Update data sources in /lib/data/datasources/remote_data_source.dart
  4. Add environment variables for different build configurations

Theme Customization

Customize the app's appearance by modifying:

  • Colors: /lib/core/constants/app_colors.dart
  • Spacing: /lib/core/constants/app_dimensions.dart
  • Themes: /lib/core/constants/app_themes.dart
  • Text: /lib/core/constants/app_strings.dart

πŸ§ͺ Testing

Running Tests

# Run all tests
flutter test

# Run tests with coverage
flutter test --coverage

# Run integration tests
flutter test integration_test/

Test Structure

  • Unit Tests: Business logic testing
  • Widget Tests: UI component testing
  • Integration Tests: End-to-end flow testing

πŸ“š Key Learning Points

This project demonstrates:

🎯 Clean Architecture Benefits

  • Separation of Concerns: Each layer has distinct responsibilities
  • Dependency Inversion: High-level modules don't depend on low-level modules
  • Testability: Easy to unit test business logic in isolation
  • Maintainability: Changes in one layer don't affect others
  • Scalability: Easy to add new features and modify existing ones

πŸ”„ BLoC Pattern Implementation

  • Event-Driven Architecture: UI triggers events, BLoC processes them
  • State Management: Predictable state changes and updates
  • Business Logic Separation: UI is purely reactive to state changes
  • Testing: Easy to test business logic with mock events and states

πŸ—οΈ Repository Pattern

  • Data Abstraction: Domain layer doesn't know about data sources
  • Multiple Data Sources: Seamless switching between remote/local data
  • Caching Strategy: Offline-first approach with proper fallbacks
  • Error Handling: Consistent error management across data operations

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Contribution Guidelines

  • Follow the existing code style and architecture patterns
  • Add appropriate documentation for new features
  • Include tests for new functionality
  • Update README if necessary

πŸ“„ License

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

πŸ‘¨β€πŸ’» Author

CodeBlood004

πŸ™ Acknowledgments

πŸ“ˆ Project Stats

  • Language: Dart
  • Framework: Flutter
  • Architecture: Clean Architecture + BLoC
  • State Management: BLoC Pattern
  • Dependency Injection: GetIt
  • Testing: Widget & Unit Tests
  • Documentation: Comprehensive inline docs

⭐ If you found this project helpful, please consider giving it a star! ⭐

This project serves as a complete reference for implementing Clean Architecture in Flutter applications. Feel free to use it as a starting point for your own projects or as a learning resource.