【问题标题】:flutter alert dialog not updating from outside the function颤振警报对话框未从函数外部更新
【发布时间】:2021-09-02 03:30:59
【问题描述】:

我想在警报对话框中制作下载进度消息,但状态未在模态框内更新 这是使用dio的下载功能

downloadBook() async {
    Directory tempDir = await getTemporaryDirectory();
    var response = await dio.download(widget.bookModel.acf.bookLink,
        tempDir.path + '/books/' + widget.bookModel.title.rendered + '.epub',
        options: Options(
          responseType: ResponseType.bytes,
        ), onReceiveProgress: (actualbytes, totalbytes) {
      var percenatge = actualbytes / totalbytes * 100;
      _percentageBar = percenatge / 100;
      setState(() {
        downloadMessage = 'Downloading... ${percenatge.floor()} %';
      });
    });
  }

这是警报对话功能

void _showDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: new Text("Alert!!"),
          content: new Text(downloadMessage),
          actions: <Widget>[
            new FlatButton(
              child: new Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

我在文本按钮内拨打了这样的电话,但消息停留在 0% 且未更新;

onPressed: () {
        // _showDialog(context);
        downloadBook();
        showDialog(
          context: context,
          builder: (context) {
            String contentText = "Content of Dialog";
            return StatefulBuilder(
              builder: (context, setState) {
                return AlertDialog(
                  title: new Text("Alert!!"),
                  content: new Text(downloadMessage),
                  actions: <Widget>[
                    new FlatButton(
                      child: new Text("OK"),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                );
              },
            );
          },
        );
      },

【问题讨论】:

    标签: flutter flutter-alertdialog


    【解决方案1】:

    正如我从您的问题中了解到的那样,您正在尝试为您的警报对话框设置不同的状态,因此我将更新您有关 alertDialog 的代码,之后您可以做您想做的事情。

    showDialog(
      context: context,
      builder: (context) {
        String contentText = "Content of Dialog";
        return StatefulBuilder(
          builder: (context, setState) {
            return AlertDialog(
              title: new Text("Alert!!"),
              content: new Text(downloadMessage),
              actions: <Widget>[
                new FlatButton(
                  child: new Text("OK"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            );
          },
        );
      },
    );
    

    所以你可以使用 setState 来实现你想要的任何进展。

    【讨论】:

    • 不工作状态卡住不改变...我更新代码
    • 正如我所解释的,警报现在有自己的状态,因此您可以使用 setState 以防万一您想更新它。 @FLASHTUBE
    【解决方案2】:

    你可以使用

    StatefullBuilder

    小部件顶部的警报对话框并通过

    设置状态

    到你调用它的地方。

    这样你就可以从外部访问State。

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 2019-10-27
      • 2021-09-25
      • 1970-01-01
      • 2020-06-02
      • 2022-06-29
      • 2019-09-02
      • 1970-01-01
      相关资源
      最近更新 更多