Rob*_*ert 52 navigation flutter
我想知道,如果有人知道有一种方法可以删除appBar当你用到Navigator.pushNamed另一个页面时显示在一个颤动的应用程序中的后退按钮.我不希望它在这个结果页面上的原因是它来自导航,我希望用户改为使用该logout按钮,以便会话重新开始.
Fab*_*ese 138
我相信解决方案如下
你实际上是:
不想显示丑后退按钮(:]),并由此去:
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);
干杯
Col*_*son 83
没有方法叫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(),
},
);
}
}
Run Code Online (Sandbox Code Playgroud)
Jac*_*pap 72
删除AppBar中后退按钮的一种简单方法是设置automaticallyImplyLeading为false.
appBar: AppBar(
title: Text("App Bar without Back Button"),
automaticallyImplyLeading: false,
),
Run Code Online (Sandbox Code Playgroud)
Mos*_*oud 22
只需在 AppBar() 中使用automaticImplyLeading
appBar: AppBaar(
automaticallyImplyLeading: false,
)
Run Code Online (Sandbox Code Playgroud)
Ken*_*usu 11
将此用于条子 AppBar
SliverAppBar (
automaticallyImplyLeading: false,
elevation: 0,
brightness: Brightness.light,
backgroundColor: Colors.white,
pinned: true,
),
Run Code Online (Sandbox Code Playgroud)
将此用于普通 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,
),
Run Code Online (Sandbox Code Playgroud)
jit*_*555 10
Just want to add some description over @Jackpap answer:
automaticallyImplyLeading:
这检查我们是否要在应用程序栏上应用后退小部件(前导小部件)。如果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!"),
),
),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
// 如果你想隐藏后退按钮使用下面的代码
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(),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
// 如果要隐藏后退按钮并停止弹出操作,请使用以下代码
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);
},
),
],
)
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
AppBar 小部件有一个名为 的属性automaticallyImplyLeading。默认情况下,它的值为true。如果您不想 flutter 自动为您构建后退按钮,那么只需创建 property false。
appBar: AppBar(
title: Text("YOUR_APPBAR_TITLE"),
automaticallyImplyLeading: false,
),
Run Code Online (Sandbox Code Playgroud)
添加自定义后退按钮
appBar: AppBar(
title: Text("YOUR_APPBAR_TITLE"),
automaticallyImplyLeading: false,
leading: YOUR_CUSTOM_WIDGET(),
),
Run Code Online (Sandbox Code Playgroud)
如果导航到另一个页面。 Navigator.pushReplacement()可以使用。如果您从登录导航到主屏幕,则可以使用它。或者你可以使用 .
AppBar(automaticallyImplyLeading: false)
| 归档时间: |
|
| 查看次数: |
37216 次 |
| 最近记录: |