计数器内部AlertDialog-颤振

我正在为我的应用程序使用AlertDialog,并在内部建立一个计数器。计数器的值不会自动更新。我使用了“ setState({})”,但没有帮助,因为它仅重建build()函数,而不重建Dialog中的int _value。

有人熟悉这个问题并帮助我吗? 谢谢

wangsdfsdfsfdgdf 回答:计数器内部AlertDialog-颤振

用户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

,

我认为最简单的方法是将StreamStreamBuilder一起使用。

本文链接:https://www.f2er.com/3141876.html

大家都在问