import 'package:flutter/material.dart';
import './screens/recents_screen.dart';
import './screens/favourites_screen.dart';
class MainDrawer extends StatefulWidget {
const MainDrawer({Key? key}) : super(key: key);
@override
State<MainDrawer> createState() => _MainDrawerState();
}
class _MainDrawerState extends State<MainDrawer> {
var texts = [
{
'title': 'Home',
'path': '/',
},
{
'title': 'Favourite',
'path': FavouritesScreen.routeName,
},
{
'title': 'Recent Search',
'path': RecentsScreen.routeName,
},
];
int _currentSelected = 0;
@override
Widget build(BuildContext context) {
return Drawer(
child: Padding(
padding: const EdgeInsets.only(top: 30, left: 20),
child: Column(children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: texts.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
texts[index]['title'] as String,
style: TextStyle(
fontSize: 16,
letterSpacing: 0.5,
height: 0.36,
color: _currentSelected == index
? Colors.black
: Colors.grey,
fontFamily: 'Roboto Regular'),
),
onTap: () {
setState(() {
_currentSelected = index;
});
Navigator.of(context)
.pushReplacementNamed(texts[index]['path'] as String);
},
);
},
),
),
]),
),
);
}
}
I want to set the active drawer text color to black and inactive screens to grey.
When I open the drawer the text colour of 'Home' is black and all other screen titles are grey always. It works fine when tapped, that is the tapped screen title is set to black and others to grey. But when opened a drawer it always shows Home screen as active. How to correct it?