Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a navigation stack? Pop all previous screens in Flutter? [duplicate]

Tags:

flutter

So I have this kind of walkthrough that pushes the user from one screen to another (Six screens in total). In the last page, I would like the user to press "Done" and take him back to the first screen without being able to pop back to any of the previous screens.

Right now it will pop the last screen but not any of the other ones so it take me back to my original screen with the ability to pop back to the screen before the last screen (hopefully that last sentence makes sense to you, because the last screen was popped it takes me back to the screen before that one).

Any idea how to get around that?

Thanks!

like image 899
Nomnom Avatar asked Nov 13 '18 16:11

Nomnom


People also ask

How do I route to the same page in flutter?

If the new page is the same as the current page then use Navigator. push(context,route) . If the new page is the different then use Navigator. pushReplacement(context,route) .

How to remove a screen from the stack in flutter?

So in case of Flutter, when we navigate to another screen, we use the push methods and Navigator widget adds the new screen onto the top of the stack. Naturally, the pop methods would remove that screen from the stack. So lets move to the codebase of our sample project and let’s see how we can move from Screen 1 to Screen 2.

How do I navigate from one screen to another in flutter?

In Flutter, navigation from one screen to another is possible because of Navigators, a simple widget that maintains a stack of Routes, or in simpler terms, a history of visited screens/pages. You will find plenty of articles that tell you how to push to a new screen or pop from the current screen, but this article is a little more than that.

How to pop multiple screens from the navigation stack using navigator?

To pop multiple screens from the navigation stack, like in your given scenario we can use Navigator.popUntil. It takes a BuildContext and a RoutePredicate as parameters. The Navigator calls pop until the returned value by the given RoutePredicate is true. Here is very basic example. It uses ModalRoute.withName to create the RoutePredicate:

How to achieve navigation in Flutter App using routes?

We can achieve navigation in our flutter app using push (), pop () methods, or by writing our own routes. Let’s consider FirstScreen and SecondScreen as two different classes in FirstScreen.dart file and SecondScreen.dart respectively.


2 Answers

To pop multiple screens from the navigation stack, like in your given scenario we can use Navigator.popUntil. It takes a BuildContextand a RoutePredicate as parameters. The Navigator calls pop until the returned value by the given RoutePredicate is true.

Here is very basic example. It uses ModalRoute.withName to create the RoutePredicate:

enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        '/': (BuildContext context) => Home(),
        '/another': (BuildContext context) => Another()
      },
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Home'),
            RaisedButton(
              child: Text('Take me to another screen!'),
              onPressed: () => Navigator.pushNamed(context, '/another'),
            )
          ],
        ),
      ),
    );
  }
}

class Another extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Next screen.'),
              onPressed: () => Navigator.pushNamed(context, '/another'),
            ),
            RaisedButton(
              child: Text('Pop em all.'),
              onPressed: () => Navigator.popUntil(
                    context,
                    ModalRoute.withName('/'),
                  ),
            ),
          ],
        ),
      ),
    );
  }
}
like image 129
NiklasPor Avatar answered Oct 22 '22 04:10

NiklasPor


The method you are looking for is popUntil(), take a look at the implementation https://api.flutter.dev/flutter/widgets/Navigator/popUntil.html or https://api.flutter.dev/flutter/widgets/NavigatorState/popUntil.html

You will have to do probably something like

Navigator.of(context).popUntil(ModalRoute.withName('/my-target-screen'));

(take a look at https://api.flutter.dev/flutter/widgets/ModalRoute/withName.html)

or use some custom function (https://api.flutter.dev/flutter/widgets/RoutePredicate.html).

like image 20
tenhobi Avatar answered Oct 22 '22 05:10

tenhobi