flutter_composable_architecture 1.0.0 copy "flutter_composable_architecture: ^1.0.0" to clipboard
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 #

  1. 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 }
  1. Define actions:
abstract class AppAction {
  const AppAction();
}

class IncrementAction extends AppAction {
  const IncrementAction();
}

class DecrementAction extends AppAction {
  const DecrementAction();
}
  1. 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;
  }
}
  1. 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

0
likes
130
points
82
downloads

Publisher

unverified uploader

Weekly Downloads

A lightweight, composable Flutter state management framework inspired by Redux and Bloc patterns, providing a more modular and testable architectural solution.

Topics

#state-management #architecture #redux #bloc #composable

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on flutter_composable_architecture