Flutter: How to open Drawer programmatically

前端 未结 6 1010
暖寄归人
暖寄归人 2020-12-29 21:11

I want to open Drawer programmatically not by sliding it, how to disable that sliding functionality (touch functionality of Drawer)

6条回答
  •  无人及你
    2020-12-29 21:50

    Calling Scaffold.of doesn't work because the context doesn't contain the Scaffold. Some solutions above have ignored this, others have used GlobalKey. I believe the cleanest solution is wrapping the button in a Builder:

    Scaffold(
       drawerEnableOpenDragGesture: false, // Prevent user sliding open
       appBar: AppBar(
          automaticallyImplyLeading: false,
          title: Text("Some Title"),
          actions: [
             Builder(builder: (context) => // Ensure Scaffold is in context
                IconButton(
                   icon: Icon(Icons.settings),
                   onPressed: () => Scaffold.of(context).openDrawer()
             )),
          ],
       ),
       // TODO ...
    )
    

提交回复
热议问题