完成app更新时显示下载进度条功能

This commit is contained in:
胡天 2023-07-26 16:11:31 +08:00
parent 1361e36be3
commit 743046afe2
2 changed files with 61 additions and 3 deletions

View File

@ -166,6 +166,7 @@ class ApplicationController extends GetxController {
await Future.delayed(Duration(seconds: 3), () async {
if (ConfigService.isIOS == false &&
await Permission.storage.isGranted == false) {
//
await [Permission.storage].request();
}
if (await Permission.storage.isGranted) {

View File

@ -1,7 +1,8 @@
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get.dart' hide Response;
import 'package:install_plugin/install_plugin.dart';
import 'package:news_getx/data/model/app.dart';
import 'package:news_getx/data/repository/app_repository.dart';
@ -10,6 +11,38 @@ import 'package:news_getx/modules/widgets/toast.dart';
import 'package:news_getx/utils/http.dart';
import 'package:path_provider/path_provider.dart';
class DownloadController extends GetxController {
//
var _progressValue = 0.0.obs;
double get progressValue => _progressValue.value;
set progressValue(double value) {
_progressValue.value = value;
}
Future<Response> download(String fileUrl, String fullPath) async {
progressValue = 0;
return await HttpUtil().dio.download(
fileUrl,
fullPath,
options: Options(
receiveTimeout: Duration(minutes: 5),
),
onReceiveProgress: (count, total) {
final value = count / total;
if (progressValue != value) {
if (progressValue < 1.0) {
progressValue = count / total;
} else {
progressValue = 0.0;
}
}
},
);
}
}
/// App
class AppUpdateUtil {
static AppUpdateUtil _instance = AppUpdateUtil._internal();
@ -84,9 +117,33 @@ class AppUpdateUtil {
break;
}
}
if (!isFileFound) {
await HttpUtil().dio.download(fileUrl, fullPath);
final DownloadController downloadController = Get.put(
DownloadController());
Get.dialog(
AlertDialog(
title: Text('文件下载中'),
content: Obx(() {
final percentage = downloadController.progressValue * 100;
if (percentage == 100) {
Get.back();
}
return Row(
children: [
Text('正在下载'),
Expanded(child: LinearProgressIndicator(
value: downloadController.progressValue)),
Text(
"${percentage.toStringAsFixed(0)}%"),
],
);
}),
),
barrierDismissible: false,
);
await downloadController.download(fileUrl, fullPath);
}
//
await InstallPlugin.installApk(fullPath);