Skip to content

Commit a3ee5e3

Browse files
Fix OverlayPortal layout in shrink-wrapped overlays (flutter#193079)
When an `Overlay` sizes itself to its content, `_RenderLayoutSurrogateProxyBox.performLayout` re-derived the constraints for its deferred child instead of using the ones the theater actually uses: ```dart final BoxConstraints theaterConstraints = theater.constraints; final Size boxSize = theaterConstraints.biggest.isFinite ? theaterConstraints.biggest : theater.size; ``` This is wrong in two ways: 1. In the unbounded case it reads `theater.size` from a descendant mid-layout, which trips `RenderBox.size accessed beyond the scope of resize, layout, or permitted parent access` (`sizeAccessAllowed`). This is the crash in flutter#192861. 2. When the overlay has `alwaysSizeToContent: true` *and* bounded incoming constraints, it falls into the first branch and uses `constraints.biggest` — but the theater sized itself to its content, so the overlay child is laid out against constraints the theater never used. There is no crash here, just a silently mis-sized overlay child. `_RenderTheater.performLayout` already computes the correct value for its non-positioned children. This PR caches it in `_nonPositionedChildConstraints` and has the surrogate reuse it, so there is a single source of truth and no ancestor size read. | Theater state | Before | After | | --- | --- | --- | | Bounded, `alwaysSizeToContent: false` | `constraints.biggest` | unchanged | | Unbounded | `theater.size` (illegal read) | same value, no illegal read | | Bounded, `alwaysSizeToContent: true` | `constraints.biggest` (wrong) | content size | The field is assigned before any non-size-determining child is laid out. It cannot be read before that assignment: the theater is always an ancestor of its layout surrogates, so a surrogate's first layout is only reachable from `_RenderTheater.performLayout`, and the only subtree laid out before the assignment is the size-determining one, which the existing `_layingOutSizeDeterminingChild` guard already skips. I added a comment recording this, since the invariant spans two classes. This also restores the `assert(size == theater.size)` invariant in `_RenderDeferredLayoutBox`, which case 2 above could violate. Fixes flutter#192861 ## Tests Added a regression test to `overlay_portal_test.dart`, run over both unbounded and `alwaysSizeToContent` overlays, covering open → resize while open → close → reopen, with `verifyTreeIsClean()` at each step. Without the source change, the unbounded variant throws the assertion from the issue and the bounded variant sizes the overlay child to 800x600 instead of 100x100. Local results: all 7331 tests in `packages/flutter/test/widgets/` pass, plus `dropdown_menu_test.dart`, `dropdown_test.dart`, `menu_anchor_test.dart` and `tooltip_test.dart`. The only failures I saw (`widget_inspector` screenshot, two dropdown goldens) reproduce identically on an unmodified checkout. ## 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/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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 [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
1 parent fca9fd9 commit a3ee5e3

2 files changed

Lines changed: 115 additions & 9 deletions

File tree

‎packages/flutter/lib/src/widgets/overlay.dart‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ class _RenderTheater extends RenderBox
13911391
final Size size = !alwaysSizeToContent && constraints.biggest.isFinite
13921392
? constraints.biggest
13931393
: _findSizeDeterminingChild().getDryLayout(constraints);
1394-
final nonPositionedChildConstraints = BoxConstraints.tight(size);
1394+
final dryChildConstraints = BoxConstraints.tight(size);
13951395
final Alignment alignment = theater._resolvedAlignment;
13961396

13971397
BaselineOffset baselineOffset = BaselineOffset.noBaseline;
@@ -1401,7 +1401,7 @@ class _RenderTheater extends RenderBox
14011401
_RenderTheaterMixin.baselineForChild(
14021402
child,
14031403
size,
1404-
nonPositionedChildConstraints,
1404+
dryChildConstraints,
14051405
alignment,
14061406
baseline,
14071407
),
@@ -1460,6 +1460,16 @@ class _RenderTheater extends RenderBox
14601460
bool get sizedByParent => false;
14611461

14621462
bool _layingOutSizeDeterminingChild = false;
1463+
1464+
// The `BoxConstraints` to use for laying out `_DeferredLayoutBox`es and other
1465+
// non-positioned and non-size-determining children.
1466+
//
1467+
// Adding a `_DeferredLayoutBox` child does not trigger a `_RenderTheater`
1468+
// relayout, so `_RenderTheater` must cache this value for
1469+
// `_RenderLayoutSurrogateProxyBox.performLayout` to set the correct
1470+
// `BoxConstraints` on the newly added `_DeferredLayoutBox`.
1471+
late BoxConstraints _nonPositionedChildConstraints;
1472+
14631473
@override
14641474
void performLayout() {
14651475
RenderBox? sizeDeterminingChild;
@@ -1474,10 +1484,10 @@ class _RenderTheater extends RenderBox
14741484
}
14751485

14761486
// Equivalent to BoxConstraints used by RenderStack for StackFit.expand.
1477-
final nonPositionedChildConstraints = BoxConstraints.tight(size);
1487+
_nonPositionedChildConstraints = BoxConstraints.tight(size);
14781488
for (final RenderBox child in _childrenInPaintOrder()) {
14791489
if (child != sizeDeterminingChild) {
1480-
layoutChild(child, nonPositionedChildConstraints);
1490+
layoutChild(child, _nonPositionedChildConstraints);
14811491
}
14821492
}
14831493
}
@@ -2796,11 +2806,7 @@ class _RenderLayoutSurrogateProxyBox extends RenderProxyBox {
27962806
// constraints of the deferred child and resize / put it in the dirty list if
27972807
// needed.
27982808
if (!theater._layingOutSizeDeterminingChild) {
2799-
final BoxConstraints theaterConstraints = theater.constraints;
2800-
final Size boxSize = theaterConstraints.biggest.isFinite
2801-
? theaterConstraints.biggest
2802-
: theater.size;
2803-
deferredChild._doLayoutFrom(this, constraints: BoxConstraints.tight(boxSize));
2809+
deferredChild._doLayoutFrom(this, constraints: theater._nonPositionedChildConstraints);
28042810
}
28052811
}
28062812
}

‎packages/flutter/test/widgets/overlay_portal_test.dart‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,106 @@ void main() {
662662
expect(tester.takeException(), isNull);
663663
});
664664

665+
for (final bounded in <bool>[false, true]) {
666+
for (final useLayoutBuilder in <bool>[false, true]) {
667+
for (final tightAnchor in <bool>[false, true]) {
668+
testWidgets('OverlayPortal opens in a shrink-wrapped Overlay '
669+
'(bounded: $bounded, layoutBuilder: $useLayoutBuilder, tightAnchor: $tightAnchor)', (
670+
WidgetTester tester,
671+
) async {
672+
// Regression test for https://github.com/flutter/flutter/issues/192861.
673+
final controller = OverlayPortalController();
674+
const overlayChildKey = ValueKey<String>('overlay child');
675+
const anchorDimension = 40.0;
676+
double dimension = 100;
677+
OverlayChildLayoutInfo? layoutInfo;
678+
679+
Widget portal = useLayoutBuilder
680+
? OverlayPortal.overlayChildLayoutBuilder(
681+
controller: controller,
682+
overlayChildBuilder: (BuildContext context, OverlayChildLayoutInfo info) {
683+
layoutInfo = info;
684+
return const SizedBox(key: overlayChildKey);
685+
},
686+
child: const SizedBox.square(dimension: anchorDimension),
687+
)
688+
: OverlayPortal(
689+
controller: controller,
690+
overlayChildBuilder: (BuildContext context) =>
691+
const SizedBox(key: overlayChildKey),
692+
child: const SizedBox.square(dimension: anchorDimension),
693+
);
694+
if (tightAnchor) {
695+
// Tight constraints make the layout surrogate its own relayout
696+
// boundary instead of a shallower ancestor, so layout starts at
697+
// the surrogate and it lays out the deferred child from the
698+
// theater's cached constraints.
699+
portal = SizedBox.square(dimension: anchorDimension, child: portal);
700+
}
701+
702+
final overlayEntry = OverlayEntry(
703+
canSizeOverlay: true,
704+
builder: (BuildContext context) {
705+
return SizedBox.square(
706+
dimension: dimension,
707+
child: Center(child: portal),
708+
);
709+
},
710+
);
711+
addTearDown(
712+
() => overlayEntry
713+
..remove()
714+
..dispose(),
715+
);
716+
final Widget overlay = Overlay(
717+
alwaysSizeToContent: bounded,
718+
initialEntries: <OverlayEntry>[overlayEntry],
719+
);
720+
await tester.pumpWidget(
721+
Directionality(
722+
textDirection: TextDirection.ltr,
723+
child: bounded ? Center(child: overlay) : UnconstrainedBox(child: overlay),
724+
),
725+
);
726+
expect(tester.getSize(find.byType(Overlay)), const Size.square(100));
727+
expect(find.byKey(overlayChildKey), findsNothing);
728+
729+
controller.show();
730+
await tester.pump();
731+
expect(tester.takeException(), isNull);
732+
expect(tester.getSize(find.byKey(overlayChildKey)), const Size.square(100));
733+
if (useLayoutBuilder) {
734+
expect(layoutInfo?.overlaySize, const Size.square(100));
735+
expect(layoutInfo?.childSize, const Size.square(anchorDimension));
736+
}
737+
verifyTreeIsClean();
738+
739+
dimension = 200;
740+
overlayEntry.markNeedsBuild();
741+
await tester.pump();
742+
expect(tester.takeException(), isNull);
743+
expect(tester.getSize(find.byType(Overlay)), const Size.square(200));
744+
expect(tester.getSize(find.byKey(overlayChildKey)), const Size.square(200));
745+
if (useLayoutBuilder) {
746+
expect(layoutInfo?.overlaySize, const Size.square(200));
747+
expect(layoutInfo?.childSize, const Size.square(anchorDimension));
748+
}
749+
verifyTreeIsClean();
750+
751+
controller.hide();
752+
await tester.pump();
753+
expect(find.byKey(overlayChildKey), findsNothing);
754+
755+
controller.show();
756+
await tester.pump();
757+
expect(tester.takeException(), isNull);
758+
expect(tester.getSize(find.byKey(overlayChildKey)), const Size.square(200));
759+
verifyTreeIsClean();
760+
});
761+
}
762+
}
763+
}
764+
665765
testWidgets('show/hide works', (WidgetTester tester) async {
666766
late final OverlayEntry overlayEntry;
667767
addTearDown(

0 commit comments

Comments
 (0)