Skip to content

Instantly share code, notes, and snippets.

@Christopher2K
Last active May 8, 2022 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Christopher2K/cdbeb435136f0ccc3c1025198f59644c to your computer and use it in GitHub Desktop.
Save Christopher2K/cdbeb435136f0ccc3c1025198f59644c to your computer and use it in GitHub Desktop.
[Programmatically open drawer from scaffold directly] How to open scaffold's drawer with code #flutter #dart
class Widget extends StatelessWidget {
final GlobalKey<ScaffoldState> _drawerKey = GlobalKey();
void _openDrawer () {
_drawerKey.currentState.openDrawer();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _drawerKey,
drawer: Drawer(),
body: Center(
child: RaisedButton(
child: Text('Open Drawer'),
onPressed: _openDrawer,
),
),
);
}
}
@MustansarBillah
Copy link

how to open it from a different class?

@Christopher2K
Copy link
Author

Hi, if you means a widget you can use:
Scaffold.of(context).openDrawer(). You obviously need the context.

From a random class, I'd recommend passing the GlobalKey()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment