user10508638
user10508638

Reputation:

Flutter Drawer navigate to a single page multiple times

when i pull the drawer and select a page to navigate to it is like create a new instance of that, for example if i clicked on the same page 4 times it shows the animation of opening a new page 4 times and same when i press the back button of the phone.

  new ListTile(
        leading: Icon(Icons.home),
        title: new Text("Home"),
        onTap: () {
          Navigator.pop(ctxt);
          Navigator.push(
              ctxt, new MaterialPageRoute(builder: (ctxt) => MyHomePage()));
        },

here's the drawer file i'm using in all the pages

class DrawerOnly extends StatelessWidget {
  @override
  Widget build(BuildContext ctxt) {
    return new Drawer(
      child: new ListView(
        children: <Widget>[
          new UserAccountsDrawerHeader(
            accountName: new Text('Fethi'),
            accountEmail: new Text('[email protected]'),
            currentAccountPicture: new CircleAvatar(
              backgroundImage: new NetworkImage('http://i.pravatar.cc/300'),
          ),
        ),
      new ListTile(
        leading: Icon(Icons.home),
        title: new Text("Home"),
        onTap: () {
          Navigator.pushReplacement(
              ctxt, new MaterialPageRoute(builder: (ctxt) => MyHomePage()));
        },
      ),
      new ListTile(
        leading: Icon(Icons.note),
        title: new Text("ADD Notes"),
        onTap: () {
          Navigator.pushReplacement(
              ctxt, new MaterialPageRoute(builder: (ctxt) => EditNote()));
        },
      ),
    ],
  ),
);

} }

Upvotes: 4

Views: 4772

Answers (2)

Dhrumil Shah - dhuma1981
Dhrumil Shah - dhuma1981

Reputation: 15789

Simply replace the Navigator.push to Navigator.pushReplacement & remove Navigator.pop

It will replace the current screen with the new screen. And it will solve your problem.

To know in detail, just check this documentation: https://docs.flutter.io/flutter/widgets/Navigator/pushReplacement.html

Upvotes: 1

Amol Gangadhare
Amol Gangadhare

Reputation: 1105

As per your code, you are initializing the MyHomePage screen on top of the current screen(starting new screen). In drawer navigation you have to replace the body: with the screens you wish to show.

Example :

Take a variable like

  int selectionScreen = 0;

you can take it as per your need, I took an int. Now based on this you can choose which screen you wish to show.

new Scaffold(
        appBar: new AppBar(
          // here we display the title corresponding to the fragment
          // you can instead choose to have a static title
          title: new Text("Drawer navigation"),
        ),
        drawer: new Drawer(
          child: new Column(
            children: <Widget>[
              new ListTile(
                  leading: Icon(Icons.home),
                  title: new Text("Home"),
                  onTap: () {
                    setState(() {
                      selectionScreen = 0;
                    });
                    Navigator.pop(context);
                  })
            ],
          ),
        ),
        body: _getScreen(selectionScreen));

Now you can use _getScreen() to get multiple screens, as you add more ListTile to drawer list.

_getScreen(int selection) {
    switch (selection) {
      case 0:
        return MyHomePage();

      case 1:
        return Screen2();
      default:
    }
  }

This will replace the screens in the existing drawer navigation screen.

Please acknowledge if this was the issue you were facing. Thank you.

Upvotes: 0

Related Questions