vontdeux
vontdeux

Reputation: 107

flutter opening a drawer using a button inside a scaffold body

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?

Upvotes: 0

Views: 952

Answers (1)

Hemal Moradiya
Hemal Moradiya

Reputation: 2097

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(),
), 

]),]),))

Upvotes: 2

Related Questions