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












2
















This question already has an answer here:




  • Flutter Navigation pop to index 1

    3 answers




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!










share|improve this question













marked as duplicate by Blasanka, Community Nov 14 '18 at 19:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • For example docs.flutter.io/flutter/widgets/Navigator/popUntil.html

    – Günter Zöchbauer
    Nov 13 '18 at 16:44
















2
















This question already has an answer here:




  • Flutter Navigation pop to index 1

    3 answers




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!










share|improve this question













marked as duplicate by Blasanka, Community Nov 14 '18 at 19:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • For example docs.flutter.io/flutter/widgets/Navigator/popUntil.html

    – Günter Zöchbauer
    Nov 13 '18 at 16:44














2












2








2









This question already has an answer here:




  • Flutter Navigation pop to index 1

    3 answers




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!










share|improve this question















This question already has an answer here:




  • Flutter Navigation pop to index 1

    3 answers




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!





This question already has an answer here:




  • Flutter Navigation pop to index 1

    3 answers








flutter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:37









NomnomNomnom

575




575




marked as duplicate by Blasanka, Community Nov 14 '18 at 19:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Blasanka, Community Nov 14 '18 at 19:36


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • For example docs.flutter.io/flutter/widgets/Navigator/popUntil.html

    – Günter Zöchbauer
    Nov 13 '18 at 16:44



















  • For example docs.flutter.io/flutter/widgets/Navigator/popUntil.html

    – Günter Zöchbauer
    Nov 13 '18 at 16:44

















For example docs.flutter.io/flutter/widgets/Navigator/popUntil.html

– Günter Zöchbauer
Nov 13 '18 at 16:44





For example docs.flutter.io/flutter/widgets/Navigator/popUntil.html

– Günter Zöchbauer
Nov 13 '18 at 16:44












2 Answers
2






active

oldest

votes


















3














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('/'),
),
),
],
),
),
);
}
}





share|improve this answer































    1














    The method you are looking for is popUntil(), take a look at the implementation https://docs.flutter.io/flutter/widgets/Navigator/popUntil.html or https://docs.flutter.io/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://docs.flutter.io/flutter/widgets/ModalRoute/withName.html)



    or use some custom function (https://docs.flutter.io/flutter/widgets/RoutePredicate.html).






    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      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('/'),
      ),
      ),
      ],
      ),
      ),
      );
      }
      }





      share|improve this answer




























        3














        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('/'),
        ),
        ),
        ],
        ),
        ),
        );
        }
        }





        share|improve this answer


























          3












          3








          3







          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('/'),
          ),
          ),
          ],
          ),
          ),
          );
          }
          }





          share|improve this answer













          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('/'),
          ),
          ),
          ],
          ),
          ),
          );
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 17:04









          NiklasNiklas

          45418




          45418

























              1














              The method you are looking for is popUntil(), take a look at the implementation https://docs.flutter.io/flutter/widgets/Navigator/popUntil.html or https://docs.flutter.io/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://docs.flutter.io/flutter/widgets/ModalRoute/withName.html)



              or use some custom function (https://docs.flutter.io/flutter/widgets/RoutePredicate.html).






              share|improve this answer




























                1














                The method you are looking for is popUntil(), take a look at the implementation https://docs.flutter.io/flutter/widgets/Navigator/popUntil.html or https://docs.flutter.io/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://docs.flutter.io/flutter/widgets/ModalRoute/withName.html)



                or use some custom function (https://docs.flutter.io/flutter/widgets/RoutePredicate.html).






                share|improve this answer


























                  1












                  1








                  1







                  The method you are looking for is popUntil(), take a look at the implementation https://docs.flutter.io/flutter/widgets/Navigator/popUntil.html or https://docs.flutter.io/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://docs.flutter.io/flutter/widgets/ModalRoute/withName.html)



                  or use some custom function (https://docs.flutter.io/flutter/widgets/RoutePredicate.html).






                  share|improve this answer













                  The method you are looking for is popUntil(), take a look at the implementation https://docs.flutter.io/flutter/widgets/Navigator/popUntil.html or https://docs.flutter.io/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://docs.flutter.io/flutter/widgets/ModalRoute/withName.html)



                  or use some custom function (https://docs.flutter.io/flutter/widgets/RoutePredicate.html).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 16:49









                  tenhobitenhobi

                  78711639




                  78711639















                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly