小编典典

扑出移除应用程序栏上的后退按钮

flutter

我想知道,是否有人知道一种用来删除appBar当您Navigator.pushNamed转到另一页时在flutter应用程序中显示的后退按钮的方法。我不希望它出现在此结果页面上的原因是它来自导航,我希望用户改用logout按钮,以便会话重新开始。


阅读 252

收藏
2020-08-13

共1个答案

小编典典

您可以通过将空白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(),
      },
    );
  }
}
2020-08-13