I want to navigate to different Routes using a Drawer, though I do not want to open a new instance of a Route each time I tap on it if I am already on that Route, rather I would
for me i'm using the laziest easiest way since no answer here helped me in main route, i just use drawer class and pass the name of the current route to the drawer constructor like this :
class MyDrawer extends StatelessWidget {
final String currentRoute;
MyDrawer(this.currentRoute);
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
children: [
InkWell(
child: Text('secondRoute'),
onTap: () {
if (currentRoute != 'secondRoute')
Navigator.push(context,
MaterialPageRoute(builder: (context) => secondRoute()));
}),
InkWell(
child: Text('thirdRoute'),
onTap: () {
if (currentRoute != 'thirdRoute')
Navigator.push(context,
MaterialPageRoute(builder: (context) => thirdRoute()));
}),
and in the routes where i call MyDrawer class i pass the name of the current route
drawer: MyDrawer('secondRoute'),