fish_redux 的主要使用场景是单个页面的状态管理,虽然也可以用作全局状态管理,但我更喜欢把全局状态管理存放于 flutter_redux 中,以下示例将两个状态管理工具进行混用。

    stores/app_store.dart

    1. // 枚举 Actions
    2. enum Actions { Increment, Decrease }
    3. // 创建 reducer
    4. Map<String, dynamic> appReducer(Map<String, dynamic> state, dynamic action) {
    5. print(state);
    6. print(action);
    7. if (action == Actions.Increment) {
    8. state['num'] += 1;
    9. } else if (action == Actions.Decrease) {
    10. state['num'] -= 1;
    11. }
    12. return state;
    13. }

    main.dart

    1. import 'package:flutter/material.dart';
    2. import 'package:flutter_app_bootstrapper/routes/app_route.dart';
    3. import 'package:flutter_app_bootstrapper/stores/app_store.dart';
    4. import 'package:flutter_redux/flutter_redux.dart';
    5. import 'package:redux/redux.dart';
    6. void main() {
    7. // 创建store
    8. final store = new Store<Map<String, dynamic>>(appReducer, initialState: {'num': 0});
    9. // 将store传入根Widget
    10. runApp(new MyApp(
    11. store: store,
    12. ));
    13. }
    14. class MyApp extends StatelessWidget {
    15. final Store<Map<String, dynamic>> store;
    16. MyApp({Key key, this.store}) : super(key: key);
    17. @override
    18. Widget build(BuildContext context) {
    19. return new StoreProvider<Map<String, dynamic>>(
    20. store: store,
    21. child: MaterialApp(
    22. title: 'Flutter Demo',
    23. theme: ThemeData(
    24. primarySwatch: Colors.blue,
    25. ),
    26. home: AppRoute.global.buildPage(RoutePath.test, {'msg': 'world'}),
    27. )
    28. );
    29. }
    30. }

    pages/test/view.dart

    1. import 'package:fish_redux/fish_redux.dart' hide Store;
    2. import 'package:flutter/material.dart';
    3. import 'package:flutter_app_bootstrapper/api/Api.dart';
    4. import 'package:flutter_app_bootstrapper/api/HttpUtil.dart';
    5. import 'package:flutter_redux/flutter_redux.dart';
    6. import 'package:redux/redux.dart';
    7. import 'package:flutter_app_bootstrapper/stores/app_store.dart' as AppStore;
    8. import 'state.dart';
    9. import 'action.dart';
    10. Widget buildView(TestState state, Dispatch dispatch, ViewService viewService) {
    11. return Scaffold(
    12. appBar: AppBar(title: Text("Hello ${state.msg}")),
    13. body: Column(
    14. children: <Widget>[
    15. Center(
    16. child: state.isLoading ? Text('Loading') : Text("Hello ${state.msg}"),
    17. ),
    18. FlatButton(
    19. child: Text('click me'),
    20. onPressed: () {
    21. bool loading = !state.isLoading;
    22. dispatch(TestActionCreator.changeLoading(loading));
    23. },
    24. ),
    25. // 以下, 混用全局状态管理 flutter_redux
    26. new StoreConnector<Map<String, dynamic>, String>(
    27. converter: (store) => store.state['num'].toString(),
    28. builder: (context, count) {
    29. return new Text(
    30. count,
    31. style: Theme.of(context).textTheme.display1,
    32. );
    33. },
    34. ),
    35. new StoreConnector<Map<String, dynamic>, Store>(
    36. converter: (store) => store,
    37. builder: (context, store) {
    38. return Row(
    39. mainAxisAlignment: MainAxisAlignment.center,
    40. children: <Widget>[
    41. FlatButton(
    42. onPressed: () => store.dispatch(AppStore.Actions.Increment),
    43. child: new Icon(Icons.plus_one),
    44. ),
    45. FlatButton(
    46. onPressed: () => store.dispatch(AppStore.Actions.Decrease),
    47. child: new Icon(Icons.exposure_neg_1),
    48. )
    49. ],
    50. );
    51. },
    52. ),
    53. ],
    54. ),
    55. );
    56. }