I have something like this
int _selectedIndex = 0;
final List<Widget> _screens = const [
TopNavigator(),
SearchScreen(),
SettingsScreen(),
SelectedAlbumScreen()
];
void setSelectedScreen(int index) {
setState(() {
_selectedIndex = index;
});
}
BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.black,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(
icon: Icon(Icons.search), label: 'Search'),
BottomNavigationBarItem(
icon: Icon(Icons.settings), label: 'Settings'),
],
currentIndex: _selectedIndex,
selectedItemColor: Theme.of(context).primaryColor,
unselectedItemColor: Colors.grey,
onTap: (int index) {
setSelectedScreen(index);
},
),
As you can see, I have 4 screens in my list, but only 3 declared in my BottomNavigationBar, so I need to have those 4 screens available to go to them but the number 4 must be hidden for the user so that it can only be accessed by tapping on some button.
When I switch to the screen via code, I get an assertion error.
GestureDetector(
onTap: () {
context.read<HandleBottomNavigationIndex>().setSelectedScreen(3);
}
Failed assertion: line 251 pos 15: '0 <= currentIndex && currentIndex < items.length': is not true.
2
Answers
It seems that by the very nature of the BottomNavigationBar it is impossible to have a number of screens different from the number of items in the BottomNavigationBar.
but I was able to solve it just by creating my own BottomNavigationBar.
here is the code:
item:
BottomNavigatonBar:
implementation:
Try below code…
And in your build method
Change showItem true/false as your requirement in setState