How to access Drawermenu in the multi-screen flutter?

Asked

Viewed 361 times

1

I’m Getting an Application in Flutter, added to my HomePage one DrawerMenu, It works well, but when I access another page, I wish the menu could be opened from anyone. I saw some examples creating the Drawer in separate files, but I couldn’t put in my project.

Meu Drawer:

        drawer: Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          DrawerHeader(

            child:  Image.asset("assets/quadro.jpg"),
            decoration: BoxDecoration(
              color: Colors.white24,
            ),
          ),
          ListTile(
            leading: Icon(Icons.home),
              title: Text('Home'),
              onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => HomePage(),
                ),
              );
            }),
          ListTile(
            title: Text('Item 2'),
            onTap: () {},
          ),
        ],
      ),
    )

1 answer

2


Create a new file called "Customdrawer" and insert a new Widget (it could be Stateless). Put the code of your Drawer in the "Return" of this widget.

Drawer(
  child: ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      DrawerHeader(

        child:  Image.asset("assets/quadro.jpg"),
        decoration: BoxDecoration(
          color: Colors.white24,
        ),
      ),
      ListTile(
        leading: Icon(Icons.home),
          title: Text('Home'),
          onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => HomePage(),
            ),
          );
        }),
      ListTile(
        title: Text('Item 2'),
        onTap: () {},
      ),
    ],
  ),
)

After doing this all you need to do is call this Customdrawer in each Appbar component you insert into your application screens.

However, it is necessary to be careful and think carefully about the behavior of the stacking of the screens. It is advisable to perform several tests by activating them and using the backbuttom of the device to check if the battery is in the order you want. In some cases you will need to replace it instead of just normal stacking.

  • Great Leonardo, it worked very well this, the stacking part I will rethink further, for now having the menu accessed from any screen already helps me a lot. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.