I have created a bottom navigation bar and a drawer in my flutter app. I managed to show the same screen when user clicks on a bottom navigation bar item or a drawer item which both have the same purpose or target screen. But I notice that when user clicks on an item in the drawer, the bottom navigation bar item targeting the same screen is not getting active. Hope somebody understands what I'm talking about. Now I wanna know what would be the way, even the code to achieve that behavior in flutter
Here are the codes which i want to enhance
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> screens = <Widget>[
Screen1(),
Screen2(),
Screen3()
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: screens.elementAt(_selectedIndex),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
drawer: Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text('Drawer Header'),
),
ListTile(
title: const Text('Item 1'),
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => Screen1());
},
),
ListTile(
title: const Text('Item 2'),
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => Screen2());
},
),
ListTile(
title: const Text('Item 3'),
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => Screen3());
},
),
],
),
),
);
}
}```