The Wayback Machine - https://web.archive.org/web/20201023040803/https://github.com/alibaba/fish-redux/issues/549
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对于pageView中的子页面,如何优雅的连接全局的状态? #549

Open
a616781689 opened this issue Nov 28, 2019 · 6 comments
Open

Comments

@a616781689
Copy link

@a616781689 a616781689 commented Nov 28, 2019

看了fish-redux的todoList demo,发现里面连接全局状态的方法是使用visitor

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;
        });
      }

但是pageview的子页面是由 AccountPage().buildPage(null)这种方式生成的,并不会触发visitor的回调。

Widget pageViewItemBuilder(BuildContext context, int index) {
  if (index == 0) {
    return AccountPage().buildPage(null);
  } else if (index == 1) {
    return TranscationPage().buildPage(null);
  } else if (index == 2) {
    return PersonPage().buildPage(null);
  }
  return Text('页面跳转错误');

}

为了解决这个问题,我在page的构造方法中连接了全局状态。

    this.connectExtraStore<GlobalState>(GlobalStore.store,
        (Object pageState, GlobalState appState) {
      debugPrint('克隆该页面');

      if (pageState is Cloneable) {
        final Object copy = pageState.clone();
        final GlobalAccountBaseState newState = copy;
        newState.totalAssets = appState.totalAssets;
        newState.netAssets = appState.netAssets;
        newState.debt = appState.debt;
        return newState;
      }
      return pageState;
    });

请问是否有更加好的解决办法

@xiongjianchang
Copy link

@xiongjianchang xiongjianchang commented Jan 4, 2020

遇到同样问题

@zjuwjf
Copy link
Contributor

@zjuwjf zjuwjf commented Jan 16, 2020

目前没有。
是否是希望这种 connector 关系 可以通过构造函数进行配置?

@a616781689
Copy link
Author

@a616781689 a616781689 commented Jan 16, 2020

目前没有。
是否是希望这种 connector 关系 可以通过构造函数进行配置?

是的,这是一个好方法

@xinpiaoyuanfang
Copy link

@xinpiaoyuanfang xinpiaoyuanfang commented Mar 12, 2020

目前没有。
是否是希望这种 connector 关系 可以通过构造函数进行配置?

遇到同样的问题

@SurfeeS
Copy link

@SurfeeS SurfeeS commented Apr 15, 2020

遇到同样问题

@didiao11300
Copy link

@didiao11300 didiao11300 commented Apr 25, 2020

把main里面的router弄成全局变量 使用routers.buildPage("test",null);通过routers构建widget,就可以刷新全局状态

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.