Reputation: 4319
If you create an Scaffold
there is an option for a Drawer
. If you now create this drawer you get automatically the menu icon on the leading position of the Appbar. But I want another icon there to open the drawer. I tried to make an iconButton myself on the leading position, but this button can't open the drawer even with Scaffold.of(context).openDrawer()
.
Is there any option to replace the icon for the drawer button?
Upvotes: 63
Views: 105535
Reputation: 233
Here is your Menu Button
backButton: IconButton(
onPressed: () {
Scaffold.of(context).isDrawerOpen
? Scaffold.of(context).closeDrawer()
: Scaffold.of(context).openDrawer();
}
icon: Icon(
Icons.menu,
color: AppColors.white,
),
),
Upvotes: 0
Reputation: 31
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
leading: IconButton(
onPressed: () {
_scaffoldKey.currentState!.openDrawer();
},
icon: const Icon(Icons.home)),
),
drawer: const Drawer(),
),
- List item
);
}
}
Upvotes: 3
Reputation: 186
Using GlobalKey:
final GlobalKey<ScaffoldState> _key = GlobalKey(); // Create a key
@override
Widget build(BuildContext context) {
return Scaffold(
key: _key, // Assign the key to Scaffold.
drawer: Drawer(),
floatingActionButton: FloatingActionButton(
onPressed: () => _key.currentState!.openDrawer(), // <-- Opens drawer
),
);
}
Using Builder:
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(),
floatingActionButton: Builder(builder: (context) {
return FloatingActionButton(
onPressed: () => Scaffold.of(context).openDrawer(), // <-- Opens drawer.
);
}),
);
}
Upvotes: 13
Reputation: 53317
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(),
),
),
);
}
}
Upvotes: 180
Reputation: 11537
Alternative to the accepted answer which does not require a GlobalKey
:
class _TestState extends State<Test> {
@override
Widget build(BuildContext context) {
return new Scaffold(
drawer: new Drawer(),
appBar: new AppBar(
leading: Builder(
builder: (context) => IconButton(
icon: new Icon(Icons.settings),
onPressed: () => Scaffold.of(context).openDrawer(),
),
),
),
);
}
}
Upvotes: 121
Reputation: 1012
you need initialize scaffoldKey
after that,
Open drawer and close drawer
GestureDetector(
onTap: () {
if(scaffoldKey.currentState.isDrawerOpen){
scaffoldKey.currentState.openEndDrawer();
}else{
scaffoldKey.currentState.openDrawer();
}
},
child: LeadingIcon(icon: Icons.menu),//your button
),
Upvotes: 8