How to reset the base route in my Flutter App - that is pop any routes and replace it with a new one using Navigator

后端 未结 3 1874
予麋鹿
予麋鹿 2021-02-07 13:00

New to Flutter to please forgive me if this is obvious, but I have read the documentation and tried a bunch of the Navigator options but none of them work as I would like. The

相关标签:
3条回答
  • 2021-02-07 13:29

    Navigator expose more than just pop. You can do things such as the following:

    Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);
    

    This will basically push a home and remove all the routes behind the new one

    0 讨论(0)
  • 2021-02-07 13:43

    The good news is now we can use pushReplacementNamed

    Navigator.pushReplacementNamed(context, '/');
    

    (Channel master, v1.15.4-pre.97, I'm not sure the previous version can do that)

    0 讨论(0)
  • 2021-02-07 13:43

    Try this code:

        Navigator.pushAndRemoveUntil(
                        context,
                        MaterialPageRoute(
                          builder: (BuildContext context) => YourInitialPage(),
                        ),
                        ModalRoute.withName('/'));
    

    If you don't want transition you can override or extend the MaterialPageRoute class

    0 讨论(0)
提交回复
热议问题