Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

flutter setState not updating showCupertinoDialog

setState is not working in showCupertinoDialog. When i get async data i want to update the progress bar which is inside dialog. _progress value is updated and setstate is called successfully but dialog is not updating. I am using statefulBuilder.

 showCupertinoDialog(
      context: context,
      builder: (BuildContext context) => CupertinoAlertDialog(
        title: Text("Preparing..."),
        content: StatefulBuilder(builder: (context, StateSetter setState) {
          print('ssetstatte');
          return UploadIndicator(_progress);
        }),
        actions: [
          CupertinoDialogAction(
            onPressed: () {
              Navigator.of(context).pop();
            },
            isDefaultAction: true,
            child: Text("Cancel"),
          ),
        ],
      ),
    ).whenComplete(() {});



_uploadTask.snapshotEvents.listen((event) {
     
      _progress =
          event.bytesTransferred.toDouble() / event.totalBytes.toDouble();
/*THIS IS NOT UPDATING DIALOG*/
      setState(() {
        _progress = double.parse((_progress * 100).toStringAsFixed(2));
       
      });
});
question from:https://stackoverflow.com/questions/65882656/flutter-setstate-not-updating-showcupertinodialog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use StatefulBuilder to use setState inside Dialog and update Widgets only inside of it.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...