1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 主要步骤如下:
1、定义 State 类: class AppState { final int counter; AppState({required this.counter}); }
2、定义 Action: enum Actions { Increment, Decrement }
3、编写 Reducer: AppState counterReducer(AppState state, dynamic action) { if (action == Actions.Increment) { return AppState(counter: state.counter + 1); } return state; }
4、创建 Store: final store = Store<AppState>(counterReducer, initialState: AppState(counter: 0));
5、绑定到 Widget: StoreProvider( store: store, child: MyApp(), );
6、使用 StoreConnector: StoreConnector<AppState, VoidCallback>( converter: (store) => () => store.dispatch(Actions.Increment), builder: (context, callback) => ElevatedButton( onPressed: callback, child: Text("增加"), ), );
|