Fix OverflowBox dry layout when deferToChild overrides the constraints (#196839) `RenderConstrainedOverflowBox.computeDryLayout` with `OverflowBoxFit.deferToChild` asks the child for its dry size under the incoming constraints. `performLayout` lays the child out under `_getInnerConstraints(constraints)` and then constrains the child's size to the incoming constraints. So when `minWidth`, `maxWidth`, `minHeight` or `maxHeight` override the incoming constraints, `getDryLayout` returns a different size from layout, and `debugCheckIntrinsicSizes` reports it. Dry baselines are off as well, because `computeDryBaseline` takes the box's own size from `getDryLayout`. This change makes `computeDryLayout` mirror `performLayout`: the child is dry-laid out under the inner constraints, and the result is constrained to the incoming ones. Fixes https://github.com/flutter/flutter/issues/196838 The new test in `overflow_box_test.dart` covers four cases: - a min above the child's size; - a max below the incoming max; - a tight incoming width; - a child larger than the incoming max. The first three cases fail without the change. The fourth (a child larger than the incoming max) passes either way, since both paths clamp it to 100×30; it guards that clamp. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Prepared with the help of Claude Code.
diff --git a/packages/flutter/lib/src/rendering/shifted_box.dart b/packages/flutter/lib/src/rendering/shifted_box.dart index 6725181..8849d7d 100644 --- a/packages/flutter/lib/src/rendering/shifted_box.dart +++ b/packages/flutter/lib/src/rendering/shifted_box.dart
@@ -727,7 +727,9 @@ Size computeDryLayout(covariant BoxConstraints constraints) { return switch (fit) { OverflowBoxFit.max => constraints.biggest, - OverflowBoxFit.deferToChild => child?.getDryLayout(constraints) ?? constraints.smallest, + OverflowBoxFit.deferToChild => constraints.constrain( + child?.getDryLayout(_getInnerConstraints(constraints)) ?? constraints.smallest, + ), }; }
diff --git a/packages/flutter/test/widgets/overflow_box_test.dart b/packages/flutter/test/widgets/overflow_box_test.dart index 7cccc53..a6b7d42 100644 --- a/packages/flutter/test/widgets/overflow_box_test.dart +++ b/packages/flutter/test/widgets/overflow_box_test.dart
@@ -65,6 +65,66 @@ } }); + testWidgets( + 'dry layout and dry baseline match layout when min or max overrides the incoming constraints', + (WidgetTester tester) async { + // Layout checks the dry size, and paint checks the dry baselines, + // against the laid-out ones while debugCheckIntrinsicSizes is on. + final bool checkIntrinsicSizes = debugCheckIntrinsicSizes; + debugCheckIntrinsicSizes = true; + addTearDown(() => debugCheckIntrinsicSizes = checkIntrinsicSizes); + final cases = <({BoxConstraints incoming, OverflowBox box})>[ + ( + incoming: const BoxConstraints(maxWidth: 400, maxHeight: 400), + box: const OverflowBox( + fit: OverflowBoxFit.deferToChild, + minWidth: 200, + minHeight: 100, + child: Text('Hi', textDirection: TextDirection.ltr), + ), + ), + ( + incoming: const BoxConstraints(maxWidth: 400, maxHeight: 400), + box: const OverflowBox( + fit: OverflowBoxFit.deferToChild, + maxWidth: 100, + child: SizedBox.expand(), + ), + ), + ( + incoming: const BoxConstraints(minWidth: 300, maxWidth: 300, maxHeight: 400), + box: const OverflowBox( + fit: OverflowBoxFit.deferToChild, + minWidth: 0, + maxWidth: 100, + minHeight: 120, + child: SizedBox(width: 50, height: 50), + ), + ), + ( + incoming: const BoxConstraints(maxWidth: 100, maxHeight: 100), + box: const OverflowBox( + fit: OverflowBoxFit.deferToChild, + maxWidth: 500, + maxHeight: 500, + child: SizedBox(width: 300, height: 30), + ), + ), + ]; + for (final testCase in cases) { + await tester.pumpWidget( + Align( + alignment: Alignment.topLeft, + child: ConstrainedBox(constraints: testCase.incoming, child: testCase.box), + ), + ); + expect(tester.takeException(), isNull); + final RenderBox box = tester.renderObject(find.byType(OverflowBox)); + expect(box.getDryLayout(testCase.incoming), box.size); + } + }, + ); + testWidgets('no child', (WidgetTester tester) async { final GlobalKey<State<StatefulWidget>> key = GlobalKey();