@@ -397,6 +397,7 @@ class ProImageEditorState extends State<ProImageEditor>
397
397
398
398
/// Manager class for managing the state of the editor.
399
399
late final StateManager stateManager = StateManager (
400
+ activeBackgroundImage: widget.editorImage,
400
401
onStateHistoryChange: () =>
401
402
mainEditorCallbacks? .onStateHistoryChange? .call (stateManager, this ),
402
403
);
@@ -513,7 +514,7 @@ class ProImageEditorState extends State<ProImageEditor>
513
514
}
514
515
515
516
/// Get the current background image.
516
- late EditorImage ? editorImage = widget.editorImage ;
517
+ EditorImage ? get editorImage => stateManager.activeBackgroundImage ;
517
518
518
519
/// A [Completer] used to track the completion of a page open operation.
519
520
///
@@ -878,7 +879,7 @@ class ProImageEditorState extends State<ProImageEditor>
878
879
);
879
880
880
881
final resolution = widget.videoController! .initialResolution;
881
- editorImage = EditorImage (
882
+ stateManager.activeBackgroundImage = EditorImage (
882
883
byteArray: await createTransparentImage (
883
884
resolution.width,
884
885
resolution.height,
@@ -994,43 +995,42 @@ class ProImageEditorState extends State<ProImageEditor>
994
995
}
995
996
}
996
997
997
- /// Replace the background image with a new image and ensures all relevant
998
- /// states are rebuilt to reflect the new background. This includes marking
999
- /// all background screenshots as "broken" to trigger re-capture with the
1000
- /// new image, and rebuilding the current editor state to apply the changes.
1001
- ///
1002
- /// The method performs the following steps:
1003
- /// 1. Updates the editor's background image.
1004
- /// 2. Decodes the new image to prepare it for rendering.
1005
- /// 3. Marks all screenshots as "broken" so they are recaptured with the
1006
- /// updated background.
1007
- /// 4. Rebuilds the current editor state to ensure the new background is
1008
- /// applied.
1009
- Future <void > updateBackgroundImage (EditorImage image) async {
1010
- editorImage = image;
1011
- await decodeImage ();
1012
-
1013
- /// Mark all background captured images with the old background image as
1014
- /// "broken" that the editor capture them with the new image again
1015
- for (var item in stateManager.screenshots) {
1016
- item.broken = true ;
998
+ /// Updates the background image in the editor.
999
+ ///
1000
+ /// If [updateHistory] is `false` , marks all background-captured images that
1001
+ /// use the old-background image as "broken" so they will be recaptured with
1002
+ /// the new image, and set the active background image to [image] .
1003
+ ///
1004
+ /// If [updateHistory] is `true` , updates the background images in the
1005
+ /// state manager, replaces the old image with [image] , and adds the change
1006
+ /// to the history.
1007
+ ///
1008
+ /// After updating, decodes the new image asynchronously.
1009
+ ///
1010
+ /// [image] : The new background image to set.
1011
+ /// [updateHistory] : Whether to update the history with this change
1012
+ /// (default is `true` ).
1013
+ Future <void > updateBackgroundImage (
1014
+ EditorImage image, {
1015
+ bool updateHistory = true ,
1016
+ }) async {
1017
+ if (! updateHistory) {
1018
+ /// Mark all background-captured images that use the old background
1019
+ /// image as "broken" so the editor captures them again with the new
1020
+ /// image.
1021
+ for (var item in stateManager.screenshots) {
1022
+ item.broken = true ;
1023
+ }
1024
+ stateManager.activeBackgroundImage = image;
1025
+ } else {
1026
+ stateManager.updateBackgroundImages (
1027
+ oldImage: editorImage ?? widget.editorImage! ,
1028
+ newImage: image,
1029
+ );
1030
+ addHistory ();
1017
1031
}
1018
1032
1019
- /// Force to rebuild everything
1020
- int pos = stateManager.historyPointer;
1021
- EditorStateHistory oldHistory = stateManager.stateHistory[pos];
1022
-
1023
- stateManager.stateHistory[pos] = EditorStateHistory (
1024
- layers: oldHistory.layers,
1025
- transformConfigs: oldHistory.transformConfigs,
1026
- blur: oldHistory.blur,
1027
- filters: [...oldHistory.filters],
1028
- tuneAdjustments: [...oldHistory.tuneAdjustments],
1029
- );
1030
- WidgetsBinding .instance.addPostFrameCallback ((_) {
1031
- if (! mounted) return ;
1032
- _backgroundImageColorFilterKey.currentState? .refresh ();
1033
- });
1033
+ await decodeImage ();
1034
1034
}
1035
1035
1036
1036
@override
0 commit comments