【发布时间】:2017-12-12 04:35:09
【问题描述】:
我想知道,当您使用 Navigator.pushNamed 转到另一个页面时,是否有人知道在颤振应用程序中删除 appBar 上显示的后退按钮的方法。我不希望它出现在这个结果页面上的原因是它来自导航,我希望用户改用 logout 按钮,以便会话重新开始。
【问题讨论】:
标签: flutter navigation back-button appbar
我想知道,当您使用 Navigator.pushNamed 转到另一个页面时,是否有人知道在颤振应用程序中删除 appBar 上显示的后退按钮的方法。我不希望它出现在这个结果页面上的原因是它来自导航,我希望用户改用 logout 按钮,以便会话重新开始。
【问题讨论】:
标签: flutter navigation back-button appbar
我认为解决方案如下
实际上你要么:
不想显示那个丑陋的后退按钮 (:]),因此选择:
AppBar(...,automaticallyImplyLeading: false,...);
不希望用户返回 - 替换当前视图 - 因此选择:
Navigator.pushReplacementNamed(## your routename here ##);
不希望用户返回 - 将某个视图替换回堆栈中 - 因此使用:
Navigator.pushNamedAndRemoveUntil(## your routename here ##, f(Route<dynamic>)→bool);
其中 f 是返回 true 的函数,当遇到要保留在堆栈中的最后一个视图时(就在新视图之前);
不希望用户返回 - EVER - 完全清空导航器堆栈:
Navigator.pushNamedAndRemoveUntil(context, ## your routename here ##, (_) => false);
干杯
【讨论】:
删除 AppBar 中的后退按钮的简单方法是将automaticallyImplyLeading 设置为false。
appBar: AppBar(
title: Text("App Bar without Back Button"),
automaticallyImplyLeading: false,
),
【讨论】:
Navigator.pushReplacementNamed 是正确的解决方案。您建议的是一种解决方法,如果应用于所有场景,最终可能会推断出错误的行为,例如某人希望 AppBar 继续暗示领先行为的某个地方(即:后退导航按钮)
您可以通过将空的new Container() 作为leading 参数传递给AppBar 来移除后退按钮。
如果您发现自己这样做了,您可能不希望用户能够按下设备的返回按钮来返回之前的路线。不要调用pushNamed,而是尝试调用Navigator.pushReplacementNamed 以使之前的路由消失。
函数pushReplacementNamed会移除backstack中之前的路由,并用新路由替换。
后者的完整代码示例如下。
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(),
},
);
}
}
【讨论】:
pushReplacementNamed() 是否处理前一个屏幕小部件(以及所有相关数据和状态)?
自动暗示前导:
这将检查我们是否要在应用栏上应用后置小部件(前导小部件)。 如果 automaticImplyLeading 为 false,则自动为标题分配空格,如果前导小部件为 true,则此参数无效。
void main() {
runApp(
new MaterialApp(
home: new Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false, // Used for removing back buttoon.
title: new Center(
child: new Text("Demo App"),
),
),
body: new Container(
child: new Center(
child: Text("Hello world!"),
),
),
),
),
);
}
【讨论】:
将此用于条子 AppBar
SliverAppBar (
automaticallyImplyLeading: false,
elevation: 0,
brightness: Brightness.light,
backgroundColor: Colors.white,
pinned: true,
),
将其用于普通 Appbar
appBar: AppBar(
title: Text
("You decide on the appbar name"
style: TextStyle(color: Colors.black,),
elevation: 0,
brightness: Brightness.light,
backgroundColor: Colors.white,
automaticallyImplyLeading: false,
),
【讨论】:
//如果你想隐藏后退按钮使用下面的代码
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Remove Back Button'),
//hide back button
automaticallyImplyLeading: false,
),
body: Center(
child: Container(),
),
);
}
}
//如果你想隐藏后退按钮并停止弹出动作,请使用下面的代码
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: () async => false,
child: Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
//For hide back button
automaticallyImplyLeading: false,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Back'),
onPressed: () {
Navigator.pop(context);
},
),
],
)
),
),
);
}
【讨论】:
AppBar 小部件有一个名为automaticallyImplyLeading 的属性。默认值为true。如果您不想让 Flutter 自动为您构建后退按钮,那么只需将属性设为 false。
appBar: AppBar(
title: Text("YOUR_APPBAR_TITLE"),
automaticallyImplyLeading: false,
),
添加自定义后退按钮
appBar: AppBar(
title: Text("YOUR_APPBAR_TITLE"),
automaticallyImplyLeading: false,
leading: YOUR_CUSTOM_WIDGET(),
),
【讨论】:
如果导航到另一个页面。可以使用Navigator.pushReplacement()。如果您从登录导航到主屏幕,则可以使用它。或者您可以使用。 AppBar(automaticallyImplyLeading: false)
【讨论】:
SliverAppBar( automaticImplyLeading: false,}
【讨论】:
只是让它透明,按下结束时没有任何动作
AppBar(
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: Colors.white.withOpacity(0),
),
onPressed: () {},
),
【讨论】:
只需在 AppBar() 中使用 automaticImplyLeading
appBar: AppBaar(
automaticallyImplyLeading: false,
)
【讨论】:
appBar: new AppBar(title: new Text("SmartDocs SPAY"),backgroundColor: Colors.blueAccent, automaticallyImplyLeading:false,
leading: new Container(),
),
一切正常
【讨论】: