When I add a drawer to a scaffold, it opens easily and without problems via Gestures (sliding from left to right),
but when I want to open it on a button click without having an AppBar, I would have to change it to a StatefulWidget.
So it would be great to be able to open the drawer everywhere, just like it is possible with e.g. snackbars or other widgets at the moment thanks to GetX.
(In case you decide to add drawers to GetX, please note that there is also an endDrawer widget (which opens from the right side). Maybe you want to add it, too.)
Drawer depends on a ScaffoldState, while the Getx context provides a NavigatorState.
So it would not be possible to create this resource, even because there is another solution available: Create a globalKey in your controller, and attach it to your Scaffold. You can open and close the drawer directly from your globalKey
@jonataslaw could you give an example of this: "Create a globalKey in your controller, and attach it to your Scaffold. You can open and close the drawer directly from your globalKey" please?
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
Get.put(Controller());
runApp(GetMaterialApp(home: Home()));
}
class Controller extends GetxController {
var scaffoldKey = GlobalKey<ScaffoldState>();
void openDrawer() {
scaffoldKey.currentState.openDrawer();
}
void closeDrawer() {
scaffoldKey.currentState.openEndDrawer();
}
}
class Home extends GetView<Controller> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
key: controller.scaffoldKey,
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: controller.closeDrawer,
)
],
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: controller.openDrawer,
child: Text('open drawer'),
)
],
)),
);
}
}
Most helpful comment