diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 795d7da96a..afb55b73f0 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -322,10 +322,12 @@ class BoxConstraints extends Constraints { return result; } + if (size.isEmpty) { + return constrain(size); + } + double width = size.width; double height = size.height; - assert(width > 0.0); - assert(height > 0.0); final double aspectRatio = width / height; if (width > maxWidth) { diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 38830fc479..2b29071998 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -2711,24 +2711,6 @@ class RenderFittedBox extends RenderProxyBox { if (child != null) { final Size childSize = child!.getDryLayout(const BoxConstraints()); - // During [RenderObject.debugCheckingIntrinsics] a child that doesn't - // support dry layout may provide us with an invalid size that triggers - // assertions if we try to work with it. Instead of throwing, we bail - // out early in that case. - bool invalidChildSize = false; - assert(() { - if (RenderObject.debugCheckingIntrinsics && childSize.width * childSize.height == 0.0) { - invalidChildSize = true; - } - return true; - }()); - if (invalidChildSize) { - assert(debugCannotComputeDryLayout( - reason: 'Child provided invalid size of $childSize.', - )); - return Size.zero; - } - switch (fit) { case BoxFit.scaleDown: final BoxConstraints sizeConstraints = constraints.loosen(); diff --git a/packages/flutter/test/rendering/box_constraints_test.dart b/packages/flutter/test/rendering/box_constraints_test.dart index 6c6cbe1445..7409e1258b 100644 --- a/packages/flutter/test/rendering/box_constraints_test.dart +++ b/packages/flutter/test/rendering/box_constraints_test.dart @@ -182,4 +182,17 @@ void main() { expect(constraints, const BoxConstraints(minWidth: 1, maxWidth: 2, minHeight: 3, maxHeight: 4)); }); + test('BoxConstraints.constrainSizeAndAttemptToPreserveAspectRatio can handle empty size', () { + const BoxConstraints constraints = BoxConstraints( + minWidth: 10.0, + maxWidth: 20.0, + minHeight: 10.0, + maxHeight: 20.0, + ); + const Size unconstrainedSize = Size(15.0, 0.0); + final Size constrainedSize = constraints.constrainSizeAndAttemptToPreserveAspectRatio( + unconstrainedSize, + ); + expect(constrainedSize, const Size(15.0, 10.0)); + }); } diff --git a/packages/flutter/test/widgets/fitted_box_test.dart b/packages/flutter/test/widgets/fitted_box_test.dart index bc5d30cb42..45f4e1ce4d 100644 --- a/packages/flutter/test/widgets/fitted_box_test.dart +++ b/packages/flutter/test/widgets/fitted_box_test.dart @@ -605,6 +605,38 @@ void main() { await tester.tap(find.byType(FittedBox), warnIfMissed: false); expect(tester.takeException(), isNull); }); + + // Regression test for https://github.com/flutter/flutter/issues/135082 + testWidgets('FittedBox with zero size child does not throw', (WidgetTester tester) async { + await tester.pumpWidget( + const Center( + child: SizedBox( + height: 200.0, + width: 200.0, + child: FittedBox( + fit: BoxFit.scaleDown, + child: SizedBox.shrink(), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + + await tester.pumpWidget( + Center( + child: ConstrainedBox( + constraints: const BoxConstraints( + maxWidth: 200.0, + maxHeight: 200.0, + ), + child: const FittedBox( + child: SizedBox.shrink(), + ), + ), + ), + ); + expect(tester.takeException(), isNull); + }); } List getLayers() {