From abec7b5e78f067aec444d9d0efef5ada732d8507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8D=8E=E8=BD=A9?= <646903573@qq.com> Date: Fri, 18 Dec 2020 11:39:32 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=88=B0=E7=9B=B8=E5=86=8C=E7=9A=84=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/download/download.dart | 53 +++++++++++++++++++++++++++++++++ lib/util/net_util.dart | 2 +- lib/zhiying_comm.dart | 1 + pubspec.yaml | 11 +++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 lib/util/download/download.dart diff --git a/lib/util/download/download.dart b/lib/util/download/download.dart new file mode 100644 index 0000000..5c44221 --- /dev/null +++ b/lib/util/download/download.dart @@ -0,0 +1,53 @@ +import 'dart:collection'; + +import 'package:dio/dio.dart'; +import 'package:flutter/material.dart'; +import 'package:fluttertoast/fluttertoast.dart'; +import 'package:image_gallery_saver/image_gallery_saver.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:permission_handler/permission_handler.dart'; + +class Download { + static HashMap hashMap = HashMap(); + + Future fileToGallery( + String url, { + String postfix, + Function(int, int) progress, + }) async { + // 检查是否已有读写内存的权限 + bool status = await Permission.storage.isGranted; + + //判断如果还没拥有读写权限就申请获取权限 + if (status || await Permission.storage.request().isGranted) { + if (hashMap.containsKey(url)) { + return "正在下载"; + } else { + hashMap[url] = false; + } + try { + var strs = url.split('.'); + if (strs.length < 0) { + return "文件链接错误"; + } + var appDocDir = await getTemporaryDirectory(); + String savePath = appDocDir.path + "/" + DateTime.now().toIso8601String().toString() + (strs[strs.length - 1].length > 4 ? (postfix ?? "") : strs[strs.length - 1]); + await Dio().download(url, savePath, onReceiveProgress: (count, toal) { + if(progress!=null){ + progress(count, toal); + } + }); + print(savePath); + final result = await ImageGallerySaver.saveFile(savePath); + print(result); + return "保存成功"; + } catch (e) { + return "下载失败,原因:" + e.toString(); + } finally { + hashMap.remove(url); + } + } else { + return "请前往系统设置开启应用的内存读取权限再尝试"; + } + } +} diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index 82c3f6a..a0f76a1 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -55,7 +55,7 @@ class NetUtil { if (_dio == null) { var setting = await NativeUtil.getSetting(); String domain = setting['domain']; //'http://www.hairuyi.com/'; - _config(domain, proxyUrl: '192.168.0.66:8866'); + _config(domain, proxyUrl: '192.168.0.102:8866'); } return _dio; } diff --git a/lib/zhiying_comm.dart b/lib/zhiying_comm.dart index 69b38c6..29f6b73 100644 --- a/lib/zhiying_comm.dart +++ b/lib/zhiying_comm.dart @@ -36,4 +36,5 @@ export 'package:cached_network_image/cached_network_image.dart'; export 'util/photo_preview/photo_preview.dart'; export 'models/base/base_change_notifier.dart'; export 'util/parse_util.dart'; +export 'util/download/Download.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index ee1fa8e..903d8a0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -61,6 +61,17 @@ dependencies: # fluwx_no_pay: 2.3.1 # loading 动画 loading_indicator: ^1.2.0 + #权限授权 + permission_handler: + git: + url: 'http://192.168.0.138:3000/FnuoOS_Flutter_Components/permission_handler.git' + ref: '0.0.1' + #文件夹路径管理 + path_provider: ^1.4.0 + + #保存文件到相册 + image_gallery_saver: ^1.5.0 + dev_dependencies: