Home > database >  How to navigate to another tab from TabBarView page flutter
How to navigate to another tab from TabBarView page flutter

Time:06-26

I have a page HomePageScreen() and it has a TabBarView with four different pages.

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.

I am trying to navigate from inside the PageOneScreen() to tab 2 if a certain value in savedPrefrence = 2.

I have tried DefaultTabController.of(context).animateTo(1) but it throws an error The method 'animateTo' was called on null. and I think it's because I don't have any tabcontroller reference in PageOneScreen().

How can I deal with this?

CodePudding user response:

As you have defined a TabController, use it do switch between tabs like this:

_tabController.index = 2;

Considering that you want to navigate from inside PageOneScreen(), instantiate it with the controller, so it has access to it:

PageOneScreen(_tabController)

  • Related