flutter - 如何在 Global Snackbar 中制作进度指示器(下载百分比)?
问题描述
我正在尝试实现一个全局 downloadFile() 函数和一个全局小吃栏,以便即使在导航到另一个小部件后,小吃栏和下载也能继续工作。问题是下载进度(下载百分比)根本没有更新。这是 CustomSnackBar
class CustomSnackBar {
static bool isDownloading = false;
static String progress = "0%";
CustomSnackBar._();
static Future<void> downloadFile(
BuildContext context, String fileUrl, fileName) async {
Dio dio = Dio();
isDownloading = true;
try {
var dir = await getApplicationDocumentsDirectory();
await dio
.download(fileUrl, "${dir.path}/notifications_files/$fileName.pdf",
onReceiveProgress: (rec, total) {
progress = '${((rec / total) * 100).toStringAsFixed(0)} %';
buildErrorSnackbar(context, "empty");
});
isDownloading = false;
} catch (e) {
print(e);
}
}
static buildErrorSnackbar(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: const Duration(days: 100),
backgroundColor: kPrimaryColor,
content: snacBarDownloading(progress),
action: SnackBarAction(
textColor: kWhiteColor,
label: 'Dismiss',
onPressed: () {},
),
),
);
}
}
Widget snacBarDownloading(String progress) {
// print(progress);
return Row(children: [
const SizedBox(
width: 25,
height: 25,
child: CircularProgressIndicator(color: kWhiteColor)),
const SizedBox(
width: 10,
),
Text("Downloading $progress ...",
style: const TextStyle(color: kWhiteColor))
]);
}
这就是我调用下载功能的地方
return InkWell(
onLongPress: () {
if (!isDownloading &&
widget.notificationList[index].fileUrl != null &&
widget.notificationList[index].fileUrl!.isNotEmpty) {
CustomSnackBar.downloadFile(
context,
widget.notificationList[index].fileUrl!,
widget.notificationList[index].serviceId!);
}
},
解决方案
推荐阅读
- vue.js - CoreUi - 为什么 nightwatch 不能运行 e2e 测试?
- javascript - 在 jQuery 变量中传递参数
- go - 如何在 gin-gonic 和 nrgin 中的 New Relic 中捕获自定义错误消息
- zend-framework2 - EntityManager persist 期望参数 1 是一个实体对象,NULL 给定 zend 教义
- android - 为什么我的图标在安卓设备上被拉伸?
- javascript - Firebase Auth:当用户密码/电子邮件不正确时,不会出现错误消息。快速解决?
- kotlin - Kotlin 多平台项目依赖项
- python-3.x - 在列表中查找相同的值并删除重复的错误输出
- python - matplotlib/mplot3d scatterplot 尊重掩蔽,但surfaceplot 没有 - 为什么?
- c++ - C++ 的文件 I/O