Skip to content

Commit

Permalink
Fix SetState will Reset ExtendedImage and clear the Crop State.
Browse files Browse the repository at this point in the history
  • Loading branch information
zmtzawqlp committed Dec 3, 2024
1 parent 6573d4e commit d43d46e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 5 additions & 2 deletions example/lib/pages/complex/image_editor_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class _ImageEditorDemoState extends State<ImageEditorDemo> {
AspectRatioItem(text: '9*16', value: CropAspectRatios.ratio9_16)
];

EdgeInsets cropRectPadding = const EdgeInsets.all(20.0);
double maxScale = 8.0;

late ValueNotifier<AspectRatioItem> _aspectRatio;

bool _cropping = false;
Expand Down Expand Up @@ -117,8 +120,8 @@ class _ImageEditorDemoState extends State<ImageEditorDemo> {
// 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,
Expand Down
51 changes: 40 additions & 11 deletions lib/src/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,29 @@ class ExtendedImageEditorState extends State<ExtendedImageEditor>
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) ??
Expand All @@ -127,14 +149,21 @@ class ExtendedImageEditorState extends State<ExtendedImageEditor>
_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
Expand All @@ -144,8 +173,8 @@ class ExtendedImageEditorState extends State<ExtendedImageEditor>

@override
void didUpdateWidget(ExtendedImageEditor oldWidget) {
_editActionDetails = null;
_initGestureConfig();
// _editActionDetails = null;
_initGestureConfig(oldWidget);
super.didUpdateWidget(oldWidget);
}

Expand Down

0 comments on commit d43d46e

Please sign in to comment.