InheritedWidget
的使用,但是当 widget
数量很多时,使用起来会越来越麻烦,所以本文再给大家分享下,flutter_redux
的使用方法
flutter_redux
是基于InheritedWidget
封装的用于Widget
树的数据传递与共享的的一套框架,它能高效的完成数据共享,进而达到ui
及时更新等目的,使用起来略显复杂,一般不是很多的数据更新不建议使用,直接用InheritedWidget
就能解决,当Widget
绑定的很多的时候,使用起来就会很爽了。
redux
数据管理方式来自 react
,React
的数据单向流通的,很多时候需要共享数据的时候就比较麻烦,需要不断地变量提升,也就导致数据上溯会非常的混乱。解决办法之一就是redux
, 进行全局统一的数据管理,并且通过State
的更新驱动虚拟dom
更新,再经过diff
算法结果驱动真实dom
的更新。优势是数据的结构清晰,各个组件之间的数据共享。flutter_redux
的使用拆解为个步骤,他们分别是:
1.封装需要共享的数据
2.封装需要发送的消息(同时也有区分动作的作用)
3.数据修改与分发
4.声明 store
5.接受与更新
6.触发app_state.dart
(这里我是对整个 app
共享所以叫这个名字,大家使用时对于修改命名就行)count
class AppState {
int count;
AppState({
this.count,
});
static AppState initialState() {
return AppState(
count: 0,
);
}
AppState copyWith({count}) {
return AppState(
count: count ?? this.count,
);
}
}
initialState
以及复制方法 copyWith
enum
action.runtimeType
即可class IncrementAction {
final payload;
IncrementAction({this.payload});
}
reducer
方法(在实例化 state
是需要传入)时,就可以这么做区分AppState counterReducer(AppState state, dynamic action) {
switch (action.runtimeType) {
case IncrementAction:
return state.copyWith(count: state.count + action.payload);
}
return state;
}
redux
的所有方案都是通过 store
沟通的,所以我们首先要先将 store
实例化class FlutterReduxApp extends StatelessWidget {
final store = Store<AppState>(counterReducer, initialState: AppState.initialState());
@override
Widget build(BuildContext context) {
···
}
}
store
呢?首先我们需要或 store
中存的对应数据count
,具体实现如下class FlutterReduxApp extends StatelessWidget {
//步骤4 申明一个store,必须传入一个方法进去,其余的参数选填,这里选择初始化一下基础参数
final store = Store<AppState>(counterReducer, initialState: AppState.initialState());
@override
Widget build(BuildContext context) {
return StoreProvider(
store: store, //绑定store
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Title"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("You have pushed the button this many times:"),
//当需要使用到共享数据的时候使用StoreConnector来获取数据
StoreConnector<AppState, String>(builder: (context, value) {
return Text(value, style: Theme.of(context).textTheme.display1);
}, converter: (Store store) {
return store.state.count.toString();
})
],
),
),
),
));
}
}
redux
的方便在于,我们并需要专门设置 setState
方法,因为 redux
已经帮我们内置了floatingActionButton
后让数字 +1class FlutterReduxApp extends StatelessWidget {
final store = Store<AppState>(counterReducer, initialState: AppState.initialState());
@override
Widget build(BuildContext context) {
return StoreProvider(
store: store, //绑定store
child: MaterialApp(
home: Scaffold(
floatingActionButton: StoreConnector<AppState, VoidCallback>(
converter: (Store store) {
return () => store.dispatch(IncrementAction(payload: 1)); //发送数据
},
builder: (BuildContext context, VoidCallback callback) {
return FloatingActionButton(
onPressed: callback, child: Icon(Icons.add));
},
),
),
));
}
}
redux
前我们每次刷新页面,所有控件都需要重绘,导致对内存小号极大:redux
后我们明显发现,只有需要修改的控件才会被刷新void setName() {
var store = StoreProvider.of<AppState>(context);
store.dispatch(xxx())
}
flutter_redux
有效的解决了控件到控件间冗长的参数传递,以及高度藕合带来的繁杂逻辑,极大的方便了我们编码,同时其刷新特性,能够很好的优化 app
内存使用github
:flutter-redux-sample扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有