[英]How to navigate to another tab from TabBarView page flutter
I have a page HomePageScreen() and it has a TabBarView with four different pages.我有一个页面 HomePageScreen(),它有一个 TabBarView 有四个不同的页面。
TabBarView(
physics: NeverScrollableScrollPhysics(),
controller: _tabController,
children: <Widget>[
PageOneScreen(),
PageTwoScreen(),
PageThreeScreen(context),
PageFourScreen(),
],
),
By default, the PageOneScreen() page shows in the HomePageScreen() and I can navigate to the other tabs quite fine.默认情况下,PageOneScreen() 页面显示在 HomePageScreen() 中,我可以很好地导航到其他选项卡。
I am trying to Navigator.popUntil(context, (route) => route.isFirst)
from inside a different route and when it gets to PageOneScreen(), I want to navigate to tab 2 if a certain value in savedPrefrence = 2.我正在尝试从不同的路线内部导航到Navigator.popUntil(context, (route) => route.isFirst)
,当它到达 PageOneScreen() 时,如果 savedPrefrence = 2 中的某个值,我想导航到选项卡 2。
I have tried DefaultTabController.of(context).animateTo(1)
on PageOneScreen() but it throws an error The method 'animateTo' was called on null.
我已经在 PageOneScreen() 上尝试了DefaultTabController.of(context).animateTo(1)
但它抛出了一个错误The method 'animateTo' was called on null.
and I think it's because I don't have any tabcontroller reference in PageOneScreen().我认为这是因为我在 PageOneScreen() 中没有任何 tabcontroller 引用。
How can I deal with this?我该如何处理?
As you have defined a TabController
, use it do switch between tabs like this:当您定义了TabController
时,请使用它在选项卡之间切换,如下所示:
_tabController.index = 2
; _tabController.index = 2
;
Considering that you want to navigate from inside PageOneScreen()
, instantiate it with the controller, so it has access to it:考虑到您想从PageOneScreen()
内部导航,请使用控制器对其进行实例化,以便它可以访问它:
PageOneScreen(_tabController)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.