From d43d46e84646d25a40e2df07c5839b60a3d4a42b Mon Sep 17 00:00:00 2001 From: zmtzawqlp Date: Wed, 27 Nov 2024 23:48:35 +0800 Subject: [PATCH] Fix SetState will Reset ExtendedImage and clear the Crop State. --- CHANGELOG.md | 1 + .../lib/pages/complex/image_editor_demo.dart | 7 ++- lib/src/editor/editor.dart | 51 +++++++++++++++---- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5563dbd..ea33be5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 9.0.8 * Fix the issue with 90-degree judgment when rotate image. +* Fix SetState will Reset ExtendedImage and clear the Crop State. (#712) ## 9.0.7 diff --git a/example/lib/pages/complex/image_editor_demo.dart b/example/lib/pages/complex/image_editor_demo.dart index e850711..3f2b2c3 100644 --- a/example/lib/pages/complex/image_editor_demo.dart +++ b/example/lib/pages/complex/image_editor_demo.dart @@ -50,6 +50,9 @@ class _ImageEditorDemoState extends State { AspectRatioItem(text: '9*16', value: CropAspectRatios.ratio9_16) ]; + EdgeInsets cropRectPadding = const EdgeInsets.all(20.0); + double maxScale = 8.0; + late ValueNotifier _aspectRatio; bool _cropping = false; @@ -117,8 +120,8 @@ class _ImageEditorDemoState extends State { // extendedImageEditorKey: editorKey, initEditorConfigHandler: (ExtendedImageState? state) { return EditorConfig( - maxScale: 8.0, - cropRectPadding: const EdgeInsets.all(20.0), + maxScale: maxScale, + cropRectPadding: cropRectPadding, hitTestSize: 20.0, cropLayerPainter: _cropLayerPainter.value, initCropRectType: InitCropRectType.imageRect, diff --git a/lib/src/editor/editor.dart b/lib/src/editor/editor.dart index 107d2f2..1578bba 100644 --- a/lib/src/editor/editor.dart +++ b/lib/src/editor/editor.dart @@ -104,7 +104,29 @@ class ExtendedImageEditorState extends State super.dispose(); } - void _initGestureConfig() { + void _initGestureConfig([ExtendedImageEditor? oldWidget]) { + if (oldWidget != null && + widget.extendedImageState.extendedImageInfo != + oldWidget.extendedImageState.extendedImageInfo) { + // reset + _editActionDetails = null; + } + + if (_editActionDetails == null) { + final int length = _history.length; + _history.clear(); + if (length != _history.length) { + _safeUpdate( + () { + _editorConfig?.controller?._notifyListeners(); + }, + ); + } + } + + // check config + final EditorConfig? oldConfig = _editorConfig?.copyWith(); + _editorConfig = widget .extendedImageState.imageWidget.initEditorConfigHandler ?.call(widget.extendedImageState) ?? @@ -127,14 +149,21 @@ class ExtendedImageEditorState extends State _editActionDetails!.cropAspectRatio = _editorConfig!.cropAspectRatio; } - final int length = _history.length; - _history.clear(); - if (length != _history.length) { - _safeUpdate( - () { - _editorConfig!.controller?._notifyListeners(); - }, - ); + if (oldConfig != null && + oldConfig.cropAspectRatio != config.cropAspectRatio) { + updateCropAspectRatio(config.cropAspectRatio); + } + + if (_editActionDetails!.totalScale > config.maxScale) { + _editActionDetails!.totalScale = config.maxScale; + _editActionDetails!.getFinalDestinationRect(); + _recalculateCropRect(); + } + + if (oldConfig != null && + oldConfig.cropRectPadding != config.cropRectPadding) { + _editActionDetails!.cropRectPadding = config.cropRectPadding; + _recalculateCropRect(); } // save after afterPaintImage @@ -144,8 +173,8 @@ class ExtendedImageEditorState extends State @override void didUpdateWidget(ExtendedImageEditor oldWidget) { - _editActionDetails = null; - _initGestureConfig(); + // _editActionDetails = null; + _initGestureConfig(oldWidget); super.didUpdateWidget(oldWidget); }