diff --git a/packages/flutter/lib/src/widgets/drag_target.dart b/packages/flutter/lib/src/widgets/drag_target.dart index 7d4e484b6f..12e8da4c00 100644 --- a/packages/flutter/lib/src/widgets/drag_target.dart +++ b/packages/flutter/lib/src/widgets/drag_target.dart @@ -124,6 +124,7 @@ class Draggable extends StatefulWidget { this.onDragEnd, this.onDragCompleted, this.ignoringFeedbackSemantics = true, + this.rootOverlay = false, }) : assert(child != null), assert(feedback != null), assert(ignoringFeedbackSemantics != null), @@ -261,6 +262,15 @@ class Draggable extends StatefulWidget { /// the tree (i.e. [State.mounted] is true). final DragEndCallback? onDragEnd; + /// Whether the feedback widget will be put on the root [Overlay]. + /// + /// When false, the feedback widget will be put on the closest [Overlay]. When + /// true, the [feedback] widget will be put on the farthest (aka root) + /// [Overlay]. + /// + /// Defaults to false. + final bool rootOverlay; + /// Creates a gesture recognizer that recognizes the start of the drag. /// /// Subclasses can override this function to customize when they start @@ -390,7 +400,7 @@ class _DraggableState extends State> { _activeCount += 1; }); final _DragAvatar avatar = _DragAvatar( - overlayState: Overlay.of(context, debugRequiredFor: widget)!, + overlayState: Overlay.of(context, debugRequiredFor: widget, rootOverlay: widget.rootOverlay)!, data: widget.data, axis: widget.axis, initialPosition: position, @@ -427,7 +437,7 @@ class _DraggableState extends State> { @override Widget build(BuildContext context) { - assert(Overlay.of(context, debugRequiredFor: widget) != null); + assert(Overlay.of(context, debugRequiredFor: widget, rootOverlay: widget.rootOverlay) != null); final bool canDrag = widget.maxSimultaneousDrags == null || _activeCount < widget.maxSimultaneousDrags!; final bool showChild = _activeCount == 0 || widget.childWhenDragging == null; diff --git a/packages/flutter/test/widgets/draggable_test.dart b/packages/flutter/test/widgets/draggable_test.dart index ab84c3872d..6eda606e8e 100644 --- a/packages/flutter/test/widgets/draggable_test.dart +++ b/packages/flutter/test/widgets/draggable_test.dart @@ -2318,6 +2318,70 @@ void main() { await _testChildAnchorFeedbackPosition(tester: tester, left: 100.0, top: 100.0); }); + testWidgets('Drag feedback is put on root overlay with [rootOverlay] flag', (WidgetTester tester) async { + final GlobalKey rootNavigatorKey = GlobalKey(); + final GlobalKey childNavigatorKey = GlobalKey(); + // Create a [MaterialApp], with a nested [Navigator], which has the + // [Draggable]. + await tester.pumpWidget(MaterialApp( + navigatorKey: rootNavigatorKey, + home: Column( + children: [ + Container( + height: 200.0, + child: Navigator( + key: childNavigatorKey, + onGenerateRoute: (RouteSettings settings) { + if (settings.name == '/') { + return MaterialPageRoute( + settings: settings, + builder: (BuildContext context) => const Draggable( + data: 1, + child: Text('Source'), + feedback: Text('Dragging'), + rootOverlay: true, + ), + ); + } + throw UnsupportedError('Unsupported route: $settings'); + }, + ), + ), + DragTarget( + builder: (BuildContext context, List data, List rejects) { + return Container( + height: 300.0, child: const Center(child: Text('Target 1')), + ); + }, + ), + ], + ), + )); + + final Offset firstLocation = tester.getCenter(find.text('Source')); + final TestGesture gesture = + await tester.startGesture(firstLocation, pointer: 7); + await tester.pump(); + + final Offset secondLocation = tester.getCenter(find.text('Target 1')); + await gesture.moveTo(secondLocation); + await tester.pump(); + + // Expect that the feedback widget is a descendant of the root overlay, + // but not a descendant of the child overlay. + expect( + find.descendant( + of: find.byType(Overlay).first, + matching: find.text('Dragging'), + ), + findsOneWidget); + expect( + find.descendant( + of: find.byType(Overlay).last, + matching: find.text('Dragging'), + ), + findsNothing); + }); testWidgets('Drag and drop can contribute semantics', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester);