Flutter Clean Architecture Introduction
Overview
The flutter_clean_architecture
package is a tool designed to seamlessly implement Uncle Bob's Clean Architecture within Flutter projects. It provides foundational classes tailored for Flutter to promote a clean separation of concerns, making your Flutter applications scalable and maintainable.
Installation
Step 1: Add the Dependency
To use this package, add it to your pubspec.yaml
:
dependencies:
flutter_clean_architecture: ^6.0.1
Step 2: Install
Run the following command to install:
$ flutter packages get
Step 3: Import
Start using it in your Dart code with:
import 'package:flutter_clean_architecture/flutter_clean_architecture.dart';
Clean Architecture Primer
Introduction
Uncle Bob's Clean Architecture is a blend of several architectural patterns, focusing primarily on separating concerns for improved scalability. It features four main modules: App
, Domain
, Data
, and Device
.
Dependency Rule
Dependencies between source code layers only point inward. Inner layers are oblivious to the implementation details of outer layers, ensuring they remain pure business rules unaffected by outer dependencies.
Architectural Layers
Domain
The Domain
layer is where business logic lives, completely free from any platform-specific code, ensuring it can easily migrate across platforms if necessary.
- Entities: Core business classes that rarely change.
- Usecases: Define all application use cases, independent of UI changes.
- Repositories: Abstract classes dictating the behavior of data and devices without implementation.
App
App
, the presentation layer, connects to the Domain
using polymorphism. It contains the user interface and event handling for the application.
- View: The UI representation, which utilizes a
Controller
for events. - Controller: Delivers dynamic data and handlers for UI events without direct access to widgets.
- Presenter: Manages use cases and processes responses from business logic.
Data
This layer handles data retrieval, including server interactions and local storage management.
- Repositories: Implementations of
Domain
layer repositories, retrieving and processing data. - Models: Extensions of entities for platform-specific needs.
- Mappers: Convert entities to models and vice versa.
Device
The Device
layer communicates directly with the Android or iOS platform, handling native functionality such as GPS and filesystem operations.
- Devices: Classes for platform-specific functionalities.
- Utilities/Constants: Tools and constants for platform interactions.
Usage
Folder Structure
Organize your project files to maintain consistency and clarity:
lib/
app/
pages/
widgets/
utils/
data/
repositories/
helpers/
device/
repositories/
utils/
domain/
entities/
usecases/
repositories/
main.dart
Sample Code
Here are examples demonstrating the use of flutter_clean_architecture
:
Basic View Example
import 'package:flutter_clean_architecture/flutter_clean_architecture.dart';
class CounterPage extends CleanView {
@override
State<StatefulWidget> createState() => CounterState();
}
class CounterState extends CleanViewState<CounterPage, CounterController> {
CounterState() : super(CounterController());
@override
Widget get view => MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
key: globalKey,
body: Column(
children: <Widget>[
Center(
child: ControlledWidgetBuilder<CounterController>(
builder: (context, controller) {
return Text(controller.counter.toString());
}
),
),
ControlledWidgetBuilder<CounterController>(
builder: (context, controller) {
return MaterialButton(onPressed: controller.increment);
}
),
],
),
),
);
}
This project is designed to help developers structure their Flutter applications in a clean, organized manner, promoting a clear separation between the user interface and business logic layers for better manageability and scalability.