I have a bottom navigation bar widget for my Flutter app. On tapping specific item it is navigating to another screen. However, I don't know how to update current index in this case so that selected tab gets highlighted. Here is my code:
BottomNavigationBar(
backgroundColor: const Color(0xffFF7B19),
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.calendar_today_rounded,
size: lh / 25,
),
label: 'Events',
),
BottomNavigationBarItem(
icon: Icon(
Icons.people_alt_outlined,
size: lh / 25,
),
label: 'Councils',
),
],
currentIndex: 0,
selectedItemColor: Colors.grey[900],
unselectedItemColor: Colors.grey[50],
onTap: (int index) {
if (index == 0) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => AllEventsScreen()));
} else {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Councils()));
}
})
I have tried creating a variable _selectedIndex
for this which I was updating inside the function I have provided for onTap as shown:
currentIndex: _selectedIndex,
selectedItemColor: Colors.grey[900],
unselectedItemColor: Colors.grey[50],
onTap: (int index) {
if (index == 0) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => AllEventsScreen()));
_selectedIndex = 0;
} else {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Councils()));
_selectedIndex = 1;
}
But this doesn't seems to work. I couldn't figure out how to do this.