I have been working with BottomNavigationBar in the flutter, but I am not able to select a Tab programmatically outside of onTap callback of BottomNavigationBar.
The code with onTap callback, which is working:
return new BottomNavigationBar(
items: <BottomNavigationBarItem>[
_bottomNavigationItem(Icons.account_circle, DrawerTitles.CONTACTS),
_bottomNavigationItem(Icons.delete, DrawerTitles.DELETED_CONTACTS),
_bottomNavigationItem(Icons.list, DrawerTitles.LOGS),
],
onTap: (int index) {
setState(() {
navigationIndex = index;
switch (navigationIndex) {
case 0:
handleBottomNavigationBarClicks(DrawerTitles.CONTACTS);
break;
case 1:
handleBottomNavigationBarClicks(DrawerTitles.DELETED_CONTACTS);
break;
case 2:
handleBottomNavigationBarClicks(DrawerTitles.LOGS);
break;
}
});
},
currentIndex: navigationIndex,
fixedColor: Colors.blue[400],
type: BottomNavigationBarType.fixed,
);
But I want to change the tabs outside of onTap callback.
I have tried changing the index used by BottomNavigatioBar outside of onTap callBack, but it didn't work.
Here is what I have tried:
void changeTabs(int tabIndex) {
setState(() {
navigationIndex = tabIndex;
});}
Here is a gist for the code.
Is there any way available to change Tabs?
Here is a complete example on how to achieve what you want.
whenever you want to change to a tab, call the
changeTab(YOUR_TAB_INDEX)
You can grab this BottomAppBar widget by using a GlobalKey. By this GlobalKey you can handle this widget. Here is an gist for the code
Here you assign a GlobalKey
And put that key in your BottomNavigationBar
Now you can call widget's method and use CurvedNavigationBar instead of BottomNavigationBar if you have to work with CurvedNavigationBar.