Last active
May 8, 2022 17:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
), | |
), | |
); | |
} | |
} |
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
how to open it from a different class?