Flutter Navigation pop to index 1

前端 未结 9 1304
梦毁少年i
梦毁少年i 2020-12-02 13:54

I am recursively adding routes to the navigator. There could be 20 views or more. Pop works as advertised, but I would like to pop to index 1 and remove all push history.

相关标签:
9条回答
  • 2020-12-02 14:33

    In case you know exactly how many pops should be performed:

    For example for 2 pops:

    count = 0;
    Navigator.popUntil(context, (route) {
        return count++ == 2;
    });
    
    0 讨论(0)
  • 2020-12-02 14:37

    Use popUntil method as mentioned in the docs

    Typical usage is as follows:

    Navigator.popUntil(context, ModalRoute.withName('/login'));

    0 讨论(0)
  • 2020-12-02 14:41

    You can also do it like this

    Navigator.of(context)
                .pushNamedAndRemoveUntil('/Destination', ModalRoute.withName('/poptillhere'),arguments: if you have any);
    

    The use case is to go the desired screen and pop the screens in between as you require.

    For more info, you can check this Post Explaining other Solutions

    0 讨论(0)
  • 2020-12-02 14:42

    Here Dashboard() is the screen name. So this will pop out all the screens and goto Dashboard() screen.

    Navigator.of(context).pushAndRemoveUntil(
                      MaterialPageRoute(builder: (c) => Dashboard()),
                      (route) => false)
    
    0 讨论(0)
  • 2020-12-02 14:43

    If you do not use named routes, you can use

    Navigator.of(context).popUntil((route) => route.isFirst);
    
    0 讨论(0)
  • 2020-12-02 14:43

    If you are using MaterialPageRoute to create routes, you can use this command:

    Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName))
    

    Navigator.defaultRouteName reflects the route that the application was started with. Here is the piece of code that illustrates it in more detail:

    child: InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Image(
                    image: AssetImage('assets/img/ic_reset.png'),),
                  Text('Change my surgery details',
                    style: TextStyle(color: Colors.blue, decoration: TextDecoration.underline),),
                ],
              ),
              onTap: () =>
                  Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName))
            ),
    

    Hope this helps.

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