Skip to content

Commit a8d2dcb

Browse files
authored
merge: pull request #622 from hm21:dev
fix(state-manager): ensure background images are copied when updated
2 parents b5b450f + 526cbe4 commit a8d2dcb

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 11.2.1
4+
- **FIX**(state-history): Resolve issue where updating the background-image overwrote previous states.
5+
36
## 11.2.0
47
- **FEAT**(state-history): Added support for undo and redo when the background image is changed in the state history.
58

lib/core/models/editor_image.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,35 @@ class EditorImage {
229229
if (list == null) return 0;
230230
return list.fold(0, (hash, byte) => hash * 31 + byte);
231231
}
232+
233+
/// Creates a copy of this [EditorImage] with optional new values for its
234+
/// fields.
235+
///
236+
/// If a parameter is not provided, the current value from this instance
237+
/// is used.
238+
/// - [byteArray]: The new image data as a [Uint8List]. If provided, a
239+
/// copy is created.
240+
/// - [file]: The new image file as a [File]. If provided, a new [File]
241+
/// instance is created with the same path.
242+
/// - [networkUrl]: The new network URL for the image.
243+
/// - [assetPath]: The new asset path for the image.
244+
///
245+
/// Returns a new [EditorImage] instance with the updated values.
246+
EditorImage copyWith({
247+
Uint8List? byteArray,
248+
File? file,
249+
String? networkUrl,
250+
String? assetPath,
251+
}) {
252+
final bytes = byteArray ?? this.byteArray;
253+
final fileHelper = file ?? this.file;
254+
return EditorImage(
255+
byteArray: bytes != null ? Uint8List.fromList(bytes) : null,
256+
file: fileHelper != null ? File(fileHelper.path) : null,
257+
networkUrl: networkUrl ?? this.networkUrl,
258+
assetPath: assetPath ?? this.assetPath,
259+
);
260+
}
232261
}
233262

234263
/// Flutter EditorImageType Enum Documentation

lib/features/main_editor/main_editor.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,14 +1023,15 @@ class ProImageEditorState extends State<ProImageEditor>
10231023
}
10241024
stateManager.activeBackgroundImage = image;
10251025
} else {
1026+
addHistory();
10261027
stateManager.updateBackgroundImages(
10271028
oldImage: editorImage ?? widget.editorImage!,
10281029
newImage: image,
10291030
);
1030-
addHistory();
10311031
}
10321032

10331033
await decodeImage();
1034+
_rebuildController.add(null);
10341035
}
10351036

10361037
@override

lib/features/main_editor/services/state_manager.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class StateManager {
6969
required EditorImage oldImage,
7070
required EditorImage newImage,
7171
}) {
72-
_backgroundImages[historyPointer] = oldImage;
73-
_backgroundImages[historyPointer + 1] = newImage;
74-
activeBackgroundImage = newImage;
72+
_backgroundImages[historyPointer - 1] ??= oldImage.copyWith();
73+
_backgroundImages[historyPointer] = newImage.copyWith();
74+
activeBackgroundImage = newImage.copyWith();
7575
}
7676

7777
/// A setter for updating the state history list.
@@ -132,7 +132,7 @@ class StateManager {
132132
onStateHistoryChange?.call();
133133

134134
if (_backgroundImages[historyPointer] != null) {
135-
activeBackgroundImage = _backgroundImages[historyPointer];
135+
activeBackgroundImage = _backgroundImages[historyPointer]!.copyWith();
136136
}
137137
}
138138

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: pro_image_editor
22
description: "A Flutter image editor: Seamlessly enhance your images with user-friendly editing features."
3-
version: 11.2.0
3+
version: 11.2.1
44
homepage: https://github.com/hm21/pro_image_editor/
55
repository: https://github.com/hm21/pro_image_editor/
66
documentation: https://github.com/hm21/pro_image_editor/

0 commit comments

Comments
 (0)