import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nb_utils/nb_utils.dart';
import 'package:world_virtual_coin/widget/colors.dart';
import 'package:world_virtual_coin/widget/custom_text.dart';
class DioFileDownloader {
final progressNotifier = ValueNotifier<double?>(0);
static String statusValue = "0%";
static dynamic totalValue;
static dynamic receivedValue;
static var dioObject = Dio();
static Future download2(String url, String savePath) async {
// dio = dioObject;
try {
Response response = await dioObject.get(
url,
onReceiveProgress: showDownloadProgress,
//Received data with List<int>
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
validateStatus: (status) {
return status! < 500;
}),
);
print(response.headers);
File file = File(savePath);
var raf = file.openSync(mode: FileMode.write);
// response.data is List<int> type
raf.writeFromSync(response.data);
await raf.close();
} catch (e) {
print(e);
}
}
static Widget downloadStatus() {
return Stack(
alignment: Alignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
decoration: BoxDecoration(
color: white,
borderRadius: BorderRadius.circular(10.r),
),
margin: EdgeInsets.symmetric(horizontal: 20.w),
padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.h),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CustomText(
statusValue,
fontSize: 15,
isCentered: true,
textColor: const Color(0XFF161618),
),
CustomText(
"Downloading",
fontSize: 15,
isCentered: true,
textColor: colorPrimary,
),
30.height,
],
),
),
],
),
)
],
);
}
static void showDownloadProgress(received, total) {
totalValue = total;
receivedValue = received;
if (total != -1) {
statusValue = ((received / total * 100).toStringAsFixed(0) + "%");
print("DownloadStatus : " +
(received / total * 100).toStringAsFixed(0) +
"%");
// statusUpdater();
}
}
}
DioFileDownloader类用于下载pdf文件。我在另一节课上用过
CustomButton(
onPressed: () async {
// showDialog(
// context: context,
// builder: (context) => downloadStatus());
WidgetsBinding.instance
.addPostFrameCallback((timeStamp) async {
var tempDir = await getTemporaryDirectory();
String fullPath = "${tempDir.path}/dummy.pdf'";
print('full path $fullPath');
File file = File(fullPath);
if (await file.exists() && Platform.isAndroid) {
OpenFile.open(file.path);
// viewPdf();
} else {
DioFileDownloader.download2(imgUrl, fullPath);
}
setState(() {});
});
},
label: 'Save Pdf',
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomText(
statusValue,
fontSize: 15,
isCentered: true,
textColor: const Color(0XFF161618),
),
CustomText(
" Downloading",
fontSize: 15,
isCentered: true,
textColor: colorPrimary,
),
30.height,
],
),
当前,下载状态正在工作,但点击自定义按钮时,它不会显示在视图侧。这就是状态值。基本设置状态不工作。如何更新视图状态。
我的意思是下载文件的百分比。。。
以下描述,
在出版和平面设计中,Lorem ipsum是一种占位符文本,通常用于演示文档或字体的视觉形式,而不依赖于有意义的内容。Lorem ipsum可在最终副本可用之前用作占位符。