简体   繁体   English

从 MaterialPageRoute 导航到另一个选项卡

[英]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.

相关问题 如何从 Flutter 中的通知导航到应用程序中的特定 MaterialPageRoute - How to navigate to specific MaterialPageRoute in app from a notification in Flutter 如何在 TabBarView 中通过 MaterialPageRoute 导航? - How to Navigate via MaterialPageRoute in a TabBarView? 如何从 TabBarView 页面抖动导航到另一个选项卡 - How to navigate to another tab from TabBarView page flutter 如何通过同一 TabBarView 父级的另一个选项卡的 onTap() 回调导航到另一个选项卡? - How to navigate to another Tab from via onTap() callback of another Tab of the same TabBarView parent? 我想导航到另一个页面并使用 Firestore 数据进行解析,但是当我使用 MaterialPageRoute 时该怎么做? - I want to Navigate to another page and parse with firestore data but how do I do that when I am using MaterialPageRoute? Flutter MaterialPageRoute 不在原生 iOS navigatorViewController 上导航 - Flutter MaterialPageRoute doesn't navigate over native iOS navigatorViewController Flutter-在另一个小部件中将 MaterialPageRoute 作为参数传递 - Flutter- pass MaterialPageRoute as parameter in another widget 如何导航到 flutter 堆栈中的另一个页面? - How to navigate to another page within a stack in flutter? Flutter:使用 MaterialPageRoute 从无状态小部件到有状态小部件 - Flutter: From Stateless Widget to Stateful Widget with MaterialPageRoute 从 ListTile 导航到另一个页面 - Navigate to another page from ListTile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM