我正在为我的应用程序使用AlertDialog,并在内部建立一个计数器。计数器的值不会自动更新。我使用了“ setState({})”,但没有帮助,因为它仅重建了build()函数,而不重建Dialog中的int _value。
有人熟悉这个问题并帮助我吗?谢谢
用户StatefulBuilder
返回AlertDialog
。
showDialog(
context: context,
builder: (context) {
String contentText = "Initial Content";
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title Here"),
content: Text(contentText),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text("Cancel"),
),
FlatButton(
onPressed: () {
setState(() {
contentText = "Changed!";
});
},
child: Text("Change Now"),
),
],
);
},
);
},
);
下面的代码示例解决了我的问题。正如@ anmol.majhail所说的那样,我在一个新类中编写了AlertDialog,然后由build()函数调用。为了调用该类并显示AlertDialog,我曾经这样做。
Widget createItem() {
return new FloatingActionButton(
elevation: 4.0,
child: const Icon(Icons.create),
onPressed: () {
showDialog(
context: context,
child: new ItemAlertView()
);
},
);
}
此示例帮助了我(与第59行相比)。 https://gist.github.com/harshapulikollu/5461844368e95c6d3a38fffe72f03eee
我认为,最简单的方法是将Stream
与StreamBuilder
结合使用。