diff --git a/lib/pages/about/view.dart b/lib/pages/about/view.dart index f0887debd..d698c05cc 100644 --- a/lib/pages/about/view.dart +++ b/lib/pages/about/view.dart @@ -18,7 +18,6 @@ import 'package:PiliPlus/utils/page_utils.dart'; import 'package:PiliPlus/utils/storage.dart'; import 'package:PiliPlus/utils/update.dart'; import 'package:PiliPlus/utils/utils.dart'; -import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart' hide ListTile; import 'package:flutter/services.dart' show Clipboard, ClipboardData; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; @@ -339,30 +338,17 @@ Future showInportExportDialog( ListTile( dense: true, title: const Text('导出文件至本地', style: style), - onTap: () async { + onTap: () { Get.back(); final res = utf8.encode(toJson()); final name = 'piliplus_${label}_${context.isTablet ? 'pad' : 'phone'}_' '${DateFormat('yyyyMMddHHmmss').format(DateTime.now())}.json'; - try { - final path = await FilePicker.platform.saveFile( - allowedExtensions: ['json'], - type: FileType.custom, - fileName: name, - bytes: Utils.isDesktop ? null : res, - ); - if (path == null) { - SmartDialog.showToast("取消保存"); - return; - } - if (Utils.isDesktop) { - await File(path).writeAsBytes(res); - } - SmartDialog.showToast("已保存"); - } catch (e) { - SmartDialog.showToast("保存失败: $e"); - } + Utils.saveBytes2File( + name: name, + bytes: res, + allowedExtensions: const ['json'], + ); }, ), ListTile( diff --git a/lib/pages/common/publish/common_rich_text_pub_page.dart b/lib/pages/common/publish/common_rich_text_pub_page.dart index 1d186299d..6d0706ab0 100644 --- a/lib/pages/common/publish/common_rich_text_pub_page.dart +++ b/lib/pages/common/publish/common_rich_text_pub_page.dart @@ -82,7 +82,10 @@ abstract class CommonRichTextPubPageState ).colorScheme.secondaryContainer.withValues(alpha: 0.5); void onClear() { - pathList.removeAt(index); + final path = pathList.removeAt(index); + if (Utils.isMobile) { + File(path).tryDel(); + } if (pathList.isEmpty && editController.rawText.trim().isEmpty) { enablePublish.value = false; } @@ -148,8 +151,9 @@ abstract class CommonRichTextPubPageState Future onCropImage(int index) async { late final colorScheme = ColorScheme.of(context); - CroppedFile? croppedFile = await ImageCropper.platform.cropImage( - sourcePath: pathList[index], + final sourcePath = pathList[index]; + final croppedFile = await ImageCropper.platform.cropImage( + sourcePath: sourcePath, uiSettings: [ AndroidUiSettings( toolbarTitle: '裁剪', @@ -161,6 +165,7 @@ abstract class CommonRichTextPubPageState ], ); if (croppedFile != null) { + File(sourcePath).tryDel(); pathList[index] = croppedFile.path; } } diff --git a/lib/pages/dynamics_create_vote/controller.dart b/lib/pages/dynamics_create_vote/controller.dart index aef0a09d9..52f327575 100644 --- a/lib/pages/dynamics_create_vote/controller.dart +++ b/lib/pages/dynamics_create_vote/controller.dart @@ -7,7 +7,6 @@ import 'package:PiliPlus/utils/accounts.dart'; import 'package:PiliPlus/utils/utils.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; -import 'package:image_picker/image_picker.dart'; class CreateVoteController extends GetxController { CreateVoteController(this.voteId); @@ -109,9 +108,9 @@ class CreateVoteController extends GetxController { } } - Future onUpload(int index, XFile pickedFile) async { + Future onUpload(int index, String path) async { var res = await MsgHttp.uploadBfs( - path: pickedFile.path, + path: path, category: 'daily', biz: 'vote', ); diff --git a/lib/pages/dynamics_create_vote/view.dart b/lib/pages/dynamics_create_vote/view.dart index ef8264a7a..0e70093bf 100644 --- a/lib/pages/dynamics_create_vote/view.dart +++ b/lib/pages/dynamics_create_vote/view.dart @@ -1,8 +1,11 @@ +import 'dart:io' show File; + import 'package:PiliPlus/common/widgets/button/icon_button.dart'; import 'package:PiliPlus/common/widgets/image/network_img_layer.dart'; import 'package:PiliPlus/models/dynamics/vote_model.dart'; import 'package:PiliPlus/pages/dynamics_create_vote/controller.dart'; import 'package:PiliPlus/utils/date_utils.dart'; +import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/utils.dart'; import 'package:easy_debounce/easy_throttle.dart'; import 'package:flutter/material.dart'; @@ -414,7 +417,12 @@ class _CreateVotePageState extends State { source: ImageSource.gallery, ); if (pickedFile != null) { - _controller.onUpload(index, pickedFile); + final path = pickedFile.path; + _controller.onUpload(index, path).whenComplete(() { + if (Utils.isMobile) { + File(path).tryDel(); + } + }); } } catch (e) { SmartDialog.showToast(e.toString()); diff --git a/lib/pages/fav_create/view.dart b/lib/pages/fav_create/view.dart index 2c047fc92..52e329754 100644 --- a/lib/pages/fav_create/view.dart +++ b/lib/pages/fav_create/view.dart @@ -1,3 +1,5 @@ +import 'dart:io' show File; + import 'package:PiliPlus/common/widgets/loading_widget/loading_widget.dart'; import 'package:PiliPlus/http/fav.dart'; import 'package:PiliPlus/http/msg.dart'; @@ -118,8 +120,8 @@ class _CreateFavPageState extends State { if (pickedFile != null && mounted) { String imgPath = pickedFile.path; if (Utils.isMobile) { - CroppedFile? croppedFile = await ImageCropper.platform.cropImage( - sourcePath: pickedFile.path, + final croppedFile = await ImageCropper.platform.cropImage( + sourcePath: imgPath, uiSettings: [ AndroidUiSettings( toolbarTitle: '裁剪', @@ -141,6 +143,7 @@ class _CreateFavPageState extends State { ], ); if (croppedFile != null) { + File(imgPath).tryDel(); imgPath = croppedFile.path; } } @@ -157,6 +160,9 @@ class _CreateFavPageState extends State { SmartDialog.showToast(res['msg']); } } + if (Utils.isMobile) { + File(imgPath).tryDel(); + } }); } } catch (e) { diff --git a/lib/pages/member_profile/view.dart b/lib/pages/member_profile/view.dart index 281b70076..27756e57e 100644 --- a/lib/pages/member_profile/view.dart +++ b/lib/pages/member_profile/view.dart @@ -1,3 +1,5 @@ +import 'dart:io' show File; + import 'package:PiliPlus/common/constants.dart'; import 'package:PiliPlus/common/widgets/loading_widget/loading_widget.dart'; import 'package:PiliPlus/http/constants.dart'; @@ -488,10 +490,10 @@ class _EditProfilePageState extends State { SmartDialog.showToast('不能选GIF'); return; } - String? imagePath; + String? imagePath = pickedFile.path; if (Utils.isMobile) { - CroppedFile? croppedFile = await ImageCropper.platform.cropImage( - sourcePath: pickedFile.path, + final croppedFile = await ImageCropper.platform.cropImage( + sourcePath: imagePath, uiSettings: [ AndroidUiSettings( toolbarTitle: '裁剪', @@ -514,11 +516,8 @@ class _EditProfilePageState extends State { ), ], ); - if (croppedFile != null) { - imagePath = croppedFile.path; - } - } else { - imagePath = pickedFile.path; + File(imagePath).tryDel(); + imagePath = croppedFile?.path; } if (imagePath != null) { Request() @@ -536,10 +535,17 @@ class _EditProfilePageState extends State { .then((res) { if (res.data['code'] == 0) { SmartDialog.showToast('修改成功'); - Future.delayed(const Duration(milliseconds: 500), _getInfo); + Future.delayed(const Duration(milliseconds: 500), () { + if (mounted) { + _getInfo(); + } + }); } else { SmartDialog.showToast(res.data['message']); } + if (Utils.isMobile && imagePath != null) { + File(imagePath).tryDel(); + } }); } } diff --git a/lib/pages/video/widgets/header_control.dart b/lib/pages/video/widgets/header_control.dart index 332bfddf7..23dedb703 100644 --- a/lib/pages/video/widgets/header_control.dart +++ b/lib/pages/video/widgets/header_control.dart @@ -42,7 +42,6 @@ import 'package:PiliPlus/utils/utils.dart'; import 'package:PiliPlus/utils/video_utils.dart'; import 'package:canvas_danmaku/canvas_danmaku.dart'; import 'package:dio/dio.dart'; -import 'package:file_picker/file_picker.dart'; import 'package:floating/floating.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -1058,20 +1057,11 @@ class HeaderControlState extends State { ); final name = '${introController.videoDetail.value.title}-${videoDetailCtr.bvid}-${videoDetailCtr.cid.value}-${item.lanDoc}.json'; - final path = await FilePicker.platform.saveFile( + Utils.saveBytes2File( + name: name, + bytes: bytes, allowedExtensions: const ['json'], - type: FileType.custom, - fileName: name, - bytes: Utils.isDesktop ? null : bytes, ); - if (path == null) { - SmartDialog.showToast("取消保存"); - return; - } - if (Utils.isDesktop) { - await File(path).writeAsBytes(bytes); - } - SmartDialog.showToast("已保存"); } } catch (e) { if (kDebugMode) rethrow; diff --git a/lib/pages/whisper_detail/view.dart b/lib/pages/whisper_detail/view.dart index 3a4588dd3..ba65521cf 100644 --- a/lib/pages/whisper_detail/view.dart +++ b/lib/pages/whisper_detail/view.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io' show File; import 'package:PiliPlus/common/widgets/image/network_img_layer.dart'; import 'package:PiliPlus/common/widgets/loading_widget/loading_widget.dart'; @@ -303,21 +304,20 @@ class _WhisperDetailPageState ); } else { try { - XFile? pickedFile = await imagePicker.pickImage( + final XFile? pickedFile = await imagePicker.pickImage( source: ImageSource.gallery, imageQuality: 100, ); if (pickedFile != null) { + final path = pickedFile.path; SmartDialog.showLoading(msg: '正在上传图片'); final result = await MsgHttp.uploadBfs( - path: pickedFile.path, + path: path, biz: 'im', ); if (result['status']) { String mimeType = - lookupMimeType( - pickedFile.path, - )?.split('/').getOrNull(1) ?? + lookupMimeType(path)?.split('/').getOrNull(1) ?? 'jpg'; UploadBfsResData data = result['data']; Map picMsg = { @@ -329,10 +329,16 @@ class _WhisperDetailPageState 'size': data.imgSize, }; SmartDialog.showLoading(msg: '正在发送'); - await _whisperDetailController.sendMsg( - picMsg: picMsg, - onClearText: editController.clear, - ); + await _whisperDetailController + .sendMsg( + picMsg: picMsg, + onClearText: editController.clear, + ) + .whenComplete(() { + if (Utils.isMobile) { + File(path).tryDel(); + } + }); } else { SmartDialog.dismiss(); SmartDialog.showToast(result['msg']); diff --git a/lib/utils/cache_manage.dart b/lib/utils/cache_manage.dart index b244b9f08..0ed89f7c4 100644 --- a/lib/utils/cache_manage.dart +++ b/lib/utils/cache_manage.dart @@ -18,9 +18,8 @@ abstract class CacheManage { final dir = Directory('${tempDirectory.path}/libCachedImageData'); if (dir.existsSync()) { return await getTotalSizeOfFilesInDir(dir, maxSize); - } else { - return 0; } + return 0; } if (tempDirectory.existsSync()) { diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 42dfa3e37..879f23773 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -6,6 +6,7 @@ import 'dart:math'; import 'package:PiliPlus/common/constants.dart'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:device_info_plus/device_info_plus.dart'; +import 'package:file_picker/file_picker.dart'; import 'package:flutter/services.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; @@ -25,6 +26,32 @@ abstract class Utils { static final bool isDesktop = Platform.isWindows || Platform.isMacOS || Platform.isLinux; + static Future saveBytes2File({ + required String name, + required Uint8List bytes, + required List allowedExtensions, + FileType type = FileType.custom, + }) async { + try { + final path = await FilePicker.platform.saveFile( + allowedExtensions: allowedExtensions, + type: type, + fileName: name, + bytes: Utils.isDesktop ? null : bytes, + ); + if (path == null) { + SmartDialog.showToast("取消保存"); + return; + } + if (Utils.isDesktop) { + await File(path).writeAsBytes(bytes); + } + SmartDialog.showToast("已保存"); + } catch (e) { + SmartDialog.showToast("保存失败: $e"); + } + } + static int? safeToInt(dynamic value) => switch (value) { int e => e, String e => int.tryParse(e), diff --git a/pubspec.lock b/pubspec.lock index 64513fff4..951017729 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -504,11 +504,12 @@ packages: file_picker: dependency: "direct main" description: - name: file_picker - sha256: f2d9f173c2c14635cc0e9b14c143c49ef30b4934e8d1d274d6206fcb0086a06f - url: "https://pub.dev" - source: hosted - version: "10.3.3" + path: "." + ref: mod + resolved-ref: "4b7dd4b85328f9ed0b369cc77c5c8e87957a9d18" + url: "https://github.com/bggRGjQaUbCoE/flutter_file_picker.git" + source: git + version: "10.3.5" file_selector_linux: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 660130717..e0c763321 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -211,7 +211,11 @@ dependencies: path: packages/window_manager ref: main tray_manager: ^0.5.1 - file_picker: ^10.3.3 + # file_picker: ^10.3.3 + file_picker: + git: + url: https://github.com/bggRGjQaUbCoE/flutter_file_picker.git + ref: mod super_sliver_list: git: url: https://github.com/bggRGjQaUbCoE/super_sliver_list.git