I'm using ListTile to display items on a navigation drawer but I'm having a hard time creating the isSelected Color and have applied it to the onTap using this but keep getting errors about the index and the app can't recognise the isSelected bool on the onTap:
for(int i = 0; i < isSelected.length; i++){
setState(() {
if (index == i) {
isSelected[index] = true;
} else { //the condition to change the highlighted item
isSelected[i] = false;
}
});
Here is the Navigation Drawer in Flutter that I have created, if you could give advice about how to take the 'isSelected' in the _createDrawerItem Widget and apply it to the drawer. I have already set 'isSelected' too true and false to the drawer items but can't seem to apply it to the onTap.
class NavDraw extends StatefulWidget {
@override
_NavDrawState createState() => _NavDrawState();
}
class _NavDrawState extends State<NavDraw> {
@override
void initState() {
super.initState();
}
Widget build(BuildContext context) {
return Drawer(
child: Container(
child: ListView(
children: <Widget> [
Container(
height: 100.0,
color: Color(0xff2B4DB9),
child: DrawerHeader(
margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
padding: EdgeInsets.zero,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.none,
image: AssetImage('images/drawer_header.png')))),
),
_createDrawerItem(
icon: Icons.timer,
text: 'Timer',
isSelected: true,
onTap: () =>
Navigator.pushReplacementNamed(context, Routes.home)),
_createDrawerItem(
icon: Icons.history,
text: 'History',
isSelected: false,
onTap: () =>
Navigator.pushReplacementNamed(context, Routes.history)),
_createDrawerItem(
icon: Icons.help,
text: 'Help & Support',
isSelected: false,
onTap: () =>
Navigator.pushReplacementNamed(context, Routes.help_and_support)),
_createDrawerItem(
icon: Icons.rate_review,
text: 'Write a Review',
isSelected: false,
onTap: () =>
Navigator.pushReplacementNamed(context, Routes.write_a_review)),
Divider(),
],
),
),
);
}
}
Widget _createDrawerItem(
{IconData icon, String text, GestureTapCallback onTap, bool isSelected}) {
return Ink(
color: isSelected ? Color(0xffE3EAFF) : Colors.transparent,
child: ListTile(
selected: true,
hoverColor: Colors.white,
title: Row(
children: <Widget>[
Icon(icon),
Padding(
padding: EdgeInsets.only(left: 8.0),
child: Text(text),
)
],
),
onTap: onTap,
),
);
}
