Skip to content

Commit 8bdce07

Browse files
authored
Make shape border lerp symmetric (flutter#187282)
<img width="1536" height="1024" alt="ChatGPT Image May 29, 2026, 01_48_49 AM" src="https://github.com/user-attachments/assets/1a48077b-6687-47de-820a-3171b8bde84b" /> <img width="1568" height="656" alt="image" src="https://github.com/user-attachments/assets/83c0b20f-25c8-49af-b1e7-c57ac5ab4ec4" /> Fix flutter#110634 (took 4 years to think on this solution!) Make ShapeBorder.lerp use reverse interpolation before fallback Some border pairs can already lerp with each other, but only one side knows how to do it. For example, `StarBorder` knows how to lerp to `RoundedRectangleBorder`, but `RoundedRectangleBorder` does not need to know about `StarBorder`. Instead of adding more pair-specific checks like `if (b is RoundedRectangleBorder)` across shapes (at a certain point I even considered sealed classes), we can make the central lerp smarter. After trying the existing direction: `b.lerpFrom(a, t) ?? a.lerpTo(b, t)` also try the same animation from the opposite direction: `b.lerpTo(a, 1.0 - t) ?? a.lerpFrom(b, 1.0 - t)` So if `ShapeBorder.lerp(star, roundedRectangle, t)` works, `ShapeBorder.lerp(roundedRectangle, star, t)` can reuse that same logic on the reversed timeline instead of falling back to the hard switch at `t = 0.5`. This keeps the cross-shape behavior centralized in `ShapeBorder.lerp` and `OutlinedBorder.lerp`, without changing direct `lerpFrom` or `lerpTo` calls.
1 parent 6414603 commit 8bdce07

2 files changed

Lines changed: 130 additions & 16 deletions

File tree

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,9 @@ abstract class ShapeBorder {
440440
/// class) to `this`.
441441
///
442442
/// When implementing this method in subclasses, return null if this class
443-
/// cannot interpolate from `a`. In that case, [lerp] will try `a`'s [lerpTo]
444-
/// method instead. If `a` is null, this must not return null.
443+
/// cannot interpolate from `a`. In that case, [lerp] will try other ways to
444+
/// interpolate between the two borders before applying a default behavior. If
445+
/// `a` is null, this must not return null.
445446
///
446447
/// The base class implementation handles the case of `a` being null by
447448
/// deferring to [scale].
@@ -471,11 +472,10 @@ abstract class ShapeBorder {
471472
/// Linearly interpolates from `this` to another [ShapeBorder] (possibly of
472473
/// another class).
473474
///
474-
/// This is called if `b`'s [lerpTo] did not know how to handle this class.
475-
///
476475
/// When implementing this method in subclasses, return null if this class
477-
/// cannot interpolate from `b`. In that case, [lerp] will apply a default
478-
/// behavior instead. If `b` is null, this must not return null.
476+
/// cannot interpolate to `b`. In that case, [lerp] will try other ways to
477+
/// interpolate between the two borders before applying a default behavior. If
478+
/// `b` is null, this must not return null.
479479
///
480480
/// The base class implementation handles the case of `b` being null by
481481
/// deferring to [scale].
@@ -503,17 +503,20 @@ abstract class ShapeBorder {
503503

504504
/// Linearly interpolates between two [ShapeBorder]s.
505505
///
506-
/// This defers to `b`'s [lerpTo] function if `b` is not null. If `b` is
507-
/// null or if its [lerpTo] returns null, it uses `a`'s [lerpFrom]
508-
/// function instead. If both return null, it returns `a` before `t=0.5`
509-
/// and `b` after `t=0.5`.
506+
/// This first tries the forward interpolation, by calling `b.lerpFrom(a, t)`
507+
/// and then `a.lerpTo(b, t)`. If both return null, this tries the same
508+
/// interpolation on the reversed timeline, by calling
509+
/// `b.lerpTo(a, 1.0 - t)` and then `a.lerpFrom(b, 1.0 - t)`. If all of
510+
/// these methods return null, this returns `a` before `t=0.5` and `b` after
511+
/// `t=0.5`.
510512
///
511513
/// {@macro dart.ui.shadow.lerp}
512514
static ShapeBorder? lerp(ShapeBorder? a, ShapeBorder? b, double t) {
513515
if (identical(a, b)) {
514516
return a;
515517
}
516-
final ShapeBorder? result = b?.lerpFrom(a, t) ?? a?.lerpTo(b, t);
518+
final ShapeBorder? result =
519+
b?.lerpFrom(a, t) ?? a?.lerpTo(b, t) ?? b?.lerpTo(a, 1.0 - t) ?? a?.lerpFrom(b, 1.0 - t);
517520
return result ?? (t < 0.5 ? a : b);
518521
}
519522

@@ -693,17 +696,20 @@ abstract class OutlinedBorder extends ShapeBorder {
693696

694697
/// Linearly interpolates between two [OutlinedBorder]s.
695698
///
696-
/// This defers to `b`'s [lerpTo] function if `b` is not null. If `b` is
697-
/// null or if its [lerpTo] returns null, it uses `a`'s [lerpFrom]
698-
/// function instead. If both return null, it returns `a` before `t=0.5`
699-
/// and `b` after `t=0.5`.
699+
/// This first tries the forward interpolation, by calling `b.lerpFrom(a, t)`
700+
/// and then `a.lerpTo(b, t)`. If both return null, this tries the same
701+
/// interpolation on the reversed timeline, by calling
702+
/// `b.lerpTo(a, 1.0 - t)` and then `a.lerpFrom(b, 1.0 - t)`. If all of
703+
/// these methods return null, this returns `a` before `t=0.5` and `b` after
704+
/// `t=0.5`.
700705
///
701706
/// {@macro dart.ui.shadow.lerp}
702707
static OutlinedBorder? lerp(OutlinedBorder? a, OutlinedBorder? b, double t) {
703708
if (identical(a, b)) {
704709
return a;
705710
}
706-
final ShapeBorder? result = b?.lerpFrom(a, t) ?? a?.lerpTo(b, t);
711+
final ShapeBorder? result =
712+
b?.lerpFrom(a, t) ?? a?.lerpTo(b, t) ?? b?.lerpTo(a, 1.0 - t) ?? a?.lerpFrom(b, 1.0 - t);
707713
return result as OutlinedBorder? ?? (t < 0.5 ? a : b);
708714
}
709715
}

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,53 @@ void main() {
3636
expect(identical(ShapeBorder.lerp(border, border, 0.5), border), true);
3737
});
3838

39+
test('ShapeBorder.lerp tries equivalent reverse interpolation', () {
40+
expect(
41+
ShapeBorder.lerp(const _LerpBorder(), const _ReverseLerpToBorder(), 0.25),
42+
const _LerpBorder(0.75),
43+
);
44+
expect(
45+
ShapeBorder.lerp(const _ReverseLerpFromBorder(), const _LerpBorder(), 0.25),
46+
const _LerpBorder(0.75),
47+
);
48+
});
49+
50+
test('OutlinedBorder.lerp tries equivalent reverse interpolation', () {
51+
expect(
52+
OutlinedBorder.lerp(const _LerpBorder(), const _ReverseLerpToBorder(), 0.25),
53+
const _LerpBorder(0.75),
54+
);
55+
expect(
56+
OutlinedBorder.lerp(const _ReverseLerpFromBorder(), const _LerpBorder(), 0.25),
57+
const _LerpBorder(0.75),
58+
);
59+
});
60+
61+
test('Outlined shape borders interpolate symmetrically', () {
62+
const borders = <OutlinedBorder>[
63+
RoundedRectangleBorder(),
64+
StadiumBorder(),
65+
CircleBorder(),
66+
OvalBorder(),
67+
StarBorder(),
68+
];
69+
70+
for (final a in borders) {
71+
for (final b in borders) {
72+
expect(
73+
ShapeBorder.lerp(a, b, 0.25).runtimeType,
74+
ShapeBorder.lerp(b, a, 0.75).runtimeType,
75+
reason: 'ShapeBorder.lerp($a, $b, 0.25)',
76+
);
77+
expect(
78+
OutlinedBorder.lerp(a, b, 0.25).runtimeType,
79+
OutlinedBorder.lerp(b, a, 0.75).runtimeType,
80+
reason: 'OutlinedBorder.lerp($a, $b, 0.25)',
81+
);
82+
}
83+
}
84+
});
85+
3986
test('Compound borders', () {
4087
final b1 = Border.all(color: const Color(0xFF00FF00));
4188
final b2 = Border.all(color: const Color(0xFF0000FF));
@@ -172,3 +219,64 @@ void main() {
172219
);
173220
});
174221
}
222+
223+
// A border that does not know how to interpolate with any other class, used
224+
// both as the lerp partner of the borders below and as their result, recording
225+
// through value equality the `t` value that the lerp method received.
226+
class _LerpBorder extends OutlinedBorder {
227+
const _LerpBorder([this.t, BorderSide side = BorderSide.none]) : super(side: side);
228+
229+
final double? t;
230+
231+
@override
232+
_LerpBorder copyWith({BorderSide? side}) => _LerpBorder(t, side ?? this.side);
233+
234+
@override
235+
Path getInnerPath(Rect rect, {TextDirection? textDirection}) => Path()..addRect(rect);
236+
237+
@override
238+
Path getOuterPath(Rect rect, {TextDirection? textDirection}) => Path()..addRect(rect);
239+
240+
@override
241+
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {}
242+
243+
@override
244+
ShapeBorder scale(double t) => _LerpBorder(t, side.scale(t));
245+
246+
@override
247+
bool operator ==(Object other) {
248+
return other is _LerpBorder &&
249+
other.runtimeType == runtimeType &&
250+
other.t == t &&
251+
other.side == side;
252+
}
253+
254+
@override
255+
int get hashCode => Object.hash(runtimeType, t, side);
256+
257+
@override
258+
String toString() => '_LerpBorder(t: $t)';
259+
}
260+
261+
// A border that can interpolate with [_LerpBorder] only through its own
262+
// [lerpTo], so that lerping it with a [_LerpBorder] succeeds only through the
263+
// reversed `b.lerpTo(a, 1.0 - t)` call.
264+
class _ReverseLerpToBorder extends _LerpBorder {
265+
const _ReverseLerpToBorder();
266+
267+
@override
268+
ShapeBorder? lerpTo(ShapeBorder? b, double t) {
269+
return b is _LerpBorder ? _LerpBorder(t) : super.lerpTo(b, t);
270+
}
271+
}
272+
273+
// Same as [_ReverseLerpToBorder], but exercising the reversed
274+
// `a.lerpFrom(b, 1.0 - t)` call instead.
275+
class _ReverseLerpFromBorder extends _LerpBorder {
276+
const _ReverseLerpFromBorder();
277+
278+
@override
279+
ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
280+
return a is _LerpBorder ? _LerpBorder(t) : super.lerpFrom(a, t);
281+
}
282+
}

0 commit comments

Comments
 (0)