适用于 Flutter 的状态管理工具很多, 有 ScopedReduxProvider 等, 不过本人最熟悉最常用的还是 Redux, 其他的也不作探究了。

    依赖:

    1. dependencies:
    2. flutter_redux: ^0.5.3

    官方示例:

    1. import 'package:flutter/material.dart';
    2. import 'package:flutter_redux/flutter_redux.dart';
    3. import 'package:redux/redux.dart';
    4. // 枚举 Actions
    5. enum Actions { Increment }
    6. // 创建 reducer
    7. int counterReducer(int state, dynamic action) {
    8. if (action == Actions.Increment) {
    9. return state + 1;
    10. }
    11. return state;
    12. }
    13. void main() {
    14. // 创建store
    15. final store = new Store<int>(counterReducer, initialState: 0);
    16. // 将store传入根Widget
    17. runApp(new FlutterReduxApp(
    18. title: 'Flutter Redux Demo',
    19. store: store,
    20. ));
    21. }
    22. class FlutterReduxApp extends StatelessWidget {
    23. final Store<int> store;
    24. final String title;
    25. FlutterReduxApp({Key key, this.store, this.title}) : super(key: key);
    26. @override
    27. Widget build(BuildContext context) {
    28. // 通过 StoreProvider 将之前的 MaterialApp 或 WidgetsApp 包裹住, 并传入store
    29. return new StoreProvider<int>(
    30. // 在 StoreConnector 中的 converter 可以使用此 Store
    31. store: store,
    32. child: new MaterialApp(
    33. theme: new ThemeData.dark(),
    34. title: title,
    35. home: new Scaffold(
    36. appBar: new AppBar(
    37. title: new Text(title),
    38. ),
    39. body: new Center(
    40. child: new Column(
    41. mainAxisAlignment: MainAxisAlignment.center,
    42. children: [
    43. new Text(
    44. 'You have pushed the button this many times:',
    45. ),
    46. // ① 使用StoreConnector连接到数据源, 并通过converter将源数据转换为string
    47. new StoreConnector<int, String>(
    48. converter: (store) => store.state.toString(),
    49. builder: (context, count) {
    50. return new Text(
    51. count,
    52. style: Theme.of(context).textTheme.display1,
    53. );
    54. },
    55. )
    56. ],
    57. ),
    58. ),
    59. // ② 使用StoreConnector连接到数据源, 并通过converter返回一个回调方法, 传入 builder 的 callback
    60. floatingActionButton: new StoreConnector<int, VoidCallback>(
    61. converter: (store) {
    62. return () => store.dispatch(Actions.Increment);
    63. },
    64. builder: (context, callback) {
    65. return new FloatingActionButton(
    66. // callback 实际调用的是converter返回的 `store.dispatch(Actions.Increment)`
    67. onPressed: callback,
    68. tooltip: 'asdasdasd',
    69. child: new Icon(Icons.add),
    70. );
    71. },
    72. ),
    73. ),
    74. ),
    75. );
    76. }
    77. }

    上面的 Store 是通过 converter 转换过的数据, 其实也可以将 store 直接放出来使用:

    1. // ① 处
    2. new StoreConnector<int, int>(
    3. converter: (store) => store.state,
    4. builder: (context, count) {
    5. return new Text(
    6. count.toString(),
    7. style: Theme.of(context).textTheme.display1,
    8. );
    9. },
    10. )
    11. // ② 处
    12. floatingActionButton: new StoreConnector<int, Store>(
    13. converter: (store) => store,
    14. builder: (context, store) {
    15. return new FloatingActionButton(
    16. onPressed: () {
    17. store.dispatch(Actions.Increment);
    18. },
    19. tooltip: 'asdasdasd',
    20. child: new Icon(Icons.add),
    21. );
    22. },
    23. ),