Skip to content

Commit 262d891

Browse files
committed
Add option to generate new id while sanitizing PaintModel.
1 parent 38da207 commit 262d891

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

lib/src/api/models/effect.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class Effect with EquatableMixin, SerializableMixin {
137137

138138
/// Duplicates this instance with given data overrides.
139139
Effect copyWith({
140+
String? id,
140141
EffectType? type,
141142
double? radius,
142143
bool? visible,
@@ -146,7 +147,7 @@ class Effect with EquatableMixin, SerializableMixin {
146147
BlendModeC? blendMode,
147148
}) =>
148149
Effect(
149-
id: id,
150+
id: id ?? this.id,
150151
type: type ?? this.type,
151152
radius: radius ?? this.radius,
152153
visible: visible ?? this.visible,
@@ -156,6 +157,27 @@ class Effect with EquatableMixin, SerializableMixin {
156157
blendMode: blendMode ?? this.blendMode,
157158
);
158159

160+
/// [Effect] can store all types of effects at the same time to allow
161+
/// switching between different effect types. However, this is not the desired
162+
/// behavior when applying a effect to another node. This method returns a
163+
/// sanitized version of this effect model that only contains the data relevant
164+
/// to the current effect type.
165+
///
166+
/// Resembles [PaintModel.sanitize]. There are no properties to be sanitized
167+
/// for [Effect] at the moment.
168+
Effect sanitize({bool changeIds = false}) {
169+
switch (type) {
170+
case EffectType.dropShadow:
171+
return copyWith(id: changeIds ? generateId() : id);
172+
case EffectType.innerShadow:
173+
return copyWith(id: changeIds ? generateId() : id);
174+
case EffectType.layerBlur:
175+
return copyWith(id: changeIds ? generateId() : id);
176+
case EffectType.backgroundBlur:
177+
return copyWith(id: changeIds ? generateId() : id);
178+
}
179+
}
180+
159181
@override
160182
List<Object?> get props => [
161183
id,

lib/src/api/models/paint.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,11 @@ class PaintModel with EquatableMixin, SerializableMixin {
433433
/// behavior when applying a paint to another node. This method returns a
434434
/// sanitized version of this paint model that only contains the data relevant
435435
/// to the current paint type.
436-
PaintModel sanitize() {
436+
PaintModel sanitize({bool changeIds = false}) {
437437
switch (type) {
438438
case PaintType.solid:
439439
return PaintModel.solid(
440-
id: id,
440+
id: changeIds ? generateId() : id,
441441
color: color,
442442
blendMode: blendMode,
443443
opacity: opacity,
@@ -448,7 +448,7 @@ class PaintModel with EquatableMixin, SerializableMixin {
448448
case PaintType.gradientAngular:
449449
case PaintType.gradientDiamond:
450450
return PaintModel(
451-
id: id,
451+
id: changeIds ? generateId() : id,
452452
type: type,
453453
blendMode: blendMode,
454454
opacity: opacity,
@@ -458,7 +458,7 @@ class PaintModel with EquatableMixin, SerializableMixin {
458458
);
459459
case PaintType.image:
460460
return PaintModel.image(
461-
id: id,
461+
id: changeIds ? generateId() : id,
462462
downloadUrl: downloadUrl,
463463
fit: fit,
464464
sourceWidth: sourceWidth,

0 commit comments

Comments
 (0)