flutter_composable_architecture 1.0.0
flutter_composable_architecture: ^1.0.0 copied to clipboard
A lightweight, composable Flutter state management framework inspired by Redux and Bloc patterns, providing a more modular and testable architectural solution.
Flutter Composable Architecture #
A lightweight, composable Flutter state management framework inspired by Redux and Bloc patterns, providing a more modular and testable architectural solution.
Features #
- ๐งฑ Composability: Easily compose business logic through Logic components
- โก Async Support: Built-in Effect handling for asynchronous operations and side effects
- ๐ State Tracking: Precisely track state changes via StateCompatible
- ๐งผ Middleware Support: Implement cross-cutting concerns like logging and API calls using MiddlewareCompatible
- ๐งช Testable: Pure functional design makes unit testing simple
- ๐ฆ Lightweight: Clean core code with no unnecessary dependencies
Quick Start #
Installation #
Add the dependency to your pubspec.yaml:
dependencies:
flutter_composable_architecture: ^1.0.0
Then run:
flutter pub get
Basic Usage #
- Define state class:
class AppState extends StateCompatible<AppState, Enum> {
int count = 0;
@override
List<Enum> diff(old) {
return count != old.count ? [AppChange.count] : [];
}
@override
AppState copy() {
return AppState()..count = count;
}
}
enum AppChange { count }
- Define actions:
abstract class AppAction {
const AppAction();
}
class IncrementAction extends AppAction {
const IncrementAction();
}
class DecrementAction extends AppAction {
const DecrementAction();
}
- Create logic handler:
class AppLogic extends LogicCompatible<AppState, AppAction> {
@override
AppState state = AppState();
@override
Future<Effect<AppAction>?> reduce(AppAction action) async {
switch (action.runtimeType) {
case IncrementAction:
state.count++;
break;
case DecrementAction:
state.count--;
break;
}
return null;
}
}
- Create Store and use:
final store = Store<AppState, AppAction, AppChange>(
initialLogic: () => AppLogic(),
);
// Listen to state changes
store.stateStream.listen((info) {
print('Count changed to: ${info.current.count}');
});
// Send action
store.send(const IncrementAction());
Core Concepts #
Store #
Store is the core of state management, responsible for:
- Maintaining application state
- Handling action dispatching
- Notifying state changes
Logic #
Logic is an abstraction of business logic that defines:
- Application's initial state
- Action handling function
reduce - State update logic
Effect #
Effect is used to handle side effects such as:
- API calls
- Navigation
- Asynchronous operations
Middleware #
Middleware provides a mechanism to intercept and process actions, commonly used for:
- Logging
- Performance monitoring
- Action transformation
Examples #
See test file for more usage examples.
License #
LICENSE