0

What I have A custom button with onpress to open a drawer, my build snippet : (inside MyClassState)

  Widget build(BuildContext context) => Scaffold(
      key: _key,
      body: Container(
        child: Column(
                  children: [
                    Row(children: [

ElevatedButton(
  child: Padding(
    padding: const EdgeInsets.all(5.0),
    child: Icon(
      Icons.settings,
      size: 38,
      color: Colors.white,
    ),
  ),
  onPressed: () => _key.currentState?.openEndDrawer(),
), 

]),]),))

method globalkey _key is used (after reading some solution here)

Class MyClassState extends State<MyClass> {
    GlobalKey<ScaffoldState> _key = GlobalKey();
  ...
}

What I expected

The drawer opens on press/tap

The current behaviour result

Nothing happens on tap, but I can open the drawer using slide gesture.

What did I do wrong in this case?

vontdeux
  • 107
  • 1
  • 2
  • 10

1 Answers1

2

You didn't declared endDrawer in scaffold, Here is the your updated code

 Widget build(BuildContext context) => Scaffold(
      key: _key,
      endDrawer: Drawer(      /// this is missing in your code
        child: Container(
          width: 200,
          color: Colors.red,
        ),
      ),
      body: Container(
        child: Column(
                  children: [
                    Row(children: [

ElevatedButton(
  child: Padding(
    padding: const EdgeInsets.all(5.0),
    child: Icon(
      Icons.settings,
      size: 38,
      color: Colors.white,
    ),
  ),
  onPressed: () => _key.currentState?.openEndDrawer(),
), 

]),]),))
Hemal Moradiya
  • 1,715
  • 1
  • 6
  • 26
  • OHHH i did declare it, but i declare as "drawer: Drawer( ..." do take note that i can open the drawer using slide left to right gesture. so that's why it leaves me wondering why it does not on tap of a button. is there a difference between endDrawer and drawer? – vontdeux Jun 10 '21 at 07:06
  • @vontdeux One is open from left side and second one is open from right side. That's a difference between `Drawer` and `EndDrawer` – Hemal Moradiya Jun 10 '21 at 08:37