Flying Redux

build codecov

What is Flying Redux?

Flying Redux is also an assembled flutter application framework based on Redux state management.

Features

It has four characteristics:

  1. Functional Programming
  1. Predictable state container
  1. Pluggable componentization
  1. Support null safety and flutter 3.x

Getting started

There are five steps to use the counter as an example:

  1. Add flying_redux package
  1. Create a state class and initialize the state
  1. Define Action and Create ActionCreator
  1. Create a function Reducer that modifies the state
  1. Create a widgets view and Page for display
import 'package:flying_redux/flying_redux.dart';

/// [State]
class PageState extends Cloneable<PageState> {
  late int count;

  @override
  PageState clone() {
    return PageState()..count = count;
  }
}

/// [InitState]
PageState initState(Map<String, dynamic>? args) {
  //just do nothing here...
  return PageState()..count = 0;
}

/// [Action]
enum CounterAction { increment }

/// [ActionCreator]
class CounterActionCreator {
  static Action increment() {
    return const Action(CounterAction.increment, payload: 1);
  }
}

/// [Reducer]
buildReducer() {
  return asReducer(<Object, Reducer<PageState>>{
    CounterAction.increment: _increment,
  });
}

PageState _increment(PageState state, Action action) {
  final int num = action.payload;
  return state.clone()..count = (state.count + num);
}

/// [Page]
class CountPage extends Page<PageState, Map<String, dynamic>> {
  CountPage()
      : super(
            initState: initState,
            reducer: buildReducer(),
            view: (PageState state, Dispatch dispatch, ComponentContext<PageState> ctx) {
              return Scaffold(
                body: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      const Text(
                        'You have pushed the button this many times:',
                      ),
                      Text(state.count.toString()),
                    ],
                  ),
                ),
                floatingActionButton: FloatingActionButton(
                  onPressed: () => dispatch(CounterActionCreator.increment()),
                  tooltip: 'Increment',
                  child: const Icon(Icons.add),
                ), // This trailing comma makes auto-formatting nicer for build methods.
              );
            });
}

Usage

If you want to know specific usage examples, please refer to the todo list code in the example project and in the /example folder.

  • todo list - a simple todo list demo.
  • run it:
cd ./example
flutter run

Additional information

In particular, the code of flying-redux has the same naming and implementation as fish-redux. Because fish-redux has not been updated for a long time. I have done a lot of refactoring and modification based on fish_redux, and simplified some concepts, and finally renamed it.

Libraries

flying_redux