【问题标题】:Counter Inside AlertDialog - Flutter计数器内部 AlertDialog - Flutter
【发布时间】:2019-11-07 20:13:50
【问题描述】:

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

任何熟悉这个问题并帮助我的人? 谢谢

【问题讨论】:

标签: flutter android-alertdialog counter setstate


【解决方案1】:

下面的代码示例解决了我的问题。正如@anmol.majhail 很好地提到的那样,a 在一个新类中编写了我的 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

【讨论】:

    【解决方案2】:

    用户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"),
                ),
              ],
            );
          },
        );
      },
    );
    

    【讨论】:

    • 非常感谢您的帮助。不幸的是,它对我不起作用。我找到了另一个解决方案,看看:)
    【解决方案3】:

    在我看来,最干净的方法是使用 StreamStreamBuilder

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多