flutter - 按下appbar后退按钮时如何确认应用退出
问题描述
当按下应用栏上的后退按钮时,我想确认退出应用。
appBar: AppBar(
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Save and Exit?'),
content: Text('are you sure you want to save and exit?'),
actions: [
FlatButton(
onPressed: () => Navigator.pop(context, false),
child: Text('No'),
),
FlatButton(
onPressed: () => Navigator.pop(context, true),
child: Text('Yes'),
),
],
);
},
);
// Navigator.pop(context);
},
),
我试过这个但没有用。我找到了一些关于如何在按下系统后退按钮时执行此操作的答案,WillPopScope
但在我的情况下它们都不起作用。
帮帮我
解决方案
我猜你可以用 Navigator.canPop(context) 检查它。它将返回真或假。在 onPressed 你可以检查它,如果它是真的你可以做 Navigator.pop(context) 否则调用 showDialog。
有文档链接 https://api.flutter.dev/flutter/widgets/Navigator/canPop.html
推荐阅读
- javascript - 查询字符串过滤
- ios - 使用苹果丢失的用户标识符登录
- aem-6 - AEM 编辑器页面变黑
- powershell - Powershell:从字符串中提取mediaid
- javascript - 在 React Native 中读取 CPU 温度
- c# - 导航 MimeKit.MimeParser
- python - Python 多处理正确完成工作,但进程仍然存在(Linux)
- algorithm - 有没有一种方法可以计算数组的某些元素是否在没有暴力破解的情况下总计为给定目标
- java - VSCode editorconfig ij_ 风格的 Java
- python - 元素未响应 Python 请求