I would like to ask how can i change the color of my icon when I navigate to the next page that means when I select the page the icon is blue then the unselected icon will remain grey. I have tried setState()
but it’s not working for me.
class _CustomBottomNavigationState extends State<CustomBottomNavigation> {
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Container(
width: size.width,
height: 80,
child: Stack(
children: [
CustomPaint(
size: Size(size.width, 80),
painter: BNBCustomPainter(),
),
Center(
heightFactor: 0.6,
child: FloatingActionButton(
onPressed: () {
widget._addingThings(context);
},
backgroundColor: Colors.black,
child: Icon(Icons.add),
elevation:0.1,
),
),
Container(
width: size.width,
height: 80,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(icon: Icon(Icons.home), onPressed: () {
Navigator.push(context, PageTransition(type: PageTransitionType.rightToLeft, child: MyScreen(), isIos: true));
}),
IconButton(icon: Icon(Icons.notifications), onPressed: () {
Navigator.push(context, PageTransition(type: PageTransitionType.leftToRight, child: My2Screen()));
}),
Container(width: size.width * 0.2),
IconButton(icon: Icon(Icons.add), onPressed: () {
_pickImageFromCamera();
Navigator.push(context, MaterialPageRoute(builder: (context) => My3Screen()));
}),
IconButton(icon: Icon(Icons.abc), onPressed: () {
Navigator.push(context, PageTransition(type: PageTransitionType.leftToRight, child: My4Screen()));
}),
],
),
)
],
),
);
}
}
2
Answers
You can create a custom widget for your icon buttons and maintain the selected/unselected state internally within that widget.
Then you can use it like this:
create variable selectedIndex