flutter_redux_hooks
A set of utilities that allow you to easily consume a Redux Store to build Flutter Widgets.
This package is built to work with Redux.dart 5.0.0+.
This library is based on flutter_redux, it actually started as a fork of that project. The implementation of StoreProvider
available here is the same you will find in that lib, I removed the rest of the widgets offered by it and replaced them with my hooks. Clearly, I aimed to replicate the behavior of the hooks you'll find in react-redux, if you think I succeeded let me know by leaving a star in the repo!
Redux Widgets
StoreProvider
- The base Widget. It will pass the given Redux Store to all descendants that request it.
Redux hooks
useStore
- Will return a reference to the store you provided viaStoreProvider
.useDispatch
- Will return a reference to the dispatch function for the store you provided viaStoreProvider
.useSelector
- Will return the result of applying a selector function to the state. To make these selectors I recommend either usingreselect
orredux_toolkit
(redux_toolkit
exportsreselect
).
Companion Libraries
- flipperkit_redux_middleware - Redux Inspector (use Flutter Debugger) for Flutter Redux apps
- flutter_redux_dev_tools - Time Travel Dev Tools for Flutter Redux apps
- redux_persist - Persist Redux State
- flutter_redux_navigation - Use redux events for navigation
- redux_toolkit - Dart port of the official, opinionated, batteries-included toolset for efficient Redux development.
Usage
Let's demo the basic usage with the all-time favorite: A counter example!
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart' show HookWidget;
import 'package:flutter_redux_hooks/flutter_redux_hooks.dart';
import 'package:redux/redux.dart';
enum Actions { Increment }
int counterReducer(int state, dynamic action) {
if (action == Actions.Increment) {
return state + 1;
}
return state;
}
void main() {
final store = Store<int>(counterReducer, initialState: 0);
runApp(
StoreProvider<int>(
store: store,
child: FlutterReduxApp(
title: 'Flutter Redux Demo',
),
),
);
}
class FlutterReduxApp extends HookWidget {
final String title;
FlutterReduxApp({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
final dispatch = useDispatch<int>();
final count = useSelector<int, String>((state) => state.toString());
return MaterialApp(
theme: ThemeData.dark(),
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'You have pushed the button this many times:',
),
Text(
count,
style: TextStyle(color: Colors.white, fontSize: 36),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => dispatch(Actions.Increment),
child: Icon(Icons.add),
),
),
);
}
}