我在导航器中递归添加路线。可能有20个或更多视图。 Pop按宣传方式工作,但我想弹出索引1,并删除所有推送历史记录。有没有办法用像... returntoIndex0 ...
这样的东西替换这个pop命令//========================================================
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);
},
),
),
// ============================================= ===========
答案 0 :(得分:20)
如果您使用MaterialPageRoute
创建路由,则可以使用以下命令:
Navigator.popUntil(context, ModalRoute.withName(Navigator.defaultRouteName))
Navigator.defaultRouteName
反映了应用程序启动的路径。这是一段代码,更详细地说明了它:
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))
),
希望这会有所帮助。
答案 1 :(得分:11)
如果您不使用命名路由,则可以使用
Navigator.of(context).popUntil((route) => route.isFirst);
答案 2 :(得分:5)
您也可以这样
Navigator.of(context)
.pushNamedAndRemoveUntil('/Destination', ModalRoute.withName('/poptillhere'),arguments: if you have any);
用例是转到所需的屏幕,然后根据需要弹出它们之间的屏幕。
有关更多信息,您可以检查此Post Explaining other Solutions
答案 3 :(得分:4)
使用文档中提到的popUntil
方法
典型用法如下:
Navigator.popUntil(context, ModalRoute.withName('/login'));
答案 4 :(得分:3)
如果您确切知道应该执行多少次弹出:
例如2次弹出:
count = 0;
Navigator.popUntil(context, (route) {
return count++ == 2;
});
答案 5 :(得分:2)
此处Dashboard()是屏幕名称。因此,这将弹出所有屏幕并转到Dashboard()屏幕。
const defVal: DefaultType = {...};
const inferedVal: AnyTypeOfChoice = {...};
const a = new A((defVal) => {...});
const a2 = new A((inferedVal) => {...}, async () => inferedVal);
答案 6 :(得分:1)
对我来说,我在推送新页面时使用了它:
widget = MyWidget();
Route route = CupertinoPageRoute(builder: (context) => widget, settings:RouteSettings(name: widget.toStringShort()));
Navigator.push(context, route);
然后返回到特定页面:
Navigator.of(context).popUntil((route) => route.settings.name == "MyWidget");
答案 7 :(得分:0)
我在本文中尝试了其他答案,并以某种方式导致以下异常。
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
...
════════════════════════════════════════════════════════════════════════════════
以下答案解决了该问题。 https://stackoverflow.com/a/52048127/2641128
Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);
答案 8 :(得分:0)
这总是让我得到预期的结果。 并且会弹出到当前导航栈的路由
Navigator.of(context, rootNavigator: true).pop();
答案 9 :(得分:-1)
//========================================================
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('/'));
},
),
),
//========================================================
用.popUntil替换.pop,实际上非常优雅。