【发布时间】:2021-01-25 10:09:44
【问题描述】:
setState 在 showCupertinoDialog 中不起作用。当我获得异步数据时,我想更新对话框内的进度条。 _progress 值已更新,setstate 已成功调用,但对话框未更新。我正在使用 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));
});
});
【问题讨论】:
-
问题是调用
setState没有使用StatefulBuilder的setState并且可能调用StatefulWidget的。您最好让一个对象实现ChangeNotifier并在对话框中监听该对象的更改
标签: flutter setstate showdialog