Skip to content

Commit aa2f375

Browse files
authored
Normalize lerped BoxConstraints with overshooting t values (flutter#190971)
BoxConstraints.lerp has the signature ```dart static BoxConstraints? lerp(BoxConstraints? a, BoxConstraints? b, double t) ``` Overshooting curves, such as `Curves.easeOutBack`, extrapolate t outside of the range 0.0 to 1.0. Without normalizing, the results can be invalid, causing crashes. This PR normalizes the returned `BoxConstraints`. Fixes [AnimatedContainer throws Exception when animating height with easeOutBack curve.](flutter#37559)
1 parent 81d55c9 commit aa2f375

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎packages/flutter/lib/src/rendering/box.dart‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,16 +478,21 @@ class BoxConstraints extends Constraints {
478478
/// If either is null, this function interpolates from a [BoxConstraints]
479479
/// object whose fields are all set to 0.0.
480480
///
481+
/// The result is [normalize]d, so that a `t` value outside of the range 0.0
482+
/// to 1.0 (for example from an overshooting [Curve] such as
483+
/// [Curves.easeOutBack]) never produces constraints with negative or
484+
/// contradictory values.
485+
///
481486
/// {@macro dart.ui.shadow.lerp}
482487
static BoxConstraints? lerp(BoxConstraints? a, BoxConstraints? b, double t) {
483488
if (identical(a, b)) {
484489
return a;
485490
}
486491
if (a == null) {
487-
return b! * t;
492+
return (b! * t).normalize();
488493
}
489494
if (b == null) {
490-
return a * (1.0 - t);
495+
return (a * (1.0 - t)).normalize();
491496
}
492497
assert(a.debugAssertIsValid());
493498
assert(b.debugAssertIsValid());
@@ -520,7 +525,7 @@ class BoxConstraints extends Constraints {
520525
maxHeight: a.maxHeight.isFinite
521526
? ui.lerpDouble(a.maxHeight, b.maxHeight, t)!
522527
: double.infinity,
523-
);
528+
).normalize();
524529
}
525530

526531
/// Returns whether the object's constraints are normalized.

‎packages/flutter/test/rendering/box_constraints_test.dart‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,58 @@ void main() {
131131
expect(() => BoxConstraints.lerp(constraints2, constraints3, 0.5), throwsAssertionError);
132132
});
133133

134+
test('BoxConstraints lerp with overshooting t is normalized', () {
135+
// Regression test for https://github.com/flutter/flutter/issues/37559.
136+
expect(
137+
BoxConstraints.lerp(
138+
const BoxConstraints.tightFor(height: 25.0),
139+
const BoxConstraints.tightFor(height: 0.0),
140+
1.2,
141+
),
142+
const BoxConstraints.tightFor(height: 0.0),
143+
);
144+
expect(
145+
BoxConstraints.lerp(
146+
BoxConstraints.tight(const Size(10.0, 10.0)),
147+
const BoxConstraints(maxWidth: 100.0, maxHeight: 100.0),
148+
-0.1,
149+
)!.isNormalized,
150+
isTrue,
151+
);
152+
expect(
153+
BoxConstraints.lerp(
154+
BoxConstraints.tight(const Size(10.0, 10.0)),
155+
const BoxConstraints(maxWidth: 100.0, maxHeight: 100.0),
156+
-0.1,
157+
),
158+
BoxConstraints.tight(const Size(11.0, 11.0)),
159+
);
160+
expect(
161+
BoxConstraints.lerp(
162+
null,
163+
const BoxConstraints.tightFor(width: 10.0, height: 10.0),
164+
-0.5,
165+
)!.isNormalized,
166+
isTrue,
167+
);
168+
expect(
169+
BoxConstraints.lerp(null, const BoxConstraints.tightFor(width: 10.0, height: 10.0), -0.5),
170+
const BoxConstraints.tightFor(width: 0.0, height: 0.0),
171+
);
172+
expect(
173+
BoxConstraints.lerp(
174+
const BoxConstraints.tightFor(width: 10.0, height: 10.0),
175+
null,
176+
1.5,
177+
)!.isNormalized,
178+
isTrue,
179+
);
180+
expect(
181+
BoxConstraints.lerp(const BoxConstraints.tightFor(width: 10.0, height: 10.0), null, 1.5),
182+
const BoxConstraints.tightFor(width: 0.0, height: 0.0),
183+
);
184+
});
185+
134186
test('BoxConstraints normalize', () {
135187
const constraints = BoxConstraints(
136188
minWidth: 3.0,

0 commit comments

Comments
 (0)