Navigation 颤振移除appbar上的后退按钮

Navigation 颤振移除appbar上的后退按钮,navigation,flutter,Navigation,Flutter,我想知道,当您使用Navigator.pushNamed转到另一页时,是否有人知道如何删除颤振应用程序中显示在appBar上的后退按钮。我不希望它出现在这个结果页面上的原因是,它来自导航,我希望用户使用注销按钮,以便会话重新开始。您可以通过将一个空的新容器()作为参数传递给用户来删除后退按钮 如果您发现自己正在这样做,您可能不希望用户能够按设备的后退按钮返回到先前的路线。不要调用pushNamed,而是尝试调用以使先前的路由消失 功能pushReplacementNamed将删除backback

我想知道,当您使用
Navigator.pushNamed
转到另一页时,是否有人知道如何删除颤振应用程序中显示在
appBar
上的后退按钮。我不希望它出现在这个结果页面上的原因是,它来自导航,我希望用户使用
注销
按钮,以便会话重新开始。

您可以通过将一个空的
新容器()
作为参数传递给用户来删除后退按钮

如果您发现自己正在这样做,您可能不希望用户能够按设备的后退按钮返回到先前的路线。不要调用
pushNamed
,而是尝试调用以使先前的路由消失

功能
pushReplacementNamed
将删除backbackback中的上一个路由,并将其替换为新路由

后者的完整代码示例如下

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(),
      },
    );
  }
}

移除AppBar中的“后退”按钮的一种简单方法是将
automaticallyImplyLeading
设置为
false

appBar: AppBar(
  title: Text("App Bar without Back Button"),
  automaticallyImplyLeading: false,
),
appBar: AppBar(
  title: Text("YOUR_APPBAR_TITLE"), 
  automaticallyImplyLeading: false,
),

我认为解决办法如下

实际上,你可以:

  • 不想显示丑陋的后退按钮(:]),因此选择:
    AppBar(…,自动加载:false…)

  • 不希望用户返回-替换当前视图-从而选择:
    Navigator.pushReplacementNamed(#######这里是您的routename#)

  • 不希望用户返回—将某个视图替换回堆栈中—从而使用:
    Navigator.pushName和removeUntil(#########,f(路线)→布尔)
    其中f是一个函数,当满足要保留在堆栈中的最后一个视图时(就在新视图之前),返回
    true

  • 不希望用户返回-曾经-使用以下命令完全清空导航器堆栈:
    Navigator.pushName和removeUntil(上下文,#######,())=>false)

干杯


它工作正常

自动加载:

这将检查是否要在应用程序栏上应用back小部件(前导小部件)。 如果automaticallyImplyLeading为false,则自动为标题提供空格;如果leading小部件为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小部件有一个名为
automaticallyImplyLeading
的属性。默认情况下,它的值为
true
。如果您不想让flatter自动为您构建back按钮,那么只需将属性设置为false即可

appBar: AppBar(
  title: Text("App Bar without Back Button"),
  automaticallyImplyLeading: false,
),
appBar: AppBar(
  title: Text("YOUR_APPBAR_TITLE"), 
  automaticallyImplyLeading: false,
),
添加自定义后退按钮的步骤

appBar: AppBar(
  title: Text("YOUR_APPBAR_TITLE"), 
  automaticallyImplyLeading: false,
  leading: YOUR_CUSTOM_WIDGET(),
),

//如果您想隐藏后退按钮,请使用下面的代码

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);
              },
            ),
          ],
        )
    ),
  ),
);
 }

//如果您想隐藏后退按钮并停止弹出操作,请使用下面的代码

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);
              },
            ),
          ],
        )
    ),
  ),
);
 }
class SecondScreen扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新的Willposcope(
onWillPop:()async=>false,
孩子:脚手架(
appBar:appBar(
标题:文本(“第二屏”),
//用于隐藏后退按钮
自动嵌入:false,
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
升起的按钮(
子项:文本('Back'),
已按下:(){
Navigator.pop(上下文);
},
),
],
)
),
),
);
}

如果导航到另一个页面。可以使用
导航器。pushReplacement()
可以在从登录导航到主屏幕时使用。也可以使用。

AppBar(automaticallyImplyLeading:false)

将其用于切片AppBar

SliverAppBar (
        automaticallyImplyLeading: false,
        elevation: 0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        pinned: true,
      ),
 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,

),
将其用于普通Appbar

SliverAppBar (
        automaticallyImplyLeading: false,
        elevation: 0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        pinned: true,
      ),
 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,

),

只需使其透明,按End时无需执行任何操作

AppBar(
            leading: IconButton(
              icon: Icon(
                Icons.arrow_back,
                color: Colors.white.withOpacity(0),
              ),
              onPressed: () {},
            ),

是的,我的命令弄错了。我会尝试一下,谢谢你的帮助。@Collin,pushReplacementNamed似乎并没有消除使用系统返回箭头返回的可能性。@Collin Jackson,确实
pushReplacementNamed()
处理以前的屏幕小部件(以及所有依赖的数据和状态)?@Jackpap这是因为如果有以前的路线,它确实会显示箭头。如果这是唯一的路线,那么就没有什么可返回的了。在您的情况下,请使用空容器()方法。空容器方法似乎会产生一个空格,其中的“后退”按钮可能会出现,因此Appbar标题会稍微移动。仍然不是一个理想的方法。我们需要提供前导:new container()标记这就是我要寻找的答案!pushReplacementNamed()对我不起作用,但用户返回才是起作用的!谢谢!这确实是最好的答案。谢谢,我不得不使用“pushReplacementNamed”而不是“popAndPushNamed”虽然这很容易实现,但对于给定的场景,
Navigator.pushReplacementNamed
是正确的解决方案。您建议的是一种解决方案,如果应用于所有场景,最终可能会推断出错误的行为,例如有人希望
AppBar
继续暗示主导行为(即:返回导航按钮)虽然它删除了后退箭头图标,但你仍然可以通过按下设备的后退按钮返回。如果我能再往下看一个答案,我就能找到问题的实际答案。谢谢你,在Android上删除后退按钮的最佳方法是什么,这样安卓用户就必须使用设备的后退按钮才能返回o后退,但iOS用户看到AppBar后退按钮?正是我要找的。谢谢,伙计:)