欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > fish-redux 全局Store的应用

fish-redux 全局Store的应用

2025/2/21 3:18:30 来源:https://blog.csdn.net/allanGold/article/details/144208538  浏览:    关键词:fish-redux 全局Store的应用
Widget createApp() {final AbstractRoutes routes = PageRoutes(pages: <String, Page<Object, dynamic>>{/// 注册TodoList主页面'todo_list': ToDoListPage(),/// 注册Todo编辑页面'todo_edit': TodoEditPage(),},visitor: (String path, Page<Object, dynamic> page) {/// 只有特定的范围的 Page 才需要建立和 AppStore 的连接关系/// 满足 Page<T> ,T 是 GlobalBaseState 的子类if (page.isTypeof<GlobalBaseState>()) {/// 建立 AppStore 驱动 PageStore 的单向数据连接/// 1. 参数1 AppStore/// 2. 参数2 当 AppStore.state 变化时, PageStore.state 该如何变化page.connectExtraStore<GlobalState>(GlobalStore.store,(Object pagestate, GlobalState appState) {final GlobalBaseState p = pagestate;if (p.themeColor != appState.themeColor) {if (pagestate is Cloneable) {final Object copy = pagestate.clone();final GlobalBaseState newState = copy;newState.themeColor = appState.themeColor;return newState;}}return pagestate;});}},);return MaterialApp(title: 'Fluro',debugShowCheckedModeBanner: false,theme: ThemeData(primarySwatch: Colors.blue,),home: routes.buildPage('todo_list', null),onGenerateRoute: (RouteSettings settings) {return MaterialPageRoute<Object>(builder: (BuildContext context) {return routes.buildPage(settings.name, settings.arguments);});},);
}

需要关注两个点:

1、一个if判断page.isTypeof<GlobalBaseState>()
       

2、一个建立全局State与页面State之间联系的方法

        /// 建立 AppStore 驱动 PageStore 的单向数据连接
        /// 1. 参数1 AppStore
        /// 2. 参数2 当 AppStore.state 变化时, PageStore.state 该如何变化
        page.connectExtraStore<GlobalState>(xx, xx)

void connectExtraStore<K>(

Store<K> extraStore,

/// To solve Reducer<Object> is neither a subtype nor a supertype of Reducer<T> issue.

Object Function(Object?, K?) update,

)

fish-redux的example中演示的是修改主题色

https://github.com/alibaba/fish-redux/blob/master/example/lib/app.dart#L50

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词