Anhkato

Flutter Bloc Clean Architecture - A Comprehensive Boilerplate Project

Flutter Bloc Clean Architecture

Table of Contents

Overview

This repository contains a Flutter project that demonstrates the use of the Bloc pattern with clean architecture. The project serves as a boilerplate template for developers looking to implement scalable and maintainable Flutter applications. It emphasizes separation of concerns, making it easier to manage the codebase.

Features

Technologies Used

Getting Started

To get started with this project, follow these steps:

  1. Clone the repository:
    git clone https://github.com/Anhkato/flutter-bloc-clean-architecture.git
    cd flutter-bloc-clean-architecture
    
  2. Install dependencies:
    flutter pub get
    
  3. Run the application:
    flutter run
    

For more detailed instructions, check the Releases section.

Project Structure

The project follows a well-defined structure to separate different concerns:

lib/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ repositories/
β”‚   └── data_sources/
β”œβ”€β”€ domain/
β”‚   β”œβ”€β”€ entities/
β”‚   β”œβ”€β”€ usecases/
β”‚   └── repositories/
β”œβ”€β”€ presentation/
β”‚   β”œβ”€β”€ blocs/
β”‚   β”œβ”€β”€ screens/
β”‚   └── widgets/
└── main.dart

Usage

This boilerplate provides a solid foundation for building Flutter applications. You can extend the existing functionality or add new features as needed. The Bloc pattern helps manage the state effectively, while clean architecture ensures that your code remains organized.

Adding a New Feature

To add a new feature, follow these steps:

  1. Create a new folder under lib/presentation/ for your feature.
  2. Define your Bloc in lib/presentation/blocs/.
  3. Create necessary screens and widgets in lib/presentation/screens/ and lib/presentation/widgets/.
  4. Implement your data models and repositories in lib/data/.

Bloc Implementation

The Bloc pattern is a core part of this project. It helps manage the application state by separating business logic from UI code. Here’s a brief overview of how to implement a Bloc:

  1. Create a Bloc class:
    import 'package:flutter_bloc/flutter_bloc.dart';
    
    class ExampleBloc extends Bloc<ExampleEvent, ExampleState> {
      ExampleBloc() : super(InitialExampleState());
    
      @override
      Stream<ExampleState> mapEventToState(ExampleEvent event) async* {
        // Handle events and yield states
      }
    }
    
  2. Define Events and States:
    abstract class ExampleEvent {}
    abstract class ExampleState {}
    
    class LoadExampleEvent extends ExampleEvent {}
    class LoadedExampleState extends ExampleState {}
    
  3. Use Bloc in UI:
    BlocProvider(
      create: (context) => ExampleBloc(),
      child: ExampleScreen(),
    );
    

Dependency Injection

This project uses get_it for dependency injection. To set up your dependencies, follow these steps:

  1. Add dependencies: Add get_it to your pubspec.yaml:
    dependencies:
      get_it: ^7.2.0
    
  2. Register your services: Create a service locator:
    final getIt = GetIt.instance;
    
    void setup() {
      getIt.registerLazySingleton<ExampleRepository>(() => ExampleRepositoryImpl());
    }
    
  3. Access services: Retrieve your services anywhere in the app:
    final repository = getIt<ExampleRepository>();
    

Testing

Testing is crucial for maintaining code quality. This project includes example tests for both the Bloc and UI components.

Writing Tests

  1. Create a test file:
    import 'package:flutter_test/flutter_test.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    
    void main() {
      test('Initial state is correct', () {
        final bloc = ExampleBloc();
        expect(bloc.state, InitialExampleState());
      });
    }
    
  2. Run tests: Use the following command to run your tests:
    flutter test
    

Contributing

Contributions are welcome! If you would like to contribute to this project, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add some feature').
  5. Push to the branch (git push origin feature/YourFeature).
  6. Open a pull request.

License

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

Releases

To view the latest releases and download the project files, visit the Releases section. Download the files and execute them to get started with your Flutter application.