garawaa garawaa
garawaa garawaa

Reputation: 320

How can i implement navigation drawer under appbar in flutter

I want to implement navigation drawer in flutter like this screenshot. But don't know how. Please give me some code hint. Thank you.

This is the image I like to archive

Screenshot

Upvotes: -1

Views: 1575

Answers (3)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63799

An easy to archive this is using another Scaffold on body and using drawer there. And to control the drawer use ScaffoldState GlobalKey.

Result
enter image description here

Widget


class _MyApp extends StatefulWidget {
  @override
  State<_MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<_MyApp> {
  static final GlobalKey<ScaffoldState> _key = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
            onPressed: () {
              if (_key.currentState != null) {
                if (_key.currentState!.isDrawerOpen) {
                  Navigator.pop(_key.currentContext!);
                } else {
                  _key.currentState!.openDrawer();
                }
              }
            },
            icon: const Icon(
              Icons.more,
            ),
          ),
        ),
        body: Scaffold(
          key: _key,
          drawer: Drawer(
            child: Container(
              color: Colors.red,
            ),
          ),
          body: Column(
            children: const [
              Text("Child"),
            ],
          ),
        ));
  }
}

Upvotes: 0

FadyFouad
FadyFouad

Reputation: 948

use the Drawer Widget in scaffold this is an example from the official documentation

Scaffold(
  appBar: AppBar(
    title: const Text('Drawer Demo'),
  ),
  drawer: Drawer(
    child: ListView(
      padding: EdgeInsets.zero,
      children: const <Widget>[
        DrawerHeader(
          decoration: BoxDecoration(
            color: Colors.blue,
          ),
          child: Text(
            'Drawer Header',
            style: TextStyle(
              color: Colors.white,
              fontSize: 24,
            ),
          ),
        ),
        ListTile(
          leading: Icon(Icons.message),
          title: Text('Messages'),
        ),
        ListTile(
          leading: Icon(Icons.account_circle),
          title: Text('Profile'),
        ),
        ListTile(
          leading: Icon(Icons.settings),
          title: Text('Settings'),
        ),
      ],
    ),
  ),
);

this is the result => enter image description here

Upvotes: 1

Shaan Mephobic
Shaan Mephobic

Reputation: 1216

You have to use the Drawer widget.

Scaffold(
  drawer: Drawer(
    child: ListView(
      padding: EdgeInsets.zero,
      children: [
        ListTile(
          title: const Text('Item 1'),
          onTap: (){
            // do something
          },
        ),
        ListTile(
          title: const Text('Item 2'),
          onTap: (){
            // do something
          },
        ),
      ],
    ),
  ),
...

And that's pretty much it! Learn more about Drawer, here.

Upvotes: 0

Related Questions