**Hello guys, I need to isolate the drawer so that I can use it on any screen and on any button in my application, how can I do that?
This is the default drawer example…….
How can I use an IconButton or an ElevatedButton for example that are on another screen?
**
class NavigationDrawerDemo extends StatefulWidget {
const NavigationDrawerDemo({Key? key}) : super(key: key);
@override
State<NavigationDrawerDemo> createState() => _NavigationDrawerDemoState();
}
class _NavigationDrawerDemoState extends State<NavigationDrawerDemo> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text(
'Navigation Drawer',
),
backgroundColor: const Color(0xff764abc),
),
endDrawer: Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: [
DrawerHeader(
child: Text("Header"),
),
ListTile(
leading: Icon(
Icons.home,
),
title: const Text('Page 1'),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
leading: Icon(
Icons.train,
),
title: const Text('Page 2'),
onTap: () {
Navigator.pop(context);
},
),
],
),
),
);
}
}
2
Answers
In other screens you can open it or close it by calling
and
I don’t get why would you would try to open the drawer from another screen when is not showing it. For your example to work just made few changes:
I understand that this is what you’re lookin for:
Modified code: