From 743046afe28ca5ffe1bf27fb7f095e21e76953ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=A4=A9?= Date: Wed, 26 Jul 2023 16:11:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90app=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=97=B6=E6=98=BE=E7=A4=BA=E4=B8=8B=E8=BD=BD=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E6=9D=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/application_controller.dart | 1 + lib/utils/update.dart | 63 ++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/lib/modules/application/application_controller.dart b/lib/modules/application/application_controller.dart index d548b0c..c21c66f 100644 --- a/lib/modules/application/application_controller.dart +++ b/lib/modules/application/application_controller.dart @@ -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) { diff --git a/lib/utils/update.dart b/lib/utils/update.dart index 1fa3c03..8b241a1 100644 --- a/lib/utils/update.dart +++ b/lib/utils/update.dart @@ -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 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);