Getx: [Feature Request]: Open Drawer via Get.drawer (like Get.snackbar etc.)

Created on 15 Sep 2020  路  3Comments  路  Source: jonataslaw/getx

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.)

Feature not possible.

Most helpful comment

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'),
          )
        ],
      )),
    );
  }
}

All 3 comments

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'),
          )
        ],
      )),
    );
  }
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manojeeva picture manojeeva  路  3Comments

R-Praveen picture R-Praveen  路  4Comments

ad-on-is picture ad-on-is  路  3Comments

DarkHeros09 picture DarkHeros09  路  3Comments

jemariads picture jemariads  路  4Comments