diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index a4e81d0d31..b6a61ea08e 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -123,6 +123,7 @@ class _CupertinoTextFieldSelectionGestureDetectorBuilder extends TextSelectionGe @override void onDragSelectionEnd(TapDragEndDetails details) { _state._requestKeyboard(); + super.onDragSelectionEnd(details); } } diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index 3b88b5ac99..dd23425ca8 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -1858,6 +1858,33 @@ class TextSelectionGestureDetectorBuilder { @protected final TextSelectionGestureDetectorBuilderDelegate delegate; + // Shows the magnifier on supported platforms at the given offset, currently + // only Android and iOS. + void _showMagnifierIfSupportedByPlatform(Offset positionToShow) { + switch (defaultTargetPlatform) { + case TargetPlatform.android: + case TargetPlatform.iOS: + editableText.showMagnifier(positionToShow); + case TargetPlatform.fuchsia: + case TargetPlatform.linux: + case TargetPlatform.macOS: + case TargetPlatform.windows: + } + } + + // Hides the magnifier on supported platforms, currently only Android and iOS. + void _hideMagnifierIfSupportedByPlatform() { + switch (defaultTargetPlatform) { + case TargetPlatform.android: + case TargetPlatform.iOS: + editableText.hideMagnifier(); + case TargetPlatform.fuchsia: + case TargetPlatform.linux: + case TargetPlatform.macOS: + case TargetPlatform.windows: + } + } + /// Returns true if lastSecondaryTapDownPosition was on selection. bool get _lastSecondaryTapWasOnSelection { assert(renderEditable.lastSecondaryTapDownPosition != null); @@ -2291,16 +2318,7 @@ class TextSelectionGestureDetectorBuilder { renderEditable.selectWord(cause: SelectionChangedCause.longPress); } - switch (defaultTargetPlatform) { - case TargetPlatform.android: - case TargetPlatform.iOS: - editableText.showMagnifier(details.globalPosition); - case TargetPlatform.fuchsia: - case TargetPlatform.linux: - case TargetPlatform.macOS: - case TargetPlatform.windows: - break; - } + _showMagnifierIfSupportedByPlatform(details.globalPosition); _dragStartViewportOffset = renderEditable.offset.pixels; _dragStartScrollOffset = _scrollPosition; @@ -2354,16 +2372,7 @@ class TextSelectionGestureDetectorBuilder { ); } - switch (defaultTargetPlatform) { - case TargetPlatform.android: - case TargetPlatform.iOS: - editableText.showMagnifier(details.globalPosition); - case TargetPlatform.fuchsia: - case TargetPlatform.linux: - case TargetPlatform.macOS: - case TargetPlatform.windows: - break; - } + _showMagnifierIfSupportedByPlatform(details.globalPosition); } } @@ -2377,16 +2386,7 @@ class TextSelectionGestureDetectorBuilder { /// callback. @protected void onSingleLongTapEnd(LongPressEndDetails details) { - switch (defaultTargetPlatform) { - case TargetPlatform.android: - case TargetPlatform.iOS: - editableText.hideMagnifier(); - case TargetPlatform.fuchsia: - case TargetPlatform.linux: - case TargetPlatform.macOS: - case TargetPlatform.windows: - break; - } + _hideMagnifierIfSupportedByPlatform(); if (shouldShowSelectionToolbar) { editableText.showToolbar(); } @@ -2599,12 +2599,12 @@ class TextSelectionGestureDetectorBuilder { switch (details.kind) { case PointerDeviceKind.mouse: case PointerDeviceKind.trackpad: - case PointerDeviceKind.stylus: - case PointerDeviceKind.invertedStylus: renderEditable.selectPositionAt( from: details.globalPosition, cause: SelectionChangedCause.drag, ); + case PointerDeviceKind.stylus: + case PointerDeviceKind.invertedStylus: case PointerDeviceKind.touch: case PointerDeviceKind.unknown: // For Android, Fucshia, and iOS platforms, a touch drag @@ -2614,6 +2614,7 @@ class TextSelectionGestureDetectorBuilder { from: details.globalPosition, cause: SelectionChangedCause.drag, ); + _showMagnifierIfSupportedByPlatform(details.globalPosition); } case null: break; @@ -2659,11 +2660,23 @@ class TextSelectionGestureDetectorBuilder { // Select word by word. if (_TextSelectionGestureDetectorState._getEffectiveConsecutiveTapCount(details.consecutiveTapCount) == 2) { - return renderEditable.selectWordsInRange( + renderEditable.selectWordsInRange( from: dragStartGlobalPosition - editableOffset - scrollableOffset, to: details.globalPosition, cause: SelectionChangedCause.drag, ); + + switch (details.kind) { + case PointerDeviceKind.stylus: + case PointerDeviceKind.invertedStylus: + case PointerDeviceKind.touch: + case PointerDeviceKind.unknown: + return _showMagnifierIfSupportedByPlatform(details.globalPosition); + case PointerDeviceKind.mouse: + case PointerDeviceKind.trackpad: + case null: + return; + } } // Select paragraph-by-paragraph. @@ -2732,10 +2745,11 @@ class TextSelectionGestureDetectorBuilder { && _dragStartSelection!.isCollapsed && _dragBeganOnPreviousSelection! ) { - return renderEditable.selectPositionAt( + renderEditable.selectPositionAt( from: details.globalPosition, cause: SelectionChangedCause.drag, ); + return _showMagnifierIfSupportedByPlatform(details.globalPosition); } case null: break; @@ -2759,10 +2773,11 @@ class TextSelectionGestureDetectorBuilder { case PointerDeviceKind.touch: case PointerDeviceKind.unknown: if (renderEditable.hasFocus) { - return renderEditable.selectPositionAt( + renderEditable.selectPositionAt( from: details.globalPosition, cause: SelectionChangedCause.drag, ); + return _showMagnifierIfSupportedByPlatform(details.globalPosition); } case null: break; @@ -2838,6 +2853,8 @@ class TextSelectionGestureDetectorBuilder { if (isShiftPressed) { _dragStartSelection = null; } + + _hideMagnifierIfSupportedByPlatform(); } /// Returns a [TextSelectionGestureDetector] configured with the handlers diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index ac3d18d5d3..a5c726fdf1 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -8907,6 +8907,105 @@ void main() { expect(find.byKey(fakeMagnifier.key!), findsNothing); }, variant: TargetPlatformVariant.only(TargetPlatform.iOS)); + testWidgets('Can drag to show, unshow, and update magnifier', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(); + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: CupertinoTextField( + dragStartBehavior: DragStartBehavior.down, + controller: controller, + magnifierConfiguration: TextMagnifierConfiguration( + magnifierBuilder: ( + _, + MagnifierController controller, + ValueNotifier localMagnifierInfo + ) { + magnifierInfo = localMagnifierInfo; + return fakeMagnifier; + }, + ), + ), + ), + ), + ); + + const String testValue = 'abc def ghi'; + await tester.enterText(find.byType(CupertinoTextField), testValue); + await tester.pumpAndSettle(); + + // Tap at '|a' to move the selection to position 0. + await tester.tapAt(textOffsetToPosition(tester, 0)); + await tester.pumpAndSettle(kDoubleTapTimeout); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 0); + expect(find.byKey(fakeMagnifier.key!), findsNothing); + + // Start a drag gesture to move the selection to the dragged position, showing + // the magnifier. + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(tester, 0)); + await tester.pump(); + + await gesture.moveTo(textOffsetToPosition(tester, 5)); + await tester.pump(); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 5); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + + Offset firstDragGesturePosition = magnifierInfo.value.globalGesturePosition; + + await gesture.moveTo(textOffsetToPosition(tester, 10)); + await tester.pump(); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 10); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + // Expect the position the magnifier gets to have moved. + expect(firstDragGesturePosition, isNot(magnifierInfo.value.globalGesturePosition)); + + // The magnifier should hide when the drag ends. + await gesture.up(); + await tester.pump(); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 10); + expect(find.byKey(fakeMagnifier.key!), findsNothing); + + // Start a double-tap select the word at the tapped position. + await gesture.down(textOffsetToPosition(tester, 1)); + await tester.pump(); + await gesture.up(); + await tester.pump(); + + await gesture.down(textOffsetToPosition(tester, 1)); + await tester.pumpAndSettle(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 3); + + // Start a drag gesture to extend the selection word-by-word, showing the + // magnifier. + await gesture.moveTo(textOffsetToPosition(tester, 5)); + await tester.pump(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 7); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + + firstDragGesturePosition = magnifierInfo.value.globalGesturePosition; + + await gesture.moveTo(textOffsetToPosition(tester, 10)); + await tester.pump(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 11); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + // Expect the position the magnifier gets to have moved. + expect(firstDragGesturePosition, isNot(magnifierInfo.value.globalGesturePosition)); + + // The magnifier should hide when the drag ends. + await gesture.up(); + await tester.pump(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 11); + expect(find.byKey(fakeMagnifier.key!), findsNothing); + }, variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.iOS })); + testWidgets('Can long press to show, unshow, and update magnifier on non-Apple platforms', (WidgetTester tester) async { final TextEditingController controller = TextEditingController(); final bool isTargetPlatformAndroid = defaultTargetPlatform == TargetPlatform.android; diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index ba015cabce..35b6872ab9 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -15770,6 +15770,107 @@ void main() { expect(find.byKey(fakeMagnifier.key!), findsNothing); }); + testWidgets('Can drag to show, unshow, and update magnifier', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: TextField( + dragStartBehavior: DragStartBehavior.down, + controller: controller, + magnifierConfiguration: TextMagnifierConfiguration( + magnifierBuilder: ( + _, + MagnifierController controller, + ValueNotifier localMagnifierInfo + ) { + magnifierInfo = localMagnifierInfo; + return fakeMagnifier; + }, + ), + ), + ), + ), + ), + ); + + const String testValue = 'abc def ghi'; + await tester.enterText(find.byType(TextField), testValue); + await skipPastScrollingAnimation(tester); + + // Tap at '|a' to move the selection to position 0. + await tester.tapAt(textOffsetToPosition(tester, 0)); + await tester.pumpAndSettle(kDoubleTapTimeout); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 0); + expect(find.byKey(fakeMagnifier.key!), findsNothing); + + // Start a drag gesture to move the selection to the dragged position, showing + // the magnifier. + final TestGesture gesture = await tester.startGesture(textOffsetToPosition(tester, 0)); + await tester.pump(); + + await gesture.moveTo(textOffsetToPosition(tester, 5)); + await tester.pump(); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 5); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + + Offset firstDragGesturePosition = magnifierInfo.value.globalGesturePosition; + + await gesture.moveTo(textOffsetToPosition(tester, 10)); + await tester.pump(); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 10); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + // Expect the position the magnifier gets to have moved. + expect(firstDragGesturePosition, isNot(magnifierInfo.value.globalGesturePosition)); + + // The magnifier should hide when the drag ends. + await gesture.up(); + await tester.pump(); + expect(controller.selection.isCollapsed, true); + expect(controller.selection.baseOffset, 10); + expect(find.byKey(fakeMagnifier.key!), findsNothing); + + // Start a double-tap select the word at the tapped position. + await gesture.down(textOffsetToPosition(tester, 1)); + await tester.pump(); + await gesture.up(); + await tester.pump(); + + await gesture.down(textOffsetToPosition(tester, 1)); + await tester.pumpAndSettle(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 3); + + // Start a drag gesture to extend the selection word-by-word, showing the + // magnifier. + await gesture.moveTo(textOffsetToPosition(tester, 5)); + await tester.pump(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 7); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + + firstDragGesturePosition = magnifierInfo.value.globalGesturePosition; + + await gesture.moveTo(textOffsetToPosition(tester, 10)); + await tester.pump(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 11); + expect(find.byKey(fakeMagnifier.key!), findsOneWidget); + // Expect the position the magnifier gets to have moved. + expect(firstDragGesturePosition, isNot(magnifierInfo.value.globalGesturePosition)); + + // The magnifier should hide when the drag ends. + await gesture.up(); + await tester.pump(); + expect(controller.selection.baseOffset, 0); + expect(controller.selection.extentOffset, 11); + expect(find.byKey(fakeMagnifier.key!), findsNothing); + }, variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.iOS })); + testWidgets('Can long press to show, unshow, and update magnifier', (WidgetTester tester) async { final TextEditingController controller = TextEditingController(); final bool isTargetPlatformAndroid = defaultTargetPlatform == TargetPlatform.android;