Skip to content

Commit b79192e

Browse files
ishivamgdkwingsmt
andauthored
Fix ShapeDecoration.lerp crash when interpolating between gradient and color (#187368)
## Description `ShapeDecoration.color` and `ShapeDecoration.gradient` are mutually exclusive — the constructor asserts `!(color != null && gradient != null)`. However, [`ShapeDecoration.lerp`] computed `Color.lerp(...)` and `Gradient.lerp(...)` independently. When animating between a `ShapeDecoration` that uses a **color** and one that uses a **gradient**, both interpolated values are non-null mid-transition, which trips the assert and crashes: ``` 'package:flutter/src/painting/shape_decoration.dart': Failed assertion: line 76 pos 14: '!(color != null && gradient != null)': is not true. ``` This reproduces with a simple `DecorationTween`/`TweenAnimationBuilder` between a color `ShapeDecoration` and a gradient `ShapeDecoration` (see the linked issue). `BoxDecoration` is unaffected because it does not enforce the mutual-exclusion invariant. ### Fix Resolve the conflict in `ShapeDecoration.lerp`: when both the lerped color and the lerped gradient would be non-null, keep the **source's** fill type before the half-way point (`t < 0.5`) and the **target's** after it, so only one of `color`/`gradient` is ever set. The end points (`t == 0.0`/`t == 1.0`) are unchanged. ### Tests Added `ShapeDecoration.lerp between gradient and color does not throw`, which verifies the end points are preserved, that exactly one of color/gradient is set on each side of the midpoint, and that interpolating across the full range (in both directions) never throws. The test fails on `master` (assertion) and passes with this change. ## Related Issues Fixes #93953 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. [Contributor Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests --------- Co-authored-by: Tong Mu <[email protected]>
1 parent 8db3a26 commit b79192e

4 files changed

Lines changed: 196 additions & 2 deletions

File tree

‎packages/flutter/lib/src/painting/gradient.dart‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,26 @@ abstract class Gradient {
238238
/// Returns a new [Gradient] with each color set to the given opacity.
239239
Gradient withOpacity(double opacity);
240240

241+
/// Returns a copy of this gradient with all of its [colors] replaced by the
242+
/// given `color`.
243+
///
244+
/// The geometry of the gradient (such as its [stops] and the
245+
/// subclass-specific positioning) is preserved, so the result paints as a
246+
/// uniform `color`. This is useful to represent a solid color as a gradient,
247+
/// for example when interpolating between a color and a gradient with [lerp].
248+
///
249+
/// Subclasses should override this method to preserve their own geometry. The
250+
/// base implementation returns a [LinearGradient]; this is sufficient because
251+
/// a gradient with uniform colors paints as a solid color regardless of its
252+
/// geometry.
253+
Gradient fromColor(Color color) {
254+
return LinearGradient(
255+
colors: List<Color>.filled(colors.length, color),
256+
stops: stops,
257+
transform: transform,
258+
);
259+
}
260+
241261
/// Linearly interpolates from another [Gradient] to `this`.
242262
///
243263
/// When implementing this method in subclasses, return null if this class
@@ -456,6 +476,18 @@ class LinearGradient extends Gradient {
456476
);
457477
}
458478

479+
@override
480+
LinearGradient fromColor(Color color) {
481+
return LinearGradient(
482+
begin: begin,
483+
end: end,
484+
colors: List<Color>.filled(colors.length, color),
485+
stops: stops,
486+
tileMode: tileMode,
487+
transform: transform,
488+
);
489+
}
490+
459491
@override
460492
Gradient? lerpFrom(Gradient? a, double t) {
461493
if (a is LinearGradient?) {
@@ -750,6 +782,20 @@ class RadialGradient extends Gradient {
750782
);
751783
}
752784

785+
@override
786+
RadialGradient fromColor(Color color) {
787+
return RadialGradient(
788+
center: center,
789+
radius: radius,
790+
colors: List<Color>.filled(colors.length, color),
791+
stops: stops,
792+
tileMode: tileMode,
793+
focal: focal,
794+
focalRadius: focalRadius,
795+
transform: transform,
796+
);
797+
}
798+
753799
@override
754800
Gradient? lerpFrom(Gradient? a, double t) {
755801
if (a is RadialGradient?) {
@@ -1058,6 +1104,19 @@ class SweepGradient extends Gradient {
10581104
);
10591105
}
10601106

1107+
@override
1108+
SweepGradient fromColor(Color color) {
1109+
return SweepGradient(
1110+
center: center,
1111+
startAngle: startAngle,
1112+
endAngle: endAngle,
1113+
colors: List<Color>.filled(colors.length, color),
1114+
stops: stops,
1115+
tileMode: tileMode,
1116+
transform: transform,
1117+
);
1118+
}
1119+
10611120
@override
10621121
Gradient? lerpFrom(Gradient? a, double t) {
10631122
if (a is SweepGradient?) {

‎packages/flutter/lib/src/painting/shape_decoration.dart‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,26 @@ class ShapeDecoration extends Decoration {
228228
return b;
229229
}
230230
}
231+
// A ShapeDecoration's color and gradient are mutually exclusive (see the
232+
// assert in the constructor). When interpolating between a decoration that
233+
// uses a color and one that uses a gradient, represent the color as a
234+
// uniform-color gradient (built from the other side's gradient) so the two
235+
// can be interpolated smoothly as gradients, rather than producing both a
236+
// color and a gradient at the same time.
237+
// See https://github.com/flutter/flutter/issues/93953
238+
Gradient? aGradient = a?.gradient;
239+
Gradient? bGradient = b?.gradient;
240+
if (aGradient == null && bGradient != null && a?.color != null) {
241+
aGradient = bGradient.fromColor(a!.color!);
242+
} else if (bGradient == null && aGradient != null && b?.color != null) {
243+
bGradient = aGradient.fromColor(b!.color!);
244+
}
245+
final Gradient? gradient = Gradient.lerp(aGradient, bGradient, t);
231246
return ShapeDecoration(
232-
color: Color.lerp(a?.color, b?.color, t),
233-
gradient: Gradient.lerp(a?.gradient, b?.gradient, t),
247+
// color and gradient are mutually exclusive, so only fall back to a color
248+
// when the interpolation did not produce a gradient.
249+
color: gradient == null ? Color.lerp(a?.color, b?.color, t) : null,
250+
gradient: gradient,
234251
image: DecorationImage.lerp(a?.image, b?.image, t),
235252
shadows: BoxShadow.lerpList(a?.shadows, b?.shadows, t),
236253
shape: ShapeBorder.lerp(a?.shape, b?.shape, t)!,

‎packages/flutter/test/painting/gradient_test.dart‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,80 @@ void main() {
370370
);
371371
});
372372

373+
test('LinearGradient.fromColor replaces all colors and preserves geometry', () {
374+
const testGradient = LinearGradient(
375+
begin: Alignment.bottomRight,
376+
end: Alignment.topCenter,
377+
colors: <Color>[Color(0xFFFFFFFF), Color(0xAF777777), Color(0x44444444)],
378+
stops: <double>[0.0, 0.3, 1.0],
379+
tileMode: TileMode.mirror,
380+
transform: GradientRotation(1),
381+
);
382+
final LinearGradient actual = testGradient.fromColor(const Color(0xFF00FF00));
383+
384+
expect(
385+
actual,
386+
const LinearGradient(
387+
begin: Alignment.bottomRight,
388+
end: Alignment.topCenter,
389+
colors: <Color>[Color(0xFF00FF00), Color(0xFF00FF00), Color(0xFF00FF00)],
390+
stops: <double>[0.0, 0.3, 1.0],
391+
tileMode: TileMode.mirror,
392+
transform: GradientRotation(1),
393+
),
394+
);
395+
});
396+
397+
test('RadialGradient.fromColor replaces all colors and preserves geometry', () {
398+
const testGradient = RadialGradient(
399+
center: Alignment.topLeft,
400+
radius: 0.25,
401+
colors: <Color>[Color(0xFFFFFFFF), Color(0x44444444)],
402+
focal: Alignment.centerRight,
403+
focalRadius: 0.1,
404+
tileMode: TileMode.mirror,
405+
transform: GradientRotation(1),
406+
);
407+
final RadialGradient actual = testGradient.fromColor(const Color(0xFF00FF00));
408+
409+
expect(
410+
actual,
411+
const RadialGradient(
412+
center: Alignment.topLeft,
413+
radius: 0.25,
414+
colors: <Color>[Color(0xFF00FF00), Color(0xFF00FF00)],
415+
focal: Alignment.centerRight,
416+
focalRadius: 0.1,
417+
tileMode: TileMode.mirror,
418+
transform: GradientRotation(1),
419+
),
420+
);
421+
});
422+
423+
test('SweepGradient.fromColor replaces all colors and preserves geometry', () {
424+
const testGradient = SweepGradient(
425+
center: Alignment.topLeft,
426+
startAngle: 0.1,
427+
endAngle: 2.0,
428+
colors: <Color>[Color(0xFFFFFFFF), Color(0x44444444)],
429+
tileMode: TileMode.repeated,
430+
transform: GradientRotation(1),
431+
);
432+
final SweepGradient actual = testGradient.fromColor(const Color(0xFF00FF00));
433+
434+
expect(
435+
actual,
436+
const SweepGradient(
437+
center: Alignment.topLeft,
438+
startAngle: 0.1,
439+
endAngle: 2.0,
440+
colors: <Color>[Color(0xFF00FF00), Color(0xFF00FF00)],
441+
tileMode: TileMode.repeated,
442+
transform: GradientRotation(1),
443+
),
444+
);
445+
});
446+
373447
test('LinearGradient.scale preserves transform', () {
374448
const testGradient = LinearGradient(
375449
begin: Alignment.bottomRight,

‎packages/flutter/test/painting/shape_decoration_test.dart‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,50 @@ void main() {
9999
expect(b.hitTest(size, const Offset(20.0, 50.0)), isTrue);
100100
});
101101

102+
test('ShapeDecoration.lerp between gradient and color is smooth and does not throw', () {
103+
// Regression test for https://github.com/flutter/flutter/issues/93953
104+
const colorR = Color(0xffff0000);
105+
const colorG = Color(0xff00ff00);
106+
const Gradient gradient = LinearGradient(colors: <Color>[colorR, colorG]);
107+
const colorDecoration = ShapeDecoration(color: colorR, shape: CircleBorder());
108+
const gradientDecoration = ShapeDecoration(
109+
gradient: gradient,
110+
shape: RoundedRectangleBorder(),
111+
);
112+
113+
// The end points are returned unchanged.
114+
expect(ShapeDecoration.lerp(colorDecoration, gradientDecoration, 0.0), colorDecoration);
115+
expect(ShapeDecoration.lerp(colorDecoration, gradientDecoration, 1.0), gradientDecoration);
116+
117+
// In between, the color is represented as a uniform-color gradient, so the
118+
// result is always a gradient (and never both a color and a gradient, which
119+
// would otherwise throw the constructor's assertion). This yields a smooth
120+
// transition rather than a sudden jump at the half-way point.
121+
for (final t in <double>[0.1, 0.25, 0.49, 0.5, 0.51, 0.75, 0.9]) {
122+
final ShapeDecoration forward =
123+
ShapeDecoration.lerp(colorDecoration, gradientDecoration, t)!;
124+
expect(forward.color, isNull);
125+
expect(forward.gradient, isA<LinearGradient>());
126+
127+
// The reverse direction (gradient -> color) behaves the same way.
128+
final ShapeDecoration reverse =
129+
ShapeDecoration.lerp(gradientDecoration, colorDecoration, t)!;
130+
expect(reverse.color, isNull);
131+
expect(reverse.gradient, isA<LinearGradient>());
132+
}
133+
134+
// Close to the color end, the interpolated gradient is (almost) the uniform
135+
// start color.
136+
final gradientNearColor =
137+
ShapeDecoration.lerp(colorDecoration, gradientDecoration, 0.001)!.gradient! as LinearGradient;
138+
expect(gradientNearColor.colors, hasLength(2));
139+
for (final Color color in gradientNearColor.colors) {
140+
expect(color.r, closeTo(colorR.r, 0.05));
141+
expect(color.g, closeTo(colorR.g, 0.05));
142+
expect(color.b, closeTo(colorR.b, 0.05));
143+
}
144+
});
145+
102146
test('ShapeDecoration.image RTL test', () async {
103147
final ui.Image image = await createTestImage(width: 100, height: 200);
104148
final log = <int>[];

0 commit comments

Comments
 (0)