flutter - Flutter WillPopScope 等待弹出窗口消失
问题描述
我用 WillPopScope 包装了我的小部件树,我想显示弹出对话框,它显示用户是否退出应用程序,但同时,我必须使用 if()else{} 条件。当我使用这种条件时,我的逻辑工作正常,但没有返回任何东西。
什么是努力实现:
当有人点击后按时,它会检查条件是否为真,然后出现弹出窗口,在弹出窗口消失后,它会向 WillPopScope 返回一些布尔值,但是当弹出窗口消失时,WillPopScope 没有做任何事情。
我尝试了不同的解决方案,但对我不起作用。
这是我的代码回压
backPressed() {
if (isCollapsed) {
showDialog(
context: context,
builder: (context) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
clipBehavior: Clip.antiAliasWithSaveLayer,
backgroundColor: Colors.white,
actionsPadding: EdgeInsets.symmetric(horizontal: 12.0),
title: Text(
'Are you sure you want to close this App?',
style: TextStyle(
color: Colors.black.withOpacity(0.7),
),
),
actions: <Widget>[
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
child: Text('Exit'),
onPressed: () {
Navigator.of(context).pop(true);
print("Return True");
return true;
}),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
child: Text('Dismiss'),
onPressed: () {
Navigator.of(context).pop(false);
print("Return False");
return false;
}),
],
),
);
} else {
menuButton();
return false;
}
}
WillPopScope 代码是:
WillPopScope(
onWillPop: () async => backPressed(),
child:
解决方案
我解决了这个问题。只使用未来
这是我的解决方案
WillPopScope 代码:
WillPopScope(
onWillPop: () async {
return await backPressed();
},
child:
我的背压代码:
Future<bool> backPressed() async {
if (isCollapsed) {
bool value = await showDialog(
context: context,
builder: (context) => AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
clipBehavior: Clip.antiAliasWithSaveLayer,
backgroundColor: Colors.white,
actionsPadding: EdgeInsets.symmetric(horizontal: 12.0),
title: Text(
'Are you sure you want to close this App?',
style: TextStyle(
color: Colors.black.withOpacity(0.7),
),
),
actions: <Widget>[
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
child: Text('Exit'),
onPressed: () {
Navigator.of(context).pop(true);
print("Return True");
return true;
}),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
child: Text('Dismiss'),
onPressed: () {
Navigator.of(context).pop(false);
print("Return False");
return false;
}),
],
),
);
return value;
} else {
menuButton();
return false;
}
}
推荐阅读
- python - 变异算法效率
- python - 管理不同环境的配置
- kubernetes - 无法连接到服务器:拨打 tcp 192.168.99.100:8443:getsockopt:没有到主机的路由
- r - 如何使用 dplyr 链中数据的第一行更改 col 名称
- algorithm - Kotlin - 按类别和子类别排序的列表
- python - 而不是写很多和如何以pythonic方式缩短你的代码
- sql-server - 将 pdf 或 doc 上传到 ASP.NET MVC-5 应用程序的数据库中
- php - isset($_POST()) 在表单中返回 false
- c# - 使用 Visual Studio 部署时如何将 Azure Functions 设置为最初禁用
- java - 使用分而治之的方法找到最重复的数字?