alibaba / fish-redux

An assembled flutter application framework.
https://github.com/alibaba/fish-redux
Apache License 2.0
7.33k stars 843 forks source link

发现在一个page中的pageview里面放入其他的page,子page的initstate不会实例化 #686

Closed beydeng closed 4 years ago

beydeng commented 4 years ago

如我在indexpage里面 void _init(Action action, Context ctx) async { dynamic pageList = [ keepAliveWrapper( ctx.buildComponent('homePage'), ), ... } 实例化pageList,里面放如多个page,如上的homePage 然后使用在indexpage中 child: PageView( scrollDirection: Axis.horizontal, children: state.pageList, controller: state.scrollController, physics: NeverScrollableScrollPhysics(), ), 虽然这样可以加载page显示UI,如homePage可以正常显示,

class HomePage extends Page<HomeState, Map<String, dynamic>> { HomePage() : super( initState: initState, effect: buildEffect(), reducer: buildReducer(), view: buildView, dependencies: Dependencies( adapter: null, slots: <String, Dependent>{ 'topBar': TopBarConnector() + TopBarComponent(), 'sectionBar': SectionBarConnector() + SectionBarComponent() }), middleware: <Middleware>[ ],);

}

但是 在homePage里面的initState方法里面,发现如下initState方法并不会走,也就是这样的话HomePage的state无法实例化。请教下原因是什么?如何解决这个问题。

HomeState initState(Map<String, dynamic> args) {

print('initState has runing');

return HomeState() ..isBackIconShow = false ..themeColor = Colors.blue ..rightTitle = '' ..categoryDatas = List();

}

beydeng commented 4 years ago

indexPage中如下 class IndexPage extends Page<IndexState, Map<String, dynamic>> { IndexPage() : super( initState: initState, effect: buildEffect(), reducer: buildReducer(), view: buildView, dependencies: Dependencies( adapter: null, slots: <String, Dependent>{ 'homePage' : HomeConnector() + HomePage(), 'mine' : MineConnector() + MinePage() }), middleware: <Middleware>[ ],);

}

beydeng commented 4 years ago

是因为 HomeConnector 连接器链接之后,只能在indexpage里面去操作homepage的state么?

beydeng commented 4 years ago

经过几天摸索,研究出来了问题。主要就是连接器不是很明白导致的问题。这里给有相同问题的同性朋友说下问题所在。 只要把状态state交给IndexState来管理的,然后在IndexState中的连接器中进行处理,就可以解决这个问题。 class HomeConnector extends ConnOp<IndexState, HomeState> { @override HomeState get(IndexState state) { if(state.homeState == null){ state.homeState = HomeState() ..recommandDatas=[] ..categoryDatas=[] ..goodsList = []; }

return state.homeState;

}

@override List factors(IndexState state) { return []; }

@override
void set(IndexState state, HomeState subState) {

  state.homeState = subState;
}

}

ArmedBone commented 4 years ago

@beydeng 牛批。兄弟,你是怎么摸索出来的。