Flutter Scaffold.of(context).openDrawer() doesn't work

scaffold.of() called with a context that does not contain a scaffold.
flutter bottom drawer
flutter custom drawer
flutter close drawer programmatically
scaffold drawer in flutter
flutter drawer width
flutter drawer header
appbar flutter drawer

I want to open a drawer after pushing on the custom button in BottomMenu I have trouble with Scaffold.of(context).openDrawer(), it doesn't work. My BottomMenu is a separate widget class. As I understand, it doesn't work because it's a separate context. How can I get the right context? Or perhaps someone knows another solution.

Here my code reproducer:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Drawer'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      bottomNavigationBar: BottomMenu(),
      endDrawer: SizedBox(
        width: double.infinity,
        child: Drawer(
          elevation: 16,
          child: Container(
            color: Colors.black,
            child: ListView(
              padding: EdgeInsets.zero,
              children: <Widget>[
                ListTile(
                    title: Text('Some context here',
                        style: TextStyle(color: Colors.white))),
                ListTile(
                    title: Text('Some context here',
                        style: TextStyle(color: Colors.white))),
              ],
            ),
          ),
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Call Drawer form menu reproducer',
            )
          ],
        ),
      ),
    );
  }
}

class BottomMenu extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Wrap(
        alignment: WrapAlignment.center,
        children: <Widget>[
          Divider(color: Colors.black, height: 1),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 2),
            child: Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  InkWell(
                    borderRadius: new BorderRadius.circular(20.0),
                    customBorder: Border.all(color: Colors.black),
                    child: Container(
                      padding: EdgeInsets.only(
                          left: 3, right: 6, bottom: 15, top: 11),
                      child: Row(
                        children: <Widget>[
                          Icon(Icons.menu),
                          Text('Show menu', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold)),
                        ],
                      ),
                    ),
                    onTap: () {
                      Scaffold.of(context).openDrawer();
                    },
                  ),
                ],
              ),
          ),
        ],
      ),
    );
  }
}

Scaffold.of(context).openEndDrawer()

How do you open a drawer in a scaffold using code? : FlutterDev, of for information about how to obtain the ScaffoldState. Implementation. void openDrawer() { if (_endDrawerKey.currentState != null && _endDrawerOpened)  The problem is because Scaffold.of(context) uses the InheritedWidget system of looking at the build context and searching up the hierarchy of that context to find the nearest instance of Scaffold. Your build method is the context and it is creating the Scaffold, so searching 'above it' will not find a Scaffold instance. There are two fixes:

The problem is that you specified endDrawer on Scaffold yet you're calling Scaffold.of(context).openDrawer().

openDrawer() documentation states:

If the scaffold has a non-null Scaffold.drawer, this function will cause the drawer to begin its entrance animation.

Since your drawer is null, nothing happens.

In contrast, openEndDrawer() informs us:

If the scaffold has a non-null Scaffold.endDrawer, this function will cause the end side drawer to begin its entrance animation.

Since your endDrawer is not null you should use openEndDrawer() method. Alternatively, if you don't care which side the drawer slides in from, you can use drawer instead of endDrawer when building Scaffold.

openDrawer method - ScaffoldState class - material , But after I change it, this leading image counldn't open drawer. Please consider asking support questions in one of the channels http://flutter.io/support . Well the leading widget does not automatically open the drawer. return IconButton( icon: Icon(Icons.android), onPressed: () { Scaffold.of(context). void openDrawer () Opens the Drawer (if any). If the scaffold has a non-null Scaffold.drawer, this function will cause the drawer to begin its entrance animation. Normally this is not needed since the Scaffold automatically shows an appropriate IconButton, and handles the edge-swipe gesture, to show the drawer.

My problem solved that instead of

Scaffold.of(context).openEndDrawer()

I give key to Scaffold and then I call by state like below

_scaffoldkey.currentState.openEndDrawer()

It solved my problem I hope It also works for you

Leading in AppBar won't open drawer after I change Leading to an , Widget build(BuildContext context) {. return new Scaffold(. key: _scaffoldKey,. appBar: new AppBar( openDrawer() call on null. Copy link. @kleinlennart. The Scaffold is so important to use in the flutter and I use it so frequently. So we must know how to use it in the right way. We have used this for all of our pages. It is a widget that used to contain our other widget. So let's look at a sample use.

Similar problem here. Clicked on button and nothing happened. The problem is I was using the context of the widget that instantiated Scaffold. Not the context of a child of Scaffold.

Here is how I solved it:

// body: Column(
        //   children: <Widget>[
        //     Row(
        //       children: <Widget>[        
        //         IconButton(
        //           icon: Icon(Icons.filter_list),
        //           onPressed: () => Scaffold.of(context).openEndDrawer(), (wrong context)
        //         ),
        //       ],
        //     ),
        //   ],
        // )

To:

body: Builder(
            builder: (context) => Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        IconButton(
                          icon: Icon(Icons.filter_list),
                          onPressed: () => Scaffold.of(context).openEndDrawer(),
                        ),
                      ],
                    ),
                  ],
                )),
      ),

Flutter Custom Drawer Icon (use keys) · GitHub, Widget build(BuildContext context) {. return new Scaffold(. drawer: new Drawer(),. appBar: new AppBar(. leading: Builder(. builder: (context) => IconButton(. A more efficient solution is to split your build function into several widgets. This introduces a new context from which you can obtain the Scaffold.In this solution, you would have an outer widget that creates the Scaffold populated by instances of your new inner widgets, and then in these inner widgets you would use Scaffold.of.

How does flutter open drawer by code?, In this tutorial, I will tell you how to use the Scaffold and more in the flutter. Widget build(BuildContext context) { you want to put other widgets here, it doesn't matter, but you should custom your own drawer. In the part of the persistentFooterButtons , we have two buttons, we call the openDrawer function in the first one. After having installed Mac OS Catalina 10.15.4, Xcode 11.4 and iOS13.4, my Flutter Application running on iOS (simulator and actual device [iPhoneX]) is very buggy ! Before, I had a floatingActionB

Widgets 08, Flutter Android iOS web Note: those must be called in the context of a Scaffold widget otherwise you might have errors. context.openDrawer(); context. Flutter Context and “.of” method. Scaffold.of(context) → will return an instance of the 1st Scaffold that is found in the context that is sent as a parameter. So, my FAB doesn’t

build_context, open drawer from right side flutter Use a Key in your Scaffold and show the drawer by calling myKey. Widget build(BuildContext context) { return new Scaffold( key: _scaffoldKey, drawer: new Drawer(), Open drawer on clicking AppBar – Icetutor, Alternative to the accepted answer which does not require a GlobalKey  Sometimes you need to implement a simple logic that shows SnackBar. Let’s imagine there is a Button that shows a SnackBar. “Scaffold.of() called with a context that does not contain a Scaffold.