【问题标题】:Close the application on back press按下后关闭应用程序
【发布时间】:2021-10-25 14:39:24
【问题描述】:

我正在使用 Flutter 进行开发,我想在用户点击 appbar 上的后退图标时将应用发送到后台或关闭它

我使用了this answer 作为参考,但除了exit(0) 之外没有任何作用,不推荐在iOS 中使用。

我已尝试遵循其他答案的建议,但没有一个适用于 iOS。

   Navigator.of(context).pop();
   Navigator.of(context).maybePop();
   Navigator.of(context, rootNavigator: true).pop(context);
   Navigator.pushReplacementNamed(context, '/route');
   SystemNavigator.pop();
   Navigator.pop(context,true);
   SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          

对于 android,以下工作正常。

SystemNavigator.pop();

在苹果指南中我应该使用什么来关闭应用程序。

编辑

SystemNavigator.pop(); 在 iOS 中出现黑屏。

【问题讨论】:

  • 这不是 iOS 上应用导航的工作方式。用户希望在按下主页按钮或在屏幕底部向上滑动之前留在应用程序中。即使您正在开发跨设备应用程序,您也应该遵循每个平台的约定。 iOS 用户会将突然返回跳板理解为应用崩溃。
  • @Paulw11 谢谢,只是为了确定我只需要为android编写close app部分吗?

标签: android ios flutter


【解决方案1】:

试试这个

appBar: AppBar(
      leading: IconButton(
        icon: Icon(Icons.arrow_back, color: Colors.black),
        onPressed: () => Navigator.of(context).pop(),
      ), 
      title: Text("Sample"),
      centerTitle: true,
    ),

【讨论】:

猜你喜欢
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
相关资源
最近更新 更多