【发布时间】:2021-05-01 02:11:20
【问题描述】:
我用 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:
【问题讨论】:
-
showDialog返回一个Future- 将其用作onWillPop回调的返回值 -
能否提供代码示例?
-
return showDialog(...)- 顺便说一句,您可以将代码缩短为onWillPop: backPressed, -
是的,我的问题解决了。感谢您的帮助
标签: flutter