本教程须要你在官方教程的基础上有概念性的了解,若你对基础概念不了解,请先阅读官方介绍问文档orGithub上的介绍。
Fluter应用框架Fish Redux官方介绍
Fish Redux Github地址git
官方推荐目录样式
sample_page
-- action.dart
-- page.dart
-- view.dart
-- effect.dart
-- reducer.dart
-- state.dart
components
sample_component
-- action.dart
-- component.dart
-- view.dart
-- effect.dart
-- reducer.dart
-- state.dartgithub
Action我把它理解为事件类型声明的声明,先是枚举定义Action type,在对应Action Creator中定义事件方法,方便dispatch调用对应Action事件。
redux
//定义 Action Type enum ItemAction { jumpDetail } //ActionCreator定义方法 class ItemActionCreator { static Action onJumpDetail() { return const Action(ItemAction.jumpDetail); } } 复制代码
View顾名思义就是界面Widget展现,其 函数签名:(T,Dispatch,ViewServices) => Widget 【T为对应state.dart定义的数据,Dispatch能够调用ActionCreator中方法,ViewService能够调用建立Adapter或是Component】markdown
Widget buildView(ItemState state, Dispatch dispatch, ViewService viewService) { return Column( children: <Widget>[ Container( padding: const EdgeInsets.all(10), child: GestureDetector( child: Row(......), onTap: ()=> dispatch(ItemActionCreator.onJumpDetail()), ), ), ], ); } 复制代码
Effect和Reducer都属于对Action事件操做行为,这二者区别在于:Effect是对非数据操做行为(包括State生命周期方法),Reducer是对数据操做行为,简单理解为只要Reducer操做过原来数据就改变了。
Effect的函数签名:(Context,Action) => Object
Reducer的函数签名:(T,Action) => T网络
///Effect 经常使用于定义 网络数据请求,或是界面点击事件等非数据操做 Effect<ItemState> buildEffect() { return combineEffects(<Object, Effect<ItemState>>{ ItemAction.jumpDetail: _onJumpDetail, }); } void _onJumpDetail(Action action, Context<ItemState> ctx) { Navigator.push(ctx.context, MaterialPageRoute(builder: (buildContext) { return DetailPage().buildPage({'item': ctx.state}); })); } //Reducer经常使用语操做数据行为,经过拿到Action payload中数据来对数据经过浅复制方式改变数据 Reducer<DetailState> buildReducer() { return asReducer( <Object, Reducer<DetailState>>{ DetailAction.loadDone: _onLoadDone, ...... }, ); } DetailState _onLoadDone(DetailState state, Action action) { final List<String> list = action.payload ?? <String>[]; final DetailState newState = state.clone(); newState.list = list; return newState; } 复制代码
State为定义Dart本地数据封装类,其要实现Cloneable接口,当中数据改变经过浅复制方式,默认插件会实现initState方法框架
///下文Demo首页列表数据 class ItemState implements Cloneable<ItemState> { String title; String subTitle; ItemState({this.title, this.subTitle}); @override ItemState clone() { return ItemState() ..title = title ..subTitle = subTitle; } } ItemState initState(Map<String, dynamic> args) { return ItemState(); } 复制代码
Component和Page相似都是对局部的展现和功能的封装。
ide
三要素:
View(组件展现),Effect(非修改数据行为),Reducer(修改数据行为)函数
component使用一般用viewService.buildComponent('component name')
工具
class ItemComponent extends Component<ItemState> { ItemComponent() : super( effect: buildEffect(), reducer: buildReducer(), view: buildView, dependencies: Dependencies<ItemState>( adapter: null, slots: <String, Dependent<ItemState>>{ }),); } ///须要Page界面注册 Component dependencies: Dependencies<HomeState>( adapter: ItemAdapter(), slots: <String, Dependent<HomeState>>{ 'header': HeaderConnector() + HeaderComponent(), }), 复制代码
connector理解为子数据与父数据绑定,在注册component以及adapter使用
oop
///Demo 中须要统计 列表数据条数 讲列表数与定义Component定义的state数据绑定在一块儿 class HeaderConnector extends Reselect1<HomeState, HeaderState, int> { @override HeaderState computed(int state) { return HeaderState()..total = state; } @override int getSub0(HomeState state) { return state.list.length; } @override void set(HomeState state, HeaderState subState) {} } 复制代码
///主要用于列表数据改变成ItemBean,提供set,get方法 class _ItemConnector extends ConnOp<HomeState, List<ItemBean>> { @override List<ItemBean> get(HomeState state) { if (state.list.isNotEmpty) { return state.list .map((itemState) => ItemBean('item', itemState)) .toList(); } else { return <ItemBean>[]; } } @override void set(HomeState state, List<ItemBean> items) { if (items.isNotEmpty) { state.list = List<ItemState>.from( items.map((ItemBean bean) => bean.data).toList()); } else { state.list = <ItemState>[]; } } @override subReducer(reducer) { // TODO: implement subReducer return super.subReducer(reducer); } } 复制代码
Fish Redux能够实现数据集中化处理,分治解耦,带来这些便利同时,也会带来必定繁琐行和易错性,因此对于一些界面逻辑简单,或是只是列表展现不建议使用Fish Redux框架模式开发。
目前Fish Redux生命周期管理只限于State生命周期,基于mixins的WidgetsBindingObserver声明周期方法,须要等待后期官方提供开放。