feat(upload_image): 支持微信资源选择及图片压缩上传
- 集成 wechat_assets_picker 库实现图片和视频选择 - 使用 flutter_image_compress 库对选中图片进行压缩处理 - 替换原有 ImagePicker,实现多图片和视频的资源选取功能 - 压缩后图片保存为临时文件再进行上传 - 优化上传过程,兼容视频和图片的不同处理流程 - 简化回调调用代码,提升代码可读性与一致性
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter_common/upload_image/ossUtil.dart';
|
import 'package:flutter_common/upload_image/ossUtil.dart';
|
||||||
|
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
|
||||||
// import 'package:images_picker/images_picker.dart';
|
// import 'package:images_picker/images_picker.dart';
|
||||||
// import 'package:images_picker/images_picker.dart';
|
// import 'package:images_picker/images_picker.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
||||||
|
|
||||||
class UploadImagesTool {
|
class UploadImagesTool {
|
||||||
static uploadImagesTool({
|
static uploadImagesTool({
|
||||||
@@ -136,9 +139,7 @@ class UploadImagesTool {
|
|||||||
)
|
)
|
||||||
: CupertinoActionSheet(
|
: CupertinoActionSheet(
|
||||||
title: const Text('上传图片'),
|
title: const Text('上传图片'),
|
||||||
message: (max == null || max == 0)
|
message: (max == null || max == 0) ? null : Text('请选择上传方式\n相册最多${max ?? 9}张'),
|
||||||
? null
|
|
||||||
: Text('请选择上传方式\n相册最多${max ?? 9}张'),
|
|
||||||
actions: isAddOtherWidget != null
|
actions: isAddOtherWidget != null
|
||||||
? <Widget>[
|
? <Widget>[
|
||||||
isAddOtherWidget,
|
isAddOtherWidget,
|
||||||
@@ -152,8 +153,7 @@ class UploadImagesTool {
|
|||||||
policy: policy ?? '',
|
policy: policy ?? '',
|
||||||
callback: callback ?? '',
|
callback: callback ?? '',
|
||||||
signature: signature ?? '',
|
signature: signature ?? '',
|
||||||
chooseImages: (list) =>
|
chooseImages: (list) => chooseImages?.call(list),
|
||||||
chooseImages?.call(list),
|
|
||||||
);
|
);
|
||||||
Get.back();
|
Get.back();
|
||||||
},
|
},
|
||||||
@@ -169,8 +169,7 @@ class UploadImagesTool {
|
|||||||
policy: policy ?? '',
|
policy: policy ?? '',
|
||||||
callback: callback ?? '',
|
callback: callback ?? '',
|
||||||
signature: signature ?? '',
|
signature: signature ?? '',
|
||||||
chooseImages: (list) =>
|
chooseImages: (list) => chooseImages?.call(list),
|
||||||
chooseImages?.call(list),
|
|
||||||
);
|
);
|
||||||
Get.back();
|
Get.back();
|
||||||
},
|
},
|
||||||
@@ -187,8 +186,7 @@ class UploadImagesTool {
|
|||||||
policy: policy ?? '',
|
policy: policy ?? '',
|
||||||
callback: callback ?? '',
|
callback: callback ?? '',
|
||||||
signature: signature ?? '',
|
signature: signature ?? '',
|
||||||
chooseImages: (list) =>
|
chooseImages: (list) => chooseImages?.call(list),
|
||||||
chooseImages?.call(list),
|
|
||||||
);
|
);
|
||||||
Get.back();
|
Get.back();
|
||||||
},
|
},
|
||||||
@@ -205,8 +203,7 @@ class UploadImagesTool {
|
|||||||
callback: callback ?? '',
|
callback: callback ?? '',
|
||||||
signature: signature ?? '',
|
signature: signature ?? '',
|
||||||
isShowLoading: isShowLoading,
|
isShowLoading: isShowLoading,
|
||||||
chooseImages: (list) =>
|
chooseImages: (list) => chooseImages?.call(list),
|
||||||
chooseImages?.call(list),
|
|
||||||
);
|
);
|
||||||
Get.back();
|
Get.back();
|
||||||
},
|
},
|
||||||
@@ -265,7 +262,11 @@ class UploadImagesTool {
|
|||||||
bool? isShowLoading,
|
bool? isShowLoading,
|
||||||
}) async {
|
}) async {
|
||||||
if (isVideo == true) {
|
if (isVideo == true) {
|
||||||
XFile? video = await ImagePicker().pickVideo(source: ImageSource.gallery);
|
final List<AssetEntity>? result = await AssetPicker.pickAssets(
|
||||||
|
Get.context!,
|
||||||
|
pickerConfig: AssetPickerConfig(maxAssets: 1, requestType: RequestType.video),
|
||||||
|
);
|
||||||
|
final File? video = await result?.first.file;
|
||||||
String path = await saveNetworkImgGallery(
|
String path = await saveNetworkImgGallery(
|
||||||
video?.path ?? '',
|
video?.path ?? '',
|
||||||
fileType: 'mp4',
|
fileType: 'mp4',
|
||||||
@@ -277,11 +278,32 @@ class UploadImagesTool {
|
|||||||
signature: signature ?? '',
|
signature: signature ?? '',
|
||||||
);
|
);
|
||||||
chooseImages?.call([path]);
|
chooseImages?.call([path]);
|
||||||
print('video path ============ $path');
|
|
||||||
} else {
|
} else {
|
||||||
List<XFile>? images = await ImagePicker().pickMultiImage();
|
final List<AssetEntity>? result = await AssetPicker.pickAssets(
|
||||||
|
Get.context!,
|
||||||
|
pickerConfig: AssetPickerConfig(maxAssets: max ?? 50),
|
||||||
|
);
|
||||||
|
/// 临时存储选中的图片
|
||||||
|
final List<XFile> selectedFiles = [];
|
||||||
|
if (result != null && result.isNotEmpty) {
|
||||||
|
for (int i = 0; i < result.length; i++) {
|
||||||
|
final File? file = await result[i].file;
|
||||||
|
if (file != null) {
|
||||||
|
/// 获取文件扩展名
|
||||||
|
final String extension = file.absolute.path.split('.').last;
|
||||||
|
// 压缩并保存到临时文件
|
||||||
|
final XFile? compressedFile = await FlutterImageCompress.compressAndGetFile(
|
||||||
|
file.absolute.path, '${file.parent.path}/${DateTime.now().millisecondsSinceEpoch}_compressed.$extension',
|
||||||
|
quality: 80, minWidth: 1920, minHeight: 1080);
|
||||||
|
if (compressedFile != null) {
|
||||||
|
selectedFiles.add(compressedFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// 上传选中的图片
|
||||||
List<String> list = [];
|
List<String> list = [];
|
||||||
for (var element in images) {
|
for (var element in selectedFiles) {
|
||||||
String path = await saveNetworkImgGallery(
|
String path = await saveNetworkImgGallery(
|
||||||
element.path,
|
element.path,
|
||||||
oSSAccessKeyId: oSSAccessKeyId ?? '',
|
oSSAccessKeyId: oSSAccessKeyId ?? '',
|
||||||
@@ -333,7 +355,7 @@ class UploadImagesTool {
|
|||||||
String? signature,
|
String? signature,
|
||||||
String? ossDirectory,
|
String? ossDirectory,
|
||||||
String? ossHost,
|
String? ossHost,
|
||||||
bool? isShowLoading,
|
bool? isShowLoading,
|
||||||
}) async {
|
}) async {
|
||||||
String string = await UploadOss.upload(
|
String string = await UploadOss.upload(
|
||||||
path,
|
path,
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ dependencies:
|
|||||||
image_editor_plus: ^1.0.6
|
image_editor_plus: ^1.0.6
|
||||||
path_provider: ^2.1.5
|
path_provider: ^2.1.5
|
||||||
cached_network_image: ^3.4.1
|
cached_network_image: ^3.4.1
|
||||||
|
wechat_assets_picker: ^10.1.0
|
||||||
|
flutter_image_compress: ^2.4.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user