Skip to content

Commit

Permalink
Fix for negative padding from a curve in AnimatedPadding. (flutter#52072
Browse files Browse the repository at this point in the history
)

* Fix for negative padding from a curve in AnimatedPadding.

* Added a EdgeInsets.clamp function that would return an EdgeInsets instance instead of a _MixedEdgeInsets used by the base class. This was causing some tests to fail that didn't have a text direction.
  • Loading branch information
darrenaustin authored Mar 6, 2020
1 parent 2e18cd3 commit 1c3ebad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/flutter/lib/src/painting/edge_insets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,16 @@ class EdgeInsets extends EdgeInsetsGeometry {
return super.add(other);
}

@override
EdgeInsetsGeometry clamp(EdgeInsetsGeometry min, EdgeInsetsGeometry max) {
return EdgeInsets.fromLTRB(
_left.clamp(min._left, max._left) as double,
_top.clamp(min._top, max._top) as double,
_right.clamp(min._right, max._right) as double,
_bottom.clamp(min._bottom, max._bottom) as double,
);
}

/// Returns the difference between two [EdgeInsets].
EdgeInsets operator -(EdgeInsets other) {
return EdgeInsets.fromLTRB(
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/widgets/implicit_animations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,9 @@ class _AnimatedPaddingState extends AnimatedWidgetBaseState<AnimatedPadding> {
@override
Widget build(BuildContext context) {
return Padding(
padding: _padding.evaluate(animation),
padding: _padding
.evaluate(animation)
.clamp(EdgeInsets.zero, EdgeInsetsGeometry.infinity),
child: widget.child,
);
}
Expand Down
40 changes: 40 additions & 0 deletions packages/flutter/test/widgets/animated_padding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,44 @@ void main() {
expect(tester.getSize(find.byKey(target)), const Size(700.0, 600.0));
expect(tester.getTopRight(find.byKey(target)), const Offset(700.0, 0.0));
});

testWidgets('AnimatedPadding animated padding clamped to positive values', (WidgetTester tester) async {
final Key target = UniqueKey();

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.rtl,
child: AnimatedPadding(
curve: Curves.easeInOutBack, // will cause negative padding during overshoot
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.only(right: 50.0),
child: SizedBox.expand(key: target),
),
),
);

expect(tester.getSize(find.byKey(target)), const Size(750.0, 600.0));
expect(tester.getTopRight(find.byKey(target)), const Offset(750.0, 0.0));

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.rtl,
child: AnimatedPadding(
curve: Curves.easeInOutBack,
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.only(right: 0.0),
child: SizedBox.expand(key: target),
),
),
);

expect(tester.getSize(find.byKey(target)), const Size(750.0, 600.0));
expect(tester.getTopRight(find.byKey(target)), const Offset(750.0, 0.0));

await tester.pump(const Duration(milliseconds: 128));
// Curve would have made the padding negative a this point if it is not clamped.
expect(tester.getSize(find.byKey(target)), const Size(800.0, 600.0));
expect(tester.getTopRight(find.byKey(target)), const Offset(800.0, 0.0));
});

}

0 comments on commit 1c3ebad

Please sign in to comment.