Skip to content

Commit

Permalink
Merge branch 'fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhigq committed Mar 17, 2024
2 parents 25d1ccc + e04a7e5 commit 1182a58
Showing 1 changed file with 65 additions and 11 deletions.
76 changes: 65 additions & 11 deletions lib/utils/download.dart
Original file line number Diff line number Diff line change
@@ -1,40 +1,94 @@
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:saver_gallery/saver_gallery.dart';

class DownloadUtils {
// 获取存储权限
static requestStoragePer() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
Permission.photos,
].request();
statuses[Permission.storage].toString();
static Future<bool> requestStoragePer() async {
await Permission.storage.request();
PermissionStatus status = await Permission.storage.status;
if (status == PermissionStatus.denied ||
status == PermissionStatus.permanentlyDenied) {
SmartDialog.show(
useSystem: true,
animationType: SmartAnimationType.centerFade_otherSlide,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('提示'),
content: const Text('存储权限未授权'),
actions: [
TextButton(
onPressed: () async {
openAppSettings();
},
child: const Text('去授权'),
)
],
);
},
);
return false;
} else {
return true;
}
}

// 获取相册权限
static Future<bool> requestPhotoPer() async {
await Permission.photos.request();
PermissionStatus status = await Permission.photos.status;
if (status == PermissionStatus.denied ||
status == PermissionStatus.permanentlyDenied) {
SmartDialog.show(
useSystem: true,
animationType: SmartAnimationType.centerFade_otherSlide,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('提示'),
content: const Text('相册权限未授权'),
actions: [
TextButton(
onPressed: () async {
openAppSettings();
},
child: const Text('去授权'),
)
],
);
},
);
return false;
} else {
return true;
}
}

static Future<bool> downloadImg(String imgUrl,
{String imgType = 'cover'}) async {
try {
await requestStoragePer();
if (!await requestPhotoPer()) {
return false;
}
SmartDialog.showLoading(msg: '保存中');
var response = await Dio()
.get(imgUrl, options: Options(responseType: ResponseType.bytes));
final String imgSuffix = imgUrl.split('.').last;
String picName =
"plpl_${imgType}_${DateTime.now().toString().split('-').join()}";
"plpl_${imgType}_${DateTime.now().toString().replaceAll(RegExp(r'[- :]'), '').split('.').first}";
final SaveResult result = await SaverGallery.saveImage(
Uint8List.fromList(response.data),
quality: 60,
name: picName,
name: '$picName.$imgSuffix',
// 保存到 PiliPala文件夹
androidRelativePath: "Pictures/PiliPala",
androidExistNotSave: false,
);
SmartDialog.dismiss();
if (result.isSuccess) {
await SmartDialog.showToast('「$picName」已保存 ');
await SmartDialog.showToast('「${'$picName.$imgSuffix'}」已保存 ');
}
return true;
} catch (err) {
Expand Down

0 comments on commit 1182a58

Please sign in to comment.