问题描述
我想知道,是否有人知道当您使用 >
但是,如果您发现自己正在执行此操作,则可能不希望用户能够按设备的后退"按钮返回到较早的路线.而不是调用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(), }, ); } }
这篇关于扑出移除应用栏上的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!