From 4a1002e2e25543c370dc227c4f6162cf908b789c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ku=C3=9F?= Date: Sun, 26 Mar 2023 22:55:12 +0200 Subject: [PATCH] Fix selection toolbar not showing on drag end (#121110) --- .../lib/src/widgets/selectable_region.dart | 1 + .../test/material/selection_area_test.dart | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/packages/flutter/lib/src/widgets/selectable_region.dart b/packages/flutter/lib/src/widgets/selectable_region.dart index 4c65c9dbbc4..c8d56898edd 100644 --- a/packages/flutter/lib/src/widgets/selectable_region.dart +++ b/packages/flutter/lib/src/widgets/selectable_region.dart @@ -537,6 +537,7 @@ class SelectableRegionState extends State with TextSelectionDe } else { _selectionOverlay!.hideMagnifier(); _selectionOverlay!.showToolbar( + context: context, contextMenuBuilder: (BuildContext context) { return widget.contextMenuBuilder!(context, this); }, diff --git a/packages/flutter/test/material/selection_area_test.dart b/packages/flutter/test/material/selection_area_test.dart index 27e8b63856e..e81281218bc 100644 --- a/packages/flutter/test/material/selection_area_test.dart +++ b/packages/flutter/test/material/selection_area_test.dart @@ -129,4 +129,53 @@ void main() { expect(content, isNotNull); expect(content!.plainText, 'How'); }); + + testWidgets('stopping drag of end handle will show the toolbar', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/119314 + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Padding( + padding: const EdgeInsets.only(top: 64), + child: Column( + children: [ + const Text('How are you?'), + SelectionArea( + focusNode: FocusNode(), + child: const Text('Good, and you?'), + ), + const Text('Fine, thank you.'), + ], + ), + ), + ), + ), + ); + final RenderParagraph paragraph2 = tester.renderObject(find.descendant(of: find.text('Good, and you?'), matching: find.byType(RichText))); + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(paragraph2, 7)); // at the 'a' + addTearDown(gesture.removePointer); + await tester.pump(const Duration(milliseconds: 500)); + await gesture.up(); + final List boxes = paragraph2.getBoxesForSelection(paragraph2.selections[0]); + expect(boxes.length, 1); + // There is a selection now. + // We check the presence of the copy button to make sure the selection toolbar + // is showing. + expect(find.text('Copy'), findsOneWidget); + + // This is the position of the selection handle displayed at the end. + final Offset handlePos = paragraph2.localToGlobal(boxes[0].toRect().bottomRight); + await gesture.down(handlePos); + await gesture.moveTo(textOffsetToPosition(paragraph2, 11) + Offset(0, paragraph2.size.height / 2)); + await tester.pump(); + + await gesture.up(); + await tester.pump(); + + // After lifting the finger up, the selection toolbar should be showing again. + expect(find.text('Copy'), findsOneWidget); + }, + variant: TargetPlatformVariant.all(), + skip: kIsWeb, // [intended] + ); }