how to contronl drawer and endDrawer with 2 button in flutter

2,947

Solution 1

this what I was need

        Scaffold.of(context).openEndDrawer();

Solution 2

try this code,

class Act_Drawer extends StatefulWidget {
  @override
  _Act_DrawerState createState() => _Act_DrawerState();
}

class _Act_DrawerState extends State<Act_Drawer> {
  final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldkey,
      appBar: AppBar(
        title: Text("Drawer Demo"),
        automaticallyImplyLeading: false,
        leading: StatefulBuilder(
          builder: (BuildContext context, setState) {
            return IconButton(
              icon: Icon(Icons.format_align_left),
              onPressed: () {
                _scaffoldkey.currentState.openDrawer();
              },
            );
          },
        ),
        actions: <Widget>[
          StatefulBuilder(builder: (BuildContext context, setState) {
            return IconButton(
              icon: Icon(Icons.format_align_right),
              onPressed: () {
                _scaffoldkey.currentState.openEndDrawer();
              },
            );
          },
          )
        ],
      ),
      drawer: Drawer(),
      endDrawer: Drawer(),
      body: Container(),
    );
  }
} 

Solution 3

Make Global key of scaffold and put it in your scaffold......

class _AppState extends State<MyHome> {

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

    @override
    Widget build(BuildContext context) {
      return Scaffold(
          key: _scaffoldKey,
          appBar: PreferredSize(
            preferredSize: Size(null, 180),
            child: _getAppBar(
                _selectedDrawerIndex), //CustomAppBar(_scaffoldKey, controller),
          ),
          drawer: createDrwaer(),
          body: WillPopScope(
            child: _getDrawerFragment(_selectedDrawerIndex),
            onWillPop: () {
              if (_selectedDrawerIndex == 1) {
                _onSelectItem(0);
              } else {
                Navigator.of(context).pop();
              }
            },
          ) //_getDrawerFragment(_selectedDrawerIndex),
          );
    }
  }

after that use this on tap of button...

onTap: () =>_scaffoldKey.currentState.openDrawer(),

Be sure to make your class Stateful

Share:
2,947
Husamuldeen
Author by

Husamuldeen

Updated on December 16, 2022

Comments

  • Husamuldeen
    Husamuldeen over 1 year

    I want to program 2 buttons: the first opens the Drawer and the second opens the endDrawer.

    the first button I used

    onTap: () => _drawerKey.currentState.openEndDrawer(),
    

    the second I used

    onTap: () => _drawerKey.currentState.openDrawer(),
    

    but the code didn't work

    • Nardeepsinh Vaghela
      Nardeepsinh Vaghela over 4 years
      try this Scaffold.of(context).openDrawer();
    • Husamuldeen
      Husamuldeen over 4 years
      dear @Florian,I have a flutter app with drawer and end drawer, in the app there is 2 button, the first button open the drawer, and the other open the endDrawer,
  • Husamuldeen
    Husamuldeen over 4 years
    I already made the global key final GlobalKey<ScaffoldState> _drawerKey = new GlobalKey<ScaffoldState>(); and remember that I have two button with tow drawers (Drawer and endDrawer) I can open the drawer but the endDrawer didn't open with the button
  • Vipul Chauhan
    Vipul Chauhan over 4 years
    So please post your full code or you can try this _scaffoldKey.currentState.openEndDrawer()
  • Husamuldeen
    Husamuldeen over 4 years
    I got this error "The method 'openDrawer' was called on null. Receiver: null Tried calling: openDrawer()"
  • Vipul Chauhan
    Vipul Chauhan over 4 years
    there is a possibility that you have not initialized global key or something else I will solve this but first, you have to post your full code.
  • Husamuldeen
    Husamuldeen over 4 years
    the code work with the appBar, but I'm using two buttons, GestureDetector( onTap: () { _scaffoldkey.currentState.openEndDrawer(); }, child: DrawerButtonMenu(), ), GestureDetector( onTap: () { _scaffoldkey.currentState.openDrawer(); },child: DollarButton(), ),
  • Husamuldeen
    Husamuldeen over 4 years
    I tried the code in empty app it dose work, but in my app I've got error ════════ (2) Exception caught by gesture ═══════════════════════════════════════════════════════════ The method 'openDrawer' was called on null. Receiver: null Tried calling: openDrawer()
  • Husamuldeen
    Husamuldeen over 4 years
    sorry I can't show the full code because of company rules but, here is the error ════════ (2) Exception caught by gesture ═══════════════════════════════════════════════════════════ The method 'openDrawer' was called on null. Receiver: null Tried calling: openDrawer()