Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open drawer on clicking AppBar

Tags:

flutter

If you create an Scafold there is an option for drawer. If you now create this drawer you get automaticly the menu icon on the leading position of the appbar. But i want an other icon there which opens the drawer. I tried to make an iconbutton myself on the leading position but this button can‘t open the drawer even with „Scafold.of(context).openDrawer()“ it can‘t open it.

Is there any option to replace the icon for the drawer button?

like image 660
Lukas Kirner Avatar asked Nov 22 '17 12:11

Lukas Kirner


People also ask

How do you open the AppBar drawer in Flutter?

The navigation drawer in Flutter allows users to navigate to different pages of your app. The navigation drawer is added using the Drawer widget. It can be opened via swipe gesture or by clicking on the menu icon in the app bar.

How do you open the drawer on custom button click in Flutter?

Usually, there will be an Icon button added to appbar when you add a drawer to the scaffold. But when you have a custom appbar and you may need to add the button with on tap and below is the ontap code that helps you to open the Drawer.

How do you open the drawer without an AppBar Flutter?

You have your own custom Menu button to open/close drawer. You don't want to use AppBar as well. In that case you can use GlobalKey<ScaffoldState>() object to open Drawer.


1 Answers

Use a Key in your Scaffold and show the drawer by calling myKey.currentState.openDrawer(), here is a working code:

enter image description here

import "package:flutter/material.dart";  class Test extends StatefulWidget {   @override   _TestState createState() => new _TestState(); }  class _TestState extends State<Test> {   final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();    @override   Widget build(BuildContext context) {     return new Scaffold(       key: _scaffoldKey,       drawer: new Drawer(),       appBar: new AppBar(         leading: new IconButton(           icon: new Icon(Icons.settings),           onPressed: () => _scaffoldKey.currentState.openDrawer(),         ),       ),     );   } }  
like image 185
Shady Aziza Avatar answered Nov 08 '22 03:11

Shady Aziza