flutter - showDialog 在颤动中多次显示
问题描述
我正在使用 showDialog 创建自己的 progressDialog,它可以工作,但会多次显示 progressDialog。我正在发布我的代码,我该怎么做才能根据进度值更新我在 CircularProgress Indicator 上的进度?
编辑:我也尝试过使用 StateBuilder,但得到了相同的结果。我知道问题是 showDialog 多次调用,我该如何防止它并更新进度?
调用示例函数:
void uploadAndShowProgress() {
for (double i = 0; i < 50; i += 10) {
Future.delayed(Duration(seconds: 1));
_dialog.showOrUpdateProgressDialog((i*0.01));
}
}
ProgressDialog 类:
import 'package:flutter/material.dart';
import 'package:snapik/common/utils/mediaquery.dart';
class ProgressDialog {
bool isShowing;
double currentProgress;
BuildContext context;
ProgressDialog(
{@required BuildContext context,
bool isShowing,
double currentProgress}) {
this.context = context;
this.isShowing = isShowing ??= false;
this.currentProgress = currentProgress ??= 0.0;
}
void showOrUpdateProgressDialog(double progress) {
if (!isShowing) {
isShowing = true;
}
final _dialog = await showDialog(
context: context,
barrierColor: Colors.black54,
builder: (context) => StatefulBuilder(
builder: (context, setState) {
setState(() {
currentProgress = progress;
});
return ProgressWidget(
progress: currentProgress,
);
},
),
barrierDismissible: true);
}
void hideProgressDialog() {
if (isShowing) {
Navigator.pop(context);
}
}
}
class ProgressWidget extends StatelessWidget {
final double progress;
ProgressWidget({@required this.progress});
Widget build(BuildContext context) {
return Container(
color: Colors.black54,
margin: EdgeInsets.all(8),
width: MediaQueryUtils(context).width * 0.85,
height: MediaQueryUtils(context).height * 0.15,
child: Card(
child: Row(
children: [
Flexible(
child: Container(
padding: EdgeInsets.all(8),
child: CircularProgressIndicator(
value: progress,
),
),
flex: 1,
),
Flexible(
child: Container(
margin: EdgeInsets.all(4),
child: Text(
'uploading...',
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: Theme.of(context).textTheme.headline5.fontSize),
),
),
flex: 4,
),
],
),
),
);
}
}
解决方案
推荐阅读
- c# - 如何在多个集合中进行排序
- java - 在这种情况下如何处理 nullsFirst
- ffmpeg - Ffmpeg rtmp 到 hls 的转换但是如何解决网络问题?
- java - 从 Firestore 增加值
- java - 如何在android studio中使用parcelable在活动之间传递数据
- javascript - Firebase Cloud Functions onUpdate 触发器向 Firestore 返回不可靠的时间戳
- windows - linux中多个文件或每三行的平均广告标准偏差
- python - Pandas:我可以在列的模式上使用 round() 方法吗?
- jquery - 显示和隐藏一个问题的文本功能。如何删除最初的“少显示”元素?
- java - 如何解析通过将字符串拆分为 Java 中的 Int 值而获得的数据?