[英]Navigate to another tab from within MaterialPageRoute
I expected this issue to have a simple solution but I didn't find yet any...我希望这个问题有一个简单的解决方案,但我还没有找到任何...
I have few tabs in my app, in one of them I open another screen using我的应用程序中有几个选项卡,其中一个我使用打开另一个屏幕
Navigator.push(context, MaterialPageRoute(...
Once user clicks on a button in that screen I want to pop it and navigate to another tab.一旦用户单击该屏幕中的按钮,我想弹出它并导航到另一个选项卡。
I tried to pass TabController to the relevant tab and its child screen, but this doesn't seem like the simplest solution, and also not easy to accomplish since the controller is not yet defined:我尝试将 TabController 传递给相关选项卡及其子屏幕,但这似乎不是最简单的解决方案,而且由于 controller 尚未定义,因此也不容易完成:
tabController = DefaultTabController(
body: TabBarView(
children: [
FirstTab(
tabController: tabController // <- tabController is not defined yet at this point:(
Is there any "global" function to reset the app's "entire" route so it will both pop MaterialPageRoute and navigate to specific tab?是否有任何“全局” function 来重置应用程序的“整个”路由,以便它会弹出 MaterialPageRoute并导航到特定选项卡?
The solution I found is to call Navigator's push synchronously and check for its returned value.我找到的解决方案是同步调用 Navigator 的 push 并检查其返回值。 Then when I want to navigate to another tab I simply send true indication in Navigator's pop.
然后,当我想导航到另一个选项卡时,我只需在导航器的弹出窗口中发送真实指示。
This is how my navigation method looks like, notice I had to add a short delay before navigating to another tab, not sure why, but it didn't work without it:这就是我的导航方法的样子,请注意我必须在导航到另一个选项卡之前添加一个短暂的延迟,不知道为什么,但没有它它就无法工作:
_navigateToDetailsScreen() async {
bool shouldNavigateToHomeTab = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailsScreen()),
));
if (shouldNavigateToHomeTab) {
Future.delayed(const Duration(milliseconds: 500), () {
DefaultTabController.of(context)!.animateTo(0);
});
}
}
And this is how I call pop:这就是我所说的流行音乐:
Navigator.of(context).pop(true);
This looks like the simplest solution for me, and so far I didn't find any issues with it.这对我来说似乎是最简单的解决方案,到目前为止我没有发现任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.