Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Scaffold's Drawer on page load?

Logging into our Flutter app opens to dashboard that has a Scaffold with a Drawer full of menu items.

I'd like to perform some A/B testing with having the Drawer open on page load or at least animating the Drawer being opened immediately on load.

I'm aware of Scaffold.of(context).openDrawer() but I'm not sure where to place this code so that it will run immediately after the build() method. I'm also not aware of any fields on either Drawer or Scaffold which would load with the Drawer open.

Thanks for your time and help.

like image 909
Greg Noe Avatar asked Jan 02 '23 19:01

Greg Noe


2 Answers

You need to wait after the first frame is loaded.

    _onLayoutDone(_) {
       //your logic here

    }

    @override
    void initState() {
      WidgetsBinding.instance.addPostFrameCallback(_onLayoutDone);
      super.initState();
    }

I wrote a post about this, you can take a look if you want : https://medium.com/@diegoveloper/flutter-widget-size-and-position-b0a9ffed9407

like image 148
diegoveloper Avatar answered Jan 10 '23 20:01

diegoveloper


Override initState.

@override
void initState() {
  super.initState();

  // use this
  Timer.run(() => Scaffold.of(context).openDrawer());
}
like image 35
CopsOnRoad Avatar answered Jan 10 '23 21:01

CopsOnRoad