扑出移除应用栏上的后退按钮 [英] flutter remove back button on appbar
问题描述
我想知道,是否有人知道当您使用Navigator.pushNamed
转到另一页时,如何删除显示在Flutter应用程序中appBar
上的后退按钮的方法.我不希望在此结果页面上显示它的原因是它来自导航,我希望用户改用logout
按钮,以便会话重新开始.
没有称为popNamed
的方法.我假设您的意思是 pushNamed
.
您可以通过传递空的new Container()
作为 leading
作为您的 AppBar
的参数. >
但是,如果您发现自己正在执行此操作,则可能不希望用户能够按设备的后退"按钮返回到较早的路线.而不是调用pushNamed
,请尝试调用 Navigator.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(),
},
);
}
}
I am wondering, if anyone knows of a way to remove the back button that shows up on the appBar
in a flutter app when you use Navigator.pushNamed
to go to another page. The reason I do not want it on this resulting page is that it is coming from the navigation and I want users to use the logout
button instead, so that the session starts over.
There's no method called popNamed
. I assume you meant pushNamed
.
You can remove the back button by passing an empty new Container()
as the leading
argument to your AppBar
.
However, if you find yourself doing this, you probably don't want the user to be able to press the device's back button to get back to the earlier route. Instead of calling pushNamed
, try calling Navigator.pushReplacementNamed
to cause the earlier route to disappear.
Full code sample for the latter approach is below.
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(),
},
);
}
}
这篇关于扑出移除应用栏上的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!