gpt4 book ai didi

flutter - 如何使用 navigator.pop 返回 bool 值?

转载 作者:行者123 更新时间:2023-12-03 02:46:40 28 4
gpt4 key购买 nike

我试图从对话框中返回一个 bool 值,但我不明白为什么该值没有根据需要返回。我尝试过作为 future 值返回,并在弹出对话框后将这些值与上下文一起返回。

final bool delete = await _showDialog();
print(delete);

Future<bool> _showDialog() {
bool result;

showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Appointment'),
content: Text(
'Are you sure? \nThis action cannot undo.',
style: TextStyle(
color: Colors.red,
fontSize: 20
),
),
actions: <Widget>[
FlatButton(
color: Colors.blue,
child: Text(
'CANCEL',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
setState(() => result = false);
//print(result);
Navigator.pop(context, result);
return Future.value(result);
},
),
SizedBox(
width: 50,
),
FlatButton(
color: Colors.red,
child: Text(
'CONFIRM',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
setState(() => result = true);
//print(result);
Navigator.pop(context, result);
return Future.value(result);
},
)
],
);
}
);
}

最佳答案

这是一个基于您的代码的工作示例:

@override
Widget build(BuildContext context) {
return Container(
child: RaisedButton(onPressed: () async {
bool delete = await _showDialog(context);
print(delete);
})
);
}

Future<bool> _showDialog(context) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Appointment'),
content: Text(
'Are you sure? \nThis action cannot undo.',
style: TextStyle(
color: Colors.red,
fontSize: 20
),
),
actions: <Widget>[
FlatButton(
color: Colors.blue,
child: Text(
'CANCEL',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
Navigator.pop(context, false);
},
),
SizedBox(
width: 50,
),
FlatButton(
color: Colors.red,
child: Text(
'CONFIRM',
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: () {
Navigator.pop(context, true);
},
)
],
);
}
);
}

关于flutter - 如何使用 navigator.pop 返回 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60222500/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com