flutter_redux的入门级使用
dependencies:
flutter_redux: ^0.5.3
Redux
class CountState {
int _count = 0;
get count => _count;
String _username = '';
get username => _username ;
CountState.initState() {
this._count = 0;
this._username = '';
}
CountState(this._count,this._username);
}
enum AllActions { Increment, SetUserName }
CountState counterReducer(CountState state, dynamic action) {
var actionsType = action[0];
var actionArgments = action[1];
switch (actionsType) {
case AllActions.Increment:
state._count += 100;
state._username = actionArgments;
return state;
}
return state;
}
Widget代码部分
floatingActionButton: new StoreConnector<CountState, VoidCallback>(
converter: (store) {
return () => store.dispatch([AllActions.Increment,'张三']);
},
builder: (context, callback) {
return new FloatingActionButton(
onPressed: callback,
tooltip: 'Increment',
child: new Icon(Icons.add),
);
},
),
读取store里面状态
StoreConnector<CountState, String>(
converter: (store) => store.state.count.toString(),
builder: (context, count) {
return Text(count);
},
),
StoreConnector<CountState, String>(
converter: (store) => store.state.username.toString(),
builder: (context, username) {
return Text(username);
},
),
你需要在main函数里面传递Store
void main () {
final store = new Store<CountState>(counterReducer, initialState: CountState.initState());
runApp(new MyApp(
title: 'Flutter Redux Demo',
store: store,
));
}