【问题标题】:Back button doesn't close the app from home page后退按钮不会从主页关闭应用程序
【发布时间】:2019-07-15 10:03:12
【问题描述】:

我有一个颤振应用程序,打开它时会打开初始屏幕,然后是主页。

当我在主页并单击 Android 的后退按钮时,没有任何反应,因为我使用了此代码:

new WillPopScope(
    onWillPop: () async => false,
    child:Directionality(
        textDirection: direction,
        child:...
   )
)

但如果我从主页单击 Android 中的后退按钮,我希望应用程序退出。

有没有办法做到这一点?

【问题讨论】:

标签: android flutter


【解决方案1】:

这是预期行为,因为 WillPopScope 会覆盖导航。

你应该自己实现导航

示例:

Future<bool> _exitApp(BuildContext context) {
  return showDialog(
        context: context,
        child: new AlertDialog(
          title: new Text('Do you want to exit this application?'),
          content: new Text('We hate to see you leave...'),
          actions: <Widget>[
            new FlatButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: new Text('No'),
            ),
            new FlatButton(
              onPressed: () => Navigator.of(context).pop(true),
              child: new Text('Yes'),
            ),
          ],
        ),
      ) ??
      false;
}
new WillPopScope(
    onWillPop: () async => _exitApp(context),
    child:Directionality(
        textDirection: direction,
        child:...
   )
)

Reference.

EDIT退出应用可以添加import 'dart:io'; abd使用exit(0)函数。

【讨论】:

  • 我不需要对话框 .. 我想在点击时关闭应用程序 .. 试图像这样放置它但它不起作用:onWillPop: () async => Navigator。 of(context).pop(true),
  • Navigator.of(context).pop() 你试过用这个背压吗?
  • 如果我点击返回会显示黑屏
  • 尝试使用exit(0) 而不是Navigator.of(context).pop()
  • 我收到了这个错误:没有为类“MainPageState”定义方法“exit”。
猜你喜欢
  • 2012-09-23
  • 2020-01-12
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
相关资源
最近更新 更多