diff --git a/packages/flutter/lib/src/widgets/lookup_boundary.dart b/packages/flutter/lib/src/widgets/lookup_boundary.dart index c0aaf56a54..98c16ce658 100644 --- a/packages/flutter/lib/src/widgets/lookup_boundary.dart +++ b/packages/flutter/lib/src/widgets/lookup_boundary.dart @@ -47,14 +47,15 @@ import 'framework.dart'; /// tree, which is rather uncommon. Such anomalies are created by /// [RenderObjectElement]s that don't attach their [RenderObject] to the closest /// ancestor [RenderObjectElement], e.g. because they bootstrap a separate -/// stand-alone render tree. -// TODO(goderbauer): Reference the View widget here once available. -/// This behavior breaks the assumption some widgets have about the structure of -/// the render tree: These widgets may try to reach out to an ancestor widget, -/// assuming that their associated [RenderObject]s are also ancestors, which due -/// to the anomaly may not be the case. At the point where the divergence in the -/// two trees is introduced, a [LookupBoundary] can be used to hide that ancestor -/// from the querying widget. +/// stand-alone render tree. This behavior breaks the assumption some widgets +/// have about the structure of the render tree: These widgets may try to reach +/// out to an ancestor widget, assuming that their associated [RenderObject]s +/// are also ancestors, which due to the anomaly may not be the case. At the +/// point where the divergence in the two trees is introduced, a +/// [LookupBoundary] can be used to hide that ancestor from the querying widget. +/// The [ViewAnchor], for example, wraps its [ViewAnchor.view] child in a +/// [LookupBoundary] because the [RenderObject] produced by that widget subtree +/// is not attached to the render tree that the [ViewAnchor] itself belongs to. /// /// As an example, [Material.of] relies on lookup boundaries to hide the /// [Material] widget from certain descendant button widget. Buttons reach out diff --git a/packages/flutter/lib/src/widgets/view.dart b/packages/flutter/lib/src/widgets/view.dart index 90e06cb4c5..7d4de2a957 100644 --- a/packages/flutter/lib/src/widgets/view.dart +++ b/packages/flutter/lib/src/widgets/view.dart @@ -597,7 +597,7 @@ class _RawViewElement extends RenderTreeRootElement { class _ViewScope extends InheritedWidget { const _ViewScope({required this.view, required super.child}); - final FlutterView? view; + final FlutterView view; @override bool updateShouldNotify(_ViewScope oldWidget) => view != oldWidget.view; @@ -713,8 +713,7 @@ class ViewAnchor extends StatelessWidget { return _MultiChildComponentWidget( views: [ if (view != null) - _ViewScope( - view: null, + LookupBoundary( child: view!, ), ], diff --git a/packages/flutter/test/material/material_test.dart b/packages/flutter/test/material/material_test.dart index 78dc7af1ff..2d0cc94615 100644 --- a/packages/flutter/test/material/material_test.dart +++ b/packages/flutter/test/material/material_test.dart @@ -11,6 +11,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; +import '../widgets/multi_view_testing.dart'; import '../widgets/test_border.dart' show TestBorder; class NotifyMaterial extends StatelessWidget { @@ -1241,6 +1242,43 @@ void main() { ); }); }); + + testWidgets('Material is not visible from sub-views', (WidgetTester tester) async { + MaterialInkController? outsideView; + MaterialInkController? insideView; + MaterialInkController? outsideViewAnchor; + + await tester.pumpWidget( + Material( + child: Builder( + builder: (BuildContext context) { + outsideViewAnchor = Material.maybeOf(context); + return ViewAnchor( + view: Builder( + builder: (BuildContext context) { + outsideView = Material.maybeOf(context); + return View( + view: FakeView(tester.view), + child: Builder( + builder: (BuildContext context) { + insideView = Material.maybeOf(context); + return const SizedBox(); + }, + ), + ); + }, + ), + child: const SizedBox(), + ); + }, + ), + ), + ); + + expect(outsideViewAnchor, isNotNull); + expect(outsideView, isNull); + expect(insideView, isNull); + }); } class TrackPaintInkFeature extends InkFeature { diff --git a/packages/flutter/test/widgets/overlay_test.dart b/packages/flutter/test/widgets/overlay_test.dart index 144ffdac7a..03bc4b7afd 100644 --- a/packages/flutter/test/widgets/overlay_test.dart +++ b/packages/flutter/test/widgets/overlay_test.dart @@ -3,10 +3,12 @@ // found in the LICENSE file. import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; +import 'multi_view_testing.dart'; import 'semantics_tester.dart'; void main() { @@ -1758,6 +1760,46 @@ void main() { contains('Overlay was given infinite constraints and cannot be sized by a suitable child.'), ); }); + + testWidgets('Overlay is not visible from sub-views', (WidgetTester tester) async { + OverlayState? outsideView; + OverlayState? insideView; + OverlayState? outsideViewAnchor; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Overlay.wrap( + child: Builder( + builder: (BuildContext context) { + outsideViewAnchor = Overlay.maybeOf(context); + return ViewAnchor( + view: Builder( + builder: (BuildContext context) { + outsideView = Overlay.maybeOf(context); + return View( + view: FakeView(tester.view), + child: Builder( + builder: (BuildContext context) { + insideView = Overlay.maybeOf(context); + return const SizedBox(); + }, + ), + ); + }, + ), + child: const SizedBox(), + ); + }, + ), + ), + ), + ); + + expect(outsideViewAnchor, isNotNull); + expect(outsideView, isNull); + expect(insideView, isNull); + }); } class StatefulTestWidget extends StatefulWidget {