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?
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.
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.
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.
Use a Key
in your Scaffold
and show the drawer by calling myKey.currentState.openDrawer()
, here is a working code:
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(), ), ), ); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With