I am trying to change the icon shape for each item of the following custom navigationBr based on weather they are selected or not:
class HomeScreen extends StatefulWidget {
static const routeName = '/home';
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: currentIndex,
children: screens,
),
bottomNavigationBar: ConvexAppBar(
backgroundColor: Theme.of(context).secondaryHeaderColor,
onTap: (index) {
setState() {
currentIndex = index;
}
},
style: TabStyle.fixed,
items: [
TabItem(
icon: currentIndex == 0
? Icons.local_fire_department
: Icons.local_fire_department_outlined,
title: 'Contests',
),
TabItem(
icon: currentIndex == 1
? Icons.remove_red_eye
: Icons.remove_red_eye_outlined,
title: 'Reviews',
),
TabItem(
icon: currentIndex == 2
? Icons.health_and_safety
: Icons.health_and_safety_outlined,
title: 'Add New',
),
]),
);
}
}
But it doesn’t work. How can I create such a behavior that when user chooses one of the nav bar options it’s icon turns on and when the user chooses another item the icon changes to outlined icon.
2
Answers
dev
I have fixed your problem actually the problem is that the state is not updating there. you have to use
onTabNotify
instead ofonTap
which will sort your issue.I have also done 1 thing is that instead of updating the whole code I used stream so the only required widget will update. and for that, i have used the rxdart: ^0.27.7 version. please check the following code snippet.
Please let me know if you still face any issue with this code.
Your code created local function
setState
instead of using existingsetState
Your code looks create new local function
So that update is not happening here.