简体   繁体   English

Flutter Navigation 弹出到索引 1

[英]Flutter Navigation pop to index 1

I am recursively adding routes to the navigator.我正在递归地将路线添加到导航器。 There could be 20 views or more.可能有 20 个或更多视图。 Pop works as advertised, but I would like to pop to index 1 and remove all push history. Pop 像宣传的那样工作,但我想弹出到索引 1 并删除所有推送历史记录。 is there a way to replace this pop command with something like... returntoIndex0...有没有办法用类似... returntoIndex0 ...

      new ListTile(
        title: new RaisedButton(
          child: new Text("POP"),
          onPressed: () {
            var route = new MaterialPageRoute(
              builder: (BuildContext context) =>
                  new NextPage3(value:"hi there from 3"),
            );
            Navigator.pop(context);
          },
        ),
      ),

如果您不使用命名路由,则可以使用

Navigator.of(context).popUntil((route) => route.isFirst);

In case you know exactly how many pops should be performed:如果您确切知道应该执行多少次弹出:

For example for 2 pops:例如 2 次弹出:

count = 0;
Navigator.popUntil(context, (route) {
    return count++ == 2;
});

If you are using MaterialPageRoute to create routes, you can use this command:如果你使用MaterialPageRoute创建路由,你可以使用这个命令:

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

Navigator.defaultRouteName reflects the route that the application was started with. Navigator.defaultRouteName反映了启动应用程序的路由。 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.希望这可以帮助。

For me I used this when pushing a new page:对我来说,我在推送新页面时使用了这个:

widget = MyWidget();
Route route = CupertinoPageRoute(builder: (context) => widget, settings:RouteSettings(name: widget.toStringShort()));
Navigator.push(context, route);

Then to go back to specific page:然后返回特定页面:

Navigator.of(context).popUntil((route) => route.settings.name == "MyWidget");

UsepopUntil method as mentioned in the docs使用文档中提到的popUntil方法

Typical usage is as follows:典型用法如下:

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

Here Dashboard() is the screen name.这里 Dashboard() 是屏幕名称。 So this will pop out all the screens and goto Dashboard() screen.所以这将弹出所有屏幕并转到 Dashboard() 屏幕。

Navigator.of(context).pushAndRemoveUntil(
                  MaterialPageRoute(builder: (c) => Dashboard()),
                  (route) => false)

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有关更多信息,您可以查看这篇解释其他解决方案的帖子

I tried other answers in this post, and somehow they causing the following exception.我在这篇文章中尝试了其他答案,但不知何故,它们导致了以下异常。

To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

The relevant error-causing widget was
    MaterialApp 
lib/main.dart:72
When the exception was thrown, this was the stack
#0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> 
package:flutter/…/widgets/framework.dart:3781
#1      Element._debugCheckStateIsActiveForAncestorLookup 
package:flutter/…/widgets/framework.dart:3795
#2      Element.dependOnInheritedWidgetOfExactType 
package:flutter/…/widgets/framework.dart:3837
#3      Theme.of 
package:flutter/…/material/theme.dart:128
#4      XXxxXX.build.<anonymous closure> 
package:xxx/widgets/xxx.dart:33
...
════════════════════════════════════════════════════════════════════════════════

The following answer fixed the issue.以下答案解决了这个问题。 https://stackoverflow.com/a/52048127/2641128 https://stackoverflow.com/a/52048127/2641128

Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);
//========================================================
          new ListTile(
            title: new RaisedButton(
              child: new Text("POP until"),
              onPressed: () {
                var route = new MaterialPageRoute(
                  builder: (BuildContext context) =>
                      new NextPage3(value:"hi there from 3"),
                );
               //Navigator.pop(context, ModalRoute.withName('/'));
                Navigator.popUntil(context,ModalRoute.withName('/'));                
              },
            ),
          ),
//========================================================

replace .pop with .popUntil, actually works very elegantly.将 .pop 替换为 .popUntil,实际上非常优雅。

This always gets me the expected result.这总是让我得到预期的结果。 And will pop to route of current Navigator stack并将弹出到当前导航器堆栈的路由

 Navigator.of(context, rootNavigator: true).pop();

这将弹出所有路由,直到主默认路由并推送到您的目标路由。

Navigator.pushNamedAndRemoveUntil(context, "destination_route", ModalRoute.withName('/')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM