如何在 Flutter 中刷新 AlertDialog? [英] How to refresh an AlertDialog in Flutter?
本文介绍了如何在 Flutter 中刷新 AlertDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我有一个带有 IconButton
的 AlertDialog
.用户可以点击 IconButton,每次点击我有两种颜色.问题是我需要关闭AlertDialog并重新打开才能看到颜色图标的状态变化.我想在用户单击时立即更改 IconButton 颜色.
Currently, I have an AlertDialog
with an IconButton
. The user can click on the IconButton, I have two colors for each click. The problem is that I need to close the AlertDialog and reopen to see the state change of the color icon. I want to change the IconButton color immediately when the user clicks it.
代码如下:
bool pressphone = false;
//....
new IconButton(
icon: new Icon(Icons.phone),
color: pressphone ? Colors.grey : Colors.green,
onPressed: () => setState(() => pressphone = !pressphone),
),
推荐答案
使用 StatefulBuilder 在 Dialog 内部使用 setState 并仅更新其中的 Widgets.
Use StatefulBuilder to use setState inside Dialog and update Widgets only inside of it.
showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: Text(contentText),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text("Cancel"),
),
TextButton(
onPressed: () {
setState(() {
contentText = "Changed Content of Dialog";
});
},
child: Text("Change"),
),
],
);
},
);
},
);
这篇关于如何在 Flutter 中刷新 AlertDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文