如何在AlertDialog小部件中更新var?

时间:2020-10-12 07:01:31

标签: flutter android-alertdialog setstate

我需要更新AlertDialog内的var百分比,我知道您不能更新我在这里使用的showDialog()内的任何东西,因为它是在另一个Widget树中构建的,但是值得一提的是很有帮助。 提前致谢。 :)

更新

uploadTask侦听器重做该功能,直到像Navigator推送到另一个屏幕一样。

我的代码

Future storageupload() async {
      await checkInternet();
      try {
        if (controller == null) {
          dialog('Error', 'Please Provide A Video Name', () => {});
        } else {
          StorageReference ref = FirebaseStorage.instance
              .ref()
              .child("Khatma 1")
              .child("Videos")
              .child(controller.text != null ? controller.text : "");
          StorageUploadTask uploadTask = ref.putFile(
              File(Variables.lastVideoPath),
              StorageMetadata(contentType: 'video/mp4'));
          uploadTask.events.listen((event) {
            if (uploadTask.isComplete) {
              AwesomeDialog(
                context: context,
                headerAnimationLoop: false,
                dialogType: DialogType.SUCCES,
                animType: AnimType.BOTTOMSLIDE,
                title: 'Uploaded',
                desc: 'Your Video Was Successfully Uploaded !',
                btnOkOnPress: () => {print('success')},
              )..show();
            } else {
              setState(() {
                percentage = event.snapshot.bytesTransferred /
                    event.snapshot.totalByteCount *
                    100;
              });
            }
          });
          return showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Uploading'),
                content: Column(
                  children: [
                    Center(
                      child: Text('Uploading .... Please Be Patient'),
                    ),
                    SizedBox(
                      height: height * 0.02,
                    ),
                    Text(percentage.toString()),
                  ],
                ),
              );
            },
          );
        }
      } catch (e) {
        print(e);
      }
    }

代码

return StatefulBuilder(
            builder: (BuildContext context, void function) => AlertDialog(
              title: Text('Uploading'),
              content: Column(
                children: [
                  Center(
                    child: Text('Uploading .... Please Be Patient'),
                  ),
                  SizedBox(
                    height: height * 0.02,
                  ),
                  Text(percentage.toString()),
                ],
              ),
            ),
          );

1 个答案:

答案 0 :(得分:0)

所有对话框都是StateLess小部件...。因此您不能调用setstate来更新其数据以显示在UI上。如果您想这样做,则可以将对话框包装在StatefullBuilder中。这将允许您调用setState。