Flutter Navigation Bar - Change tab from another page Flutter Navigation Bar - Change tab from another page dart dart

Flutter Navigation Bar - Change tab from another page


You have to change a TabControlller like this

1* Create TabController instance

TabController _tabController;

2* in initState methode use this

@overridevoid initState() {    super.initState();    _tabController = TabController(vsync: this, length: 3);  }

3* add a Mixin to _HomeState

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {....}

4* assign the TabController to your TabBar

TabBar(      controller: _tabController,      tabs: _yourTabsHere,    ),

5* Pass controller to your Pages

TabBarView(    controller: _tabController,    children:<Widget> [  Page1(tabController:_tabController),  Page2(tabController:_tabController),  Page3(tabController:_tabController),];

6* call tabController.animateTo() from Page1

class Page1 extends StatefulWidget {final TabController tabControllerPage1({this.tabController});....}class _Page1State extends  State<Page1>{....onButtonClick(){  widget._tabController.animateTo(index); //index is the index of the page your are intending to (open. 1 for page2)}}

Hope it helps


in the button :

onPressed: (){     _currentIndex = 1;   //or which value you want     setState((){});}

in the bottom Navigation bar :

currentIndex = _currentIndex