Bring a route to the top of the navigation stack

Viewed 611

I have a action in my flutter app, which navigates to a named route like so:

            InkWell(
              onTap: () {
                Navigator.pushNamed(context, '/add_request');
              },
              splashColor: Theme.of(_selfContext).colorScheme.secondary,
              child: ListTile(
                leading: Icon(Icons.nature_people_rounded),
                title: Text("Add request"),
              )

However what I really want is the line Navigator.pushNamed(context, '/add_request'); to be replaced by something like Navigator.pushOrBringToTopNamed(context, '/add_request')

i.e., I do not want to always create a new Route but instead bring an existing one to the top of the navigation stack if it exists already.

I did not find any proper way in doing so. Someone knows how to achieve such a thing in somewhat proper flutter?

1 Answers

Using the new Navigator 2.0 (Flutter v1.22) you can now use the pages property to obtain a List of the stack.

You could extend the Navigator and handle custom events such as pop / push events depending on what you find in your stack.

This is a helpful article on the subject.

And this is the official documentation of the Navigator 2.0 from the Flutter team.

Related