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
2274

被折叠的 条评论
为什么被折叠?



