I've created a bottom navigation bar and everytime I use it in different pages its state is back to first item. What am I missing? Thank you.
There are 5 routes defined in a list (and in the main.dart file), so everything is working good except the state.
I've tried with AutomaticKeepAliveClientMixin but I'm not sure that I've used it correctly (it's in the code below).
Here is the code:
class MyBottomNavigationBar extends StatefulWidget {
MyBottomNavigationBar({Key key}) : super(key: key);
@override
MyBottomNavigationBarState createState() => MyBottomNavigationBarState();
}
class MyBottomNavigationBarState extends State<MyBottomNavigationBar>
with AutomaticKeepAliveClientMixin {
int bottomTabIndex = 0;
var routes = ['/', '/', '/my_categories', '/my_people_list', '/my_chat'];
@override
bool get wantKeepAlive => true;
@override
void onTabTapped(int index) {
setState(() {
bottomTabIndex = index;
Navigator.pushNamed(context, routes.elementAt(bottomTabIndex));
});
}
@override
Widget build(BuildContext context) {
theme:
MyFirstTheme().theme;
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
//
selectedLabelStyle: TextStyle(fontWeight: FontWeight.w700),
unselectedLabelStyle: TextStyle(fontWeight: FontWeight.w700),
iconSize: 24,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text(
'HOME',
),
),
BottomNavigationBarItem(
icon: Icon(Icons.insert_chart),
title: Text(
'STATS',
),
),
BottomNavigationBarItem(
icon: Icon(Icons.view_list),
title: Text(
'INVENTORY',
),
),
BottomNavigationBarItem(
icon: Icon(Icons.group),
title: Text(
'PEOPLE',
),
),
BottomNavigationBarItem(
icon: Icon(Icons.forum),
title: Text(
'CHAT',
),
),
],
currentIndex: bottomTabIndex,
onTap: onTabTapped,
);
}
}
