State:耦合太强,大型应用管理混乱
Redux:国内用最多,咸鱼在用(出了fish redux)
Scoped Model:
bloc:比Redux简单,特别单页面
Provide:Google的Github下,刚出不久,Provider被设计为ScopedModel的替代品,比Redux简单。
Redux状态管理
添加依赖:
flutter_redux: ^0.5.3
在根widget中添加不变成员
final Store<int> store;
mainReducer为应用程序的主reducer(函数),接受一个状态,返回一个状态;action标记处理逻辑
在Redux修改状态,实际上是使用Store。dispatch分发action,由reducer对action进行解析,返回新的state
三步走:
1、在原来MaterialApp外套一层StoreProvider
2、子widget为StatelessWidget(因为用redux来管理状态,就不需要stful了)
3、传递状态
用来存储状态
在build中外层包裹StoreProvider,通过StoreConnector把store和MyHomePage页面绑定,converter是改变后的状态,通过StoreConnector把button的点击事件和store状态改变绑定。这样就实现了redux状态改变管理。
管理状态为int
在mainReducer中增加对action的处理:
int mainReducer(int state, dynamic action){
if(Actions.Increase==action){
return state + 1;
}
}