I'm trying to use the IconButton that can open the drawer on my app page, so when I tap the icon button, I expect to see the drawer. I've been looking up a way to do so online, but seemed like there're only two solutions: I can either use the appbar to put the IconButton in, or I can try the floating action button. But they're not what I'm looking for, I want just the IconButton to open the drawer. Is it possible do it?
Asked
Active
Viewed 2,459 times
2
-
take a look on this: https://stackoverflow.com/questions/47435231/change-the-menu-icon-for-drawer-at-appbar . I already have the same problem and this answer helps me – Rubens Melo Apr 15 '19 at 22:42
-
@RubensMelo the problem is the IconButton is still in appBar. I want to use the IconButton to open the drawer, but I really don't want an app bar in my page. – DSC Apr 15 '19 at 22:48
-
1the solution is the same, use a globally to "active" drawer with icon button using `_scaffoldKey.currentState.openDrawer()` – Rubens Melo Apr 15 '19 at 22:54
-
1Possible duplicate of [Change the menu icon for drawer at appbar](https://stackoverflow.com/questions/47435231/change-the-menu-icon-for-drawer-at-appbar) – Worthwelle Apr 15 '19 at 23:08
-
Figured this out. Thank you! – DSC Apr 15 '19 at 23:39
1 Answers
3
Yes, you can easily open the drawer through IconButton without using appBar. You need to use Key like I had used _scaffoldKey and use _scaffoldKey.currentState.openDrawer() method to open the drawer in IconButton widget.
class HomeState extends StatelessWidget {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: Text("Ttem 1"),
trailing: Icon(Icons.arrow_forward),
),
ListTile(
title: Text("Item 2"),
trailing: Icon(Icons.arrow_forward),
),
],
),
),
body: ListView(
children:[
Container(
margin: EdgeInsets.only(left: 15.0,top:100.0),
child: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
},
),
),
]
),
);}
}

Tahseen Quraishi
- 1,353
- 1
- 14
- 16