From 9e0ad9b6b5a0c69a152d0b9eddad6f7309fe46f2 Mon Sep 17 00:00:00 2001 From: Andre Date: Tue, 18 Aug 2020 01:36:02 -0400 Subject: [PATCH] Fix FittedBox BoxFit.scaleDown sizing (#63668) --- .../flutter/lib/src/rendering/proxy_box.dart | 29 ++++- .../flutter/test/widgets/fitted_box_test.dart | 105 ++++++++++++++++++ 2 files changed, 131 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 99be07ba7b..b83f1b1054 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -2352,6 +2352,15 @@ class RenderFittedBox extends RenderProxyBox { markNeedsPaint(); } + bool _fitAffectsLayout(BoxFit fit) { + switch (fit) { + case BoxFit.scaleDown: + return true; + default: + return false; + } + } + /// How to inscribe the child into the space allocated during layout. BoxFit get fit => _fit; BoxFit _fit; @@ -2359,9 +2368,14 @@ class RenderFittedBox extends RenderProxyBox { assert(value != null); if (_fit == value) return; + final BoxFit lastFit = _fit; _fit = value; - _clearPaintData(); - markNeedsPaint(); + if (_fitAffectsLayout(lastFit) || _fitAffectsLayout(value)) { + markNeedsLayout(); + } else { + _clearPaintData(); + markNeedsPaint(); + } } /// How to align the child within its parent's bounds. @@ -2403,7 +2417,16 @@ class RenderFittedBox extends RenderProxyBox { void performLayout() { if (child != null) { child.layout(const BoxConstraints(), parentUsesSize: true); - size = constraints.constrainSizeAndAttemptToPreserveAspectRatio(child.size); + switch (fit) { + case BoxFit.scaleDown: + final BoxConstraints sizeConstraints = constraints.loosen(); + final Size unconstrainedSize = sizeConstraints.constrainSizeAndAttemptToPreserveAspectRatio(child.size); + size = constraints.constrain(unconstrainedSize); + break; + default: + size = constraints.constrainSizeAndAttemptToPreserveAspectRatio(child.size); + break; + } _clearPaintData(); } else { size = constraints.smallest; diff --git a/packages/flutter/test/widgets/fitted_box_test.dart b/packages/flutter/test/widgets/fitted_box_test.dart index 37f2261e09..476d5be9b6 100644 --- a/packages/flutter/test/widgets/fitted_box_test.dart +++ b/packages/flutter/test/widgets/fitted_box_test.dart @@ -486,6 +486,111 @@ void main() { await tester.pumpWidget(FittedBox(fit: BoxFit.none, child: Container(), clipBehavior: Clip.antiAlias)); expect(renderObject.clipBehavior, equals(Clip.antiAlias)); }); + + testWidgets('BoxFit.scaleDown matches size of child', (WidgetTester tester) async { + final Key outside = UniqueKey(); + final Key inside = UniqueKey(); + + // Does not scale up when child is smaller than constraints + + await tester.pumpWidget( + Center( + child: Container( + width: 200.0, + child: FittedBox( + key: outside, + fit: BoxFit.scaleDown, + child: Container( + key: inside, + width: 100.0, + height: 50.0, + ), + ), + ), + ), + ); + + final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside)); + final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside)); + + expect(outsideBox.size.width, 200.0); + expect(outsideBox.size.height, 50.0); + + Offset outsidePoint = outsideBox.localToGlobal(Offset.zero); + Offset insidePoint = insideBox.localToGlobal(Offset.zero); + expect(insidePoint - outsidePoint, equals(const Offset(50.0, 0.0))); + + // Scales down when child is bigger than constraints + + await tester.pumpWidget( + Center( + child: Container( + width: 200.0, + child: FittedBox( + key: outside, + fit: BoxFit.scaleDown, + child: Container( + key: inside, + width: 400.0, + height: 200.0, + ), + ), + ), + ), + ); + + expect(outsideBox.size.width, 200.0); + expect(outsideBox.size.height, 100.0); + + outsidePoint = outsideBox.localToGlobal(Offset.zero); + insidePoint = insideBox.localToGlobal(Offset.zero); + + expect(insidePoint - outsidePoint, equals(Offset.zero)); + }); + + testWidgets('Switching to and from BoxFit.scaleDown causes relayout', (WidgetTester tester) async { + final Key outside = UniqueKey(); + + final Widget scaleDownWidget = Center( + child: Container( + width: 200.0, + child: FittedBox( + key: outside, + fit: BoxFit.scaleDown, + child: Container( + width: 100.0, + height: 50.0, + ), + ), + ), + ); + + final Widget coverWidget = Center( + child: Container( + width: 200.0, + child: FittedBox( + key: outside, + child: Container( + width: 100.0, + height: 50.0, + ), + ), + ), + ); + + await tester.pumpWidget(scaleDownWidget); + + final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside)); + expect(outsideBox.size.height, 50.0); + + await tester.pumpWidget(coverWidget); + + expect(outsideBox.size.height, 100.0); + + await tester.pumpWidget(scaleDownWidget); + + expect(outsideBox.size.height, 50.0); + }); } List getLayers() {