Skip to content

Commit 625854c

Browse files
committed
Fix owner-mismatch crash for OverlayPortal inside a deferred-mount parent
`_RenderDeferredLayoutBox.redepthChildren` unconditionally called `_layoutSurrogate.redepthChild(this)`. When a parent like `Table` defers adopting its render-object children until every row has been mounted, the deferred-layout box is adopted by the theater (and gains a pipeline `owner`) while the layout surrogate is still un-attached, so the `child.owner == owner` assertion fires. The opposite direction in `_RenderLayoutSurrogateProxyBox.redepthChildren` already guards on `child.attached`. Mirror that guard here. The depth invariant is restored when the surrogate is finally attached, because the surrogate's own `redepthChildren` re-depths this box. Adds a regression test placing an `OverlayPortal` (with `controller.show()` called before mount, mirroring `Slider`) inside a `TableRow` and verifying neither the assertion nor an exception fires and the overlay child is present. Fixes flutter#174133 Fixes flutter#180337
1 parent c27ddc2 commit 625854c

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2573,7 +2573,17 @@ final class _RenderDeferredLayoutBox extends RenderProxyBox
25732573

25742574
@override
25752575
void redepthChildren() {
2576-
_layoutSurrogate.redepthChild(this);
2576+
// The layout surrogate may not be attached yet — e.g. when a parent like
2577+
// [Table] defers calling [adoptChild] on its [RenderObject] children until
2578+
// every row has been mounted, this deferred box is adopted by the theater
2579+
// (and gets an [owner]) before the surrogate has been adopted by its own
2580+
// parent. Calling [redepthChild] across that owner boundary trips the
2581+
// `child.owner == owner` assertion. The depth invariant is restored when
2582+
// the surrogate is finally attached: [_RenderLayoutSurrogateProxyBox]
2583+
// already redepths this box from its own [redepthChildren].
2584+
if (_layoutSurrogate.attached) {
2585+
_layoutSurrogate.redepthChild(this);
2586+
}
25772587
super.redepthChildren();
25782588
}
25792589

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,53 @@ void main() {
921921
);
922922
});
923923

924+
// Regression test for https://github.com/flutter/flutter/issues/174133
925+
// (and duplicate https://github.com/flutter/flutter/issues/180337). [Table]
926+
// defers adopting its render-object children until every row has been
927+
// mounted, which means an [OverlayPortal] cell mounts its overlay child
928+
// (and the overlay child gets an owner) before the OverlayPortal's own
929+
// layout-surrogate render object has been adopted by its parent.
930+
// [_RenderDeferredLayoutBox.redepthChildren] used to call
931+
// `_layoutSurrogate.redepthChild(this)` unconditionally and trip a
932+
// `child.owner == owner` assertion in that window.
933+
testWidgets('OverlayPortal child inside a TableRow does not crash', (
934+
WidgetTester tester,
935+
) async {
936+
// Mirrors how Slider uses OverlayPortal: controller.show() runs in a
937+
// field initializer, before the OverlayPortal widget is mounted.
938+
final controller = OverlayPortalController()..show();
939+
const overlayKey = Key('overlay-child');
940+
await tester.pumpWidget(
941+
Directionality(
942+
textDirection: TextDirection.ltr,
943+
child: Overlay(
944+
initialEntries: <OverlayEntry>[
945+
OverlayEntry(
946+
builder: (BuildContext context) {
947+
return Table(
948+
children: <TableRow>[
949+
TableRow(
950+
children: <Widget>[
951+
OverlayPortal(
952+
controller: controller,
953+
overlayChildBuilder: (BuildContext context) =>
954+
const SizedBox(key: overlayKey, width: 10, height: 10),
955+
child: const SizedBox(width: 10, height: 10),
956+
),
957+
],
958+
),
959+
],
960+
);
961+
},
962+
),
963+
],
964+
),
965+
),
966+
);
967+
expect(tester.takeException(), isNull);
968+
expect(find.byKey(overlayKey), findsOneWidget);
969+
});
970+
924971
testWidgets('Set defaultVerticalAlignment to intrinsic height and check their heights', (
925972
WidgetTester tester,
926973
) async {

0 commit comments

Comments
 (0)