How to close Scaffold's drawer after an item tap?

Solution 1:

Navigator.pop() will pop the Drawer route off the stack and cause it to close.

Solution 2:

Navigator.of(context).pop() should do what you want :)

https://docs.flutter.io/flutter/widgets/Navigator-class.html https://docs.flutter.io/flutter/material/Drawer-class.html

Solution 3:

Shorthand for closing the drawer and navigating to a new route:

Navigator.popAndPushNamed(context, '/newroute');

Solution 4:

First create a ScaffoldState key

GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

Scaffold(
  key: _scaffoldKey,)

Now you can open and close the drawer using the toggleDrawer() method.

for the left drawer

toggleDrawer() async {
  if (_scaffoldKey.currentState.isDrawerOpen) {
    _scaffoldKey.currentState.openEndDrawer();
  } else {
    _scaffoldKey.currentState.openDrawer();
  }
}

for the right drawer

toggleDrawer() async {
  if (_scaffoldKey.currentState.isDrawerOpen) {
    _scaffoldKey.currentState.openDrawer();
  } else {
    _scaffoldKey.currentState.openEndDrawer();
  }
}

Solution 5:

If you simply want to close the Drawer, you can use any of the following:

Navigator.pop(context);
Navigator.of(context).pop();

And if you want to navigate to a new page, you can use

Navigator.popAndPushNamed(context, "/new_page");

or

Navigator.pop(context);
Navigator.push(context, MaterialPageRoute(builder: (_) => Page2()));