In my flutter app, selectedItemcolor doesnot change when i try to navigate to another screen with Navigator.push( context, MaterialPageRoute(builder: (context) => _pages.elementAt(index)), );
here is the full code
import 'package:flutter/material.dart';
class BottomBar extends StatefulWidget {
const BottomBar({Key? key}) : super(key: key);
@override
_BottomBarState createState() => _BottomBarState();
}
class _BottomBarState extends State<BottomBar> {
int _SelectedIndex = 0;
final List<Widget> _pages = <Widget>[
Home(),
Order(),
Profile(),
];
void onTabTapped(int index) {
setState(() {
_SelectedIndex = index;
});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => _pages.elementAt(index)),
);
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "Home",
),
BottomNavigationBarItem(
icon: Icon(Icons.card_travel),
label: "Orders",
),
BottomNavigationBarItem(icon: Icon(Icons.person), label: "Profile"),
],
currentIndex: _SelectedIndex,
onTap: onTabTapped,
);
}
}
You shouldn't use the navigator to change the active page.
BottomNavigationBaris taking care for the navigation. Please check the following example: