From 5b9c92e4d3324cb4dca6d985c9fb83008d6fd9b1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 1 Aug 2024 23:44:07 +0800 Subject: [PATCH] :sparkles: Crop image --- android/app/src/main/AndroidManifest.xml | 6 ++ lib/services.dart | 2 +- lib/translations/en_us.dart | 1 + lib/translations/zh_cn.dart | 1 + .../attachments/attachment_editor.dart | 78 +++++++++++++++---- web/index.html | 4 + 6 files changed, 74 insertions(+), 18 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 8b82d6c..ec70ee6 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -70,6 +70,12 @@ + + + { ); } + Future _cropAttachment(int queueIndex) async { + final task = _uploadController.queueOfUpload[queueIndex]; + CroppedFile? croppedFile = await ImageCropper().cropImage( + sourcePath: task.file.path, + uiSettings: [ + AndroidUiSettings( + toolbarTitle: 'cropImage'.tr, + toolbarColor: Colors.deepOrange, + toolbarWidgetColor: Colors.white, + aspectRatioPresets: CropAspectRatioPreset.values, + ), + IOSUiSettings( + title: 'cropImage'.tr, + aspectRatioPresets: CropAspectRatioPreset.values, + ), + WebUiSettings( + context: context, + ), + ], + ); + if (croppedFile == null) return; + _uploadController.queueOfUpload[queueIndex].file = File(croppedFile.path); + _uploadController.queueOfUpload.refresh(); + } + Future _deleteAttachment(Attachment element) async { setState(() => _isBusy = true); try { @@ -216,6 +242,9 @@ class _AttachmentEditorPopupState extends State { } Widget _buildQueueEntry(AttachmentUploadTask element, int index) { + final extName = extension(element.file.path).substring(1); + final canBeCrop = ['png', 'jpg', 'jpeg', 'gif'].contains(extName); + return Container( padding: const EdgeInsets.only(left: 16, right: 8, bottom: 16), child: Card( @@ -269,23 +298,38 @@ class _AttachmentEditorPopupState extends State { child: Icon(Icons.check), ), ), + if (!element.isCompleted && canBeCrop) + Obx( + () => IconButton( + color: Colors.teal, + icon: const Icon(Icons.crop), + visualDensity: const VisualDensity(horizontal: -4), + onPressed: _uploadController.isUploading.value + ? null + : () { + _cropAttachment(index); + }, + ), + ), if (!element.isCompleted && !element.isUploading) - IconButton( - color: Colors.green, - icon: const Icon(Icons.play_arrow), - visualDensity: const VisualDensity(horizontal: -4), - onPressed: _uploadController.isUploading.value - ? null - : () { - _uploadController - .performSingleTask(index) - .then((r) { - widget.onAdd(r.id); - if (mounted) { - setState(() => _attachments.add(r)); - } - }); - }, + Obx( + () => IconButton( + color: Colors.green, + icon: const Icon(Icons.play_arrow), + visualDensity: const VisualDensity(horizontal: -4), + onPressed: _uploadController.isUploading.value + ? null + : () { + _uploadController + .performSingleTask(index) + .then((r) { + widget.onAdd(r.id); + if (mounted) { + setState(() => _attachments.add(r)); + } + }); + }, + ), ), if (!element.isCompleted && !element.isUploading) IconButton( diff --git a/web/index.html b/web/index.html index d11360a..cbe7334 100644 --- a/web/index.html +++ b/web/index.html @@ -26,6 +26,10 @@ + + + +