我想知道,当您使用appBar
转到另一个页面时,是否有人知道有什么方法可以删除显示在Navigator.pushNamed
应用程序中的后退按钮。我不希望它出现在这个结果页面上的原因是,它来自导航,我希望用户使用logout
按钮,以便会话重新开始。
发布于 2017-07-07 11:45:29
您可以通过将一个空的new Container()
作为leading
参数传递给您的AppBar
来删除back按钮。
如果您发现自己这样做,您可能不希望用户能够按下设备的后退按钮返回到以前的路线。不要调用pushNamed
,而是尝试调用Navigator.pushReplacementNamed
以导致早期路由消失。
函数pushReplacementNamed
将移除后堆栈中的前一个路由,并将其替换为新路由。
后者的完整代码示例如下所示。
import 'package:flutter/material.dart';
class LogoutPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Logout Page"),
),
body: new Center(
child: new Text('You have been logged out'),
),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Remove Back Button"),
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.fullscreen_exit),
onPressed: () {
Navigator.pushReplacementNamed(context, "/logout");
},
),
);
}
}
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(),
routes: {
"/logout": (_) => new LogoutPage(),
},
);
}
}
发布于 2017-10-12 07:18:33
我相信解决方案如下
你实际上要么是:
AppBar(...,automaticallyImplyLeading: false,...)
;Navigator.pushReplacementNamed(## your routename here ##)
;Navigator.pushNamedAndRemoveUntil(## your routename here ##, f(Route<dynamic>)→bool);
,其中f是一个函数,在满足您希望保留在堆栈中的最后一个视图时返回true
(就在新视图之前);Navigator.pushNamedAndRemoveUntil(context, ## your routename here ##, (_) => false);
完全清空导航器堆栈干杯
发布于 2017-08-24 06:54:44
删除AppBar中的back按钮的一个简单方法是将automaticallyImplyLeading
设置为false
。
appBar: AppBar(
title: Text("App Bar without Back Button"),
automaticallyImplyLeading: false,
),
https://stackoverflow.com/questions/44978216
复制相似问题