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.
To get started with this project, follow these steps:
git clone https://github.com/Anhkato/flutter-bloc-clean-architecture.git
cd flutter-bloc-clean-architecture
flutter pub get
flutter run
For more detailed instructions, check the Releases section.
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
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.
To add a new feature, follow these steps:
lib/presentation/
for your feature.lib/presentation/blocs/
.lib/presentation/screens/
and lib/presentation/widgets/
.lib/data/
.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:
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
}
}
abstract class ExampleEvent {}
abstract class ExampleState {}
class LoadExampleEvent extends ExampleEvent {}
class LoadedExampleState extends ExampleState {}
BlocProvider(
create: (context) => ExampleBloc(),
child: ExampleScreen(),
);
This project uses get_it
for dependency injection. To set up your dependencies, follow these steps:
get_it
to your pubspec.yaml
:
dependencies:
get_it: ^7.2.0
final getIt = GetIt.instance;
void setup() {
getIt.registerLazySingleton<ExampleRepository>(() => ExampleRepositoryImpl());
}
final repository = getIt<ExampleRepository>();
Testing is crucial for maintaining code quality. This project includes example tests for both the Bloc and UI components.
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());
});
}
flutter test
Contributions are welcome! If you would like to contribute to this project, please follow these steps:
git checkout -b feature/YourFeature
).git commit -m 'Add some feature'
).git push origin feature/YourFeature
).This project is licensed under the MIT License. See the LICENSE file for details.
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.