如何在flutter中停用或覆盖appBar中的箭头后退按钮?

Posted

技术标签:

【中文标题】如何在flutter中停用或覆盖appBar中的箭头后退按钮?【英文标题】:How to deactivate or override the arrow back button in the appBar in flutter? 【发布时间】:2020-03-13 19:55:32 【问题描述】:

有没有办法在特定页面上停用箭头返回按钮? 我建立了一个启动画面,每当启动画面导航到主页时,在主页应用程序栏中,返回的箭头会显示,如何停用它或清除返回上一页的箭头(欢迎屏幕)。

Widget build(BuildContext context) 
return WillPopScope(
onWillPop: () return Future.value();
, // async => true,
child: Scaffold(
  appBar: AppBar(
    title: Text("Application!"),
    elevation: 20,
    centerTitle: true,
    actions: <Widget>[
      IconButton(
        icon: Icon(Icons.exit_to_app),
        onPressed: () 
          Navigator.pop(context);
        ,
      ),
      PopupMenuButton(
        icon: Icon(Icons.more_vert),
        itemBuilder: (context) => [
          PopupMenuItem(
            child: Text("Home"),
          ),
          PopupMenuItem(
            child: Text("Hire"),
          ),
          PopupMenuItem(
            child: Text("Settings"),
          ),
          PopupMenuItem(
            child: Text("About"),
          ),
        ],
      ),
    ],
  ),
  body: Center(
    child: Container(
      child: Text("Chech out my new Mobile Applications!")
    ),
  ),
),
);

【问题讨论】:

【参考方案1】:

将您的小部件包装在 WillPopScope 中并onWillPop 属性中返回一个错误的 Future

    @override
    Widget build(BuildContext context) 
      return WillPopScope(
        onWillPop: () => Future.value(false),
        child: Scaffold(
          appBar: AppBar(
            title: const Text("Home Page"),
          ),
          body: Center(
            child: const Text("Home Page"),
          ),
        ),
      );
    

参考号:how to disable phone back press in flutter

【讨论】:

【参考方案2】:

flutter remove back button on appbar

您可能会发现您的答案已在此问题中得到解答

【讨论】:

以上是关于如何在flutter中停用或覆盖appBar中的箭头后退按钮?的主要内容,如果未能解决你的问题,请参考以下文章