From 52c715fe43a530c95fb593d6bf1403e821e5ded8 Mon Sep 17 00:00:00 2001 From: Jaime Blasco Date: Thu, 22 Oct 2020 22:31:51 +0200 Subject: [PATCH] Add textSelectionControls to TextField etc. (#66785) Enables custom text selection menus by allowing selectionControls to be passed to TextField et. al. --- .../flutter/lib/src/cupertino/text_field.dart | 8 +- .../lib/src/material/selectable_text.dart | 12 ++- .../flutter/lib/src/material/text_field.dart | 11 ++- .../lib/src/material/text_form_field.dart | 2 + .../lib/src/widgets/editable_text.dart | 2 + .../test/cupertino/text_field_test.dart | 46 ++++++++++ .../test/material/text_field_test.dart | 78 +++++++++++++++++ .../test/material/text_form_field_test.dart | 17 ++++ .../test/widgets/selectable_text_test.dart | 87 +++++++++++++++++++ 9 files changed, 256 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 5d3e0dd1a1..aee197b982 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -270,6 +270,7 @@ class CupertinoTextField extends StatefulWidget { this.scrollPadding = const EdgeInsets.all(20.0), this.dragStartBehavior = DragStartBehavior.start, this.enableInteractiveSelection = true, + this.selectionControls, this.onTap, this.scrollController, this.scrollPhysics, @@ -559,6 +560,9 @@ class CupertinoTextField extends StatefulWidget { /// {@macro flutter.widgets.editableText.enableInteractiveSelection} final bool enableInteractiveSelection; + /// {@macro flutter.widgets.editableText.selectionControls} + final TextSelectionControls? selectionControls; + /// {@macro flutter.widgets.scrollable.dragStartBehavior} final DragStartBehavior dragStartBehavior; @@ -615,6 +619,7 @@ class CupertinoTextField extends StatefulWidget { properties.add(DiagnosticsProperty('cursorRadius', cursorRadius, defaultValue: null)); properties.add(createCupertinoColorProperty('cursorColor', cursorColor, defaultValue: null)); properties.add(FlagProperty('selectionEnabled', value: selectionEnabled, defaultValue: true, ifFalse: 'selection disabled')); + properties.add(DiagnosticsProperty('selectionControls', selectionControls, defaultValue: null)); properties.add(DiagnosticsProperty('scrollController', scrollController, defaultValue: null)); properties.add(DiagnosticsProperty('scrollPhysics', scrollPhysics, defaultValue: null)); properties.add(EnumProperty('textAlign', textAlign, defaultValue: TextAlign.start)); @@ -874,6 +879,7 @@ class _CupertinoTextFieldState extends State with Restoratio assert(debugCheckHasDirectionality(context)); final TextEditingController controller = _effectiveController; final List formatters = widget.inputFormatters ?? []; + final TextSelectionControls textSelectionControls = widget.selectionControls ?? cupertinoTextSelectionControls; final bool enabled = widget.enabled ?? true; final Offset cursorOffset = Offset(_iOSHorizontalCursorOffsetPixels / MediaQuery.of(context)!.devicePixelRatio, 0); if (widget.maxLength != null && widget.maxLengthEnforced) { @@ -957,7 +963,7 @@ class _CupertinoTextFieldState extends State with Restoratio expands: widget.expands, selectionColor: selectionColor, selectionControls: widget.selectionEnabled - ? cupertinoTextSelectionControls : null, + ? textSelectionControls : null, onChanged: widget.onChanged, onSelectionChanged: _handleSelectionChanged, onEditingComplete: widget.onEditingComplete, diff --git a/packages/flutter/lib/src/material/selectable_text.dart b/packages/flutter/lib/src/material/selectable_text.dart index 81fdacff1f..360e31fb07 100644 --- a/packages/flutter/lib/src/material/selectable_text.dart +++ b/packages/flutter/lib/src/material/selectable_text.dart @@ -193,6 +193,7 @@ class SelectableText extends StatefulWidget { this.cursorColor, this.dragStartBehavior = DragStartBehavior.start, this.enableInteractiveSelection = true, + this.selectionControls, this.onTap, this.scrollPhysics, this.textHeightBehavior, @@ -245,6 +246,7 @@ class SelectableText extends StatefulWidget { this.cursorColor, this.dragStartBehavior = DragStartBehavior.start, this.enableInteractiveSelection = true, + this.selectionControls, this.onTap, this.scrollPhysics, this.textHeightBehavior, @@ -353,6 +355,9 @@ class SelectableText extends StatefulWidget { /// {@macro flutter.widgets.editableText.enableInteractiveSelection} final bool enableInteractiveSelection; + /// {@macro flutter.widgets.editableText.selectionControls} + final TextSelectionControls? selectionControls; + /// {@macro flutter.widgets.scrollable.dragStartBehavior} final DragStartBehavior dragStartBehavior; @@ -416,6 +421,7 @@ class SelectableText extends StatefulWidget { properties.add(DiagnosticsProperty('cursorRadius', cursorRadius, defaultValue: null)); properties.add(DiagnosticsProperty('cursorColor', cursorColor, defaultValue: null)); properties.add(FlagProperty('selectionEnabled', value: selectionEnabled, defaultValue: true, ifFalse: 'selection disabled')); + properties.add(DiagnosticsProperty('selectionControls', selectionControls, defaultValue: null)); properties.add(DiagnosticsProperty('scrollPhysics', scrollPhysics, defaultValue: null)); properties.add(DiagnosticsProperty('textHeightBehavior', textHeightBehavior, defaultValue: null)); } @@ -565,7 +571,7 @@ class _SelectableTextState extends State with AutomaticKeepAlive final TextSelectionThemeData selectionTheme = TextSelectionTheme.of(context); final FocusNode focusNode = _effectiveFocusNode; - final TextSelectionControls textSelectionControls; + TextSelectionControls? textSelectionControls = widget.selectionControls; final bool paintCursorAboveText; final bool cursorOpacityAnimates; Offset? cursorOffset; @@ -578,7 +584,7 @@ class _SelectableTextState extends State with AutomaticKeepAlive case TargetPlatform.macOS: final CupertinoThemeData cupertinoTheme = CupertinoTheme.of(context); forcePressEnabled = true; - textSelectionControls = cupertinoTextSelectionControls; + textSelectionControls ??= cupertinoTextSelectionControls; paintCursorAboveText = true; cursorOpacityAnimates = true; cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor; @@ -592,7 +598,7 @@ class _SelectableTextState extends State with AutomaticKeepAlive case TargetPlatform.linux: case TargetPlatform.windows: forcePressEnabled = false; - textSelectionControls = materialTextSelectionControls; + textSelectionControls ??= materialTextSelectionControls; paintCursorAboveText = false; cursorOpacityAnimates = false; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 858b8abcfb..91f8dde9b8 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -372,6 +372,7 @@ class TextField extends StatefulWidget { this.scrollPadding = const EdgeInsets.all(20.0), this.dragStartBehavior = DragStartBehavior.start, this.enableInteractiveSelection = true, + this.selectionControls, this.onTap, this.mouseCursor, this.buildCounter, @@ -674,6 +675,9 @@ class TextField extends StatefulWidget { /// {@macro flutter.widgets.editableText.enableInteractiveSelection} final bool enableInteractiveSelection; + /// {@macro flutter.widgets.editableText.selectionControls} + final TextSelectionControls? selectionControls; + /// {@macro flutter.widgets.scrollable.dragStartBehavior} final DragStartBehavior dragStartBehavior; @@ -818,6 +822,7 @@ class TextField extends StatefulWidget { properties.add(DiagnosticsProperty('keyboardAppearance', keyboardAppearance, defaultValue: null)); properties.add(DiagnosticsProperty('scrollPadding', scrollPadding, defaultValue: const EdgeInsets.all(20.0))); properties.add(FlagProperty('selectionEnabled', value: selectionEnabled, defaultValue: true, ifFalse: 'selection disabled')); + properties.add(DiagnosticsProperty('selectionControls', selectionControls, defaultValue: null)); properties.add(DiagnosticsProperty('scrollController', scrollController, defaultValue: null)); properties.add(DiagnosticsProperty('scrollPhysics', scrollPhysics, defaultValue: null)); } @@ -1092,7 +1097,7 @@ class _TextFieldState extends State with RestorationMixin implements if (widget.maxLength != null && widget.maxLengthEnforced) formatters.add(LengthLimitingTextInputFormatter(widget.maxLength)); - final TextSelectionControls textSelectionControls; + TextSelectionControls? textSelectionControls = widget.selectionControls; final bool paintCursorAboveText; final bool cursorOpacityAnimates; Offset? cursorOffset; @@ -1106,7 +1111,7 @@ class _TextFieldState extends State with RestorationMixin implements case TargetPlatform.macOS: final CupertinoThemeData cupertinoTheme = CupertinoTheme.of(context); forcePressEnabled = true; - textSelectionControls = cupertinoTextSelectionControls; + textSelectionControls ??= cupertinoTextSelectionControls; paintCursorAboveText = true; cursorOpacityAnimates = true; cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor; @@ -1121,7 +1126,7 @@ class _TextFieldState extends State with RestorationMixin implements case TargetPlatform.linux: case TargetPlatform.windows: forcePressEnabled = false; - textSelectionControls = materialTextSelectionControls; + textSelectionControls ??= materialTextSelectionControls; paintCursorAboveText = false; cursorOpacityAnimates = false; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index af6dacfed3..f5b9fdce7b 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -182,6 +182,7 @@ class TextFormField extends FormField { Brightness? keyboardAppearance, EdgeInsets scrollPadding = const EdgeInsets.all(20.0), bool enableInteractiveSelection = true, + TextSelectionControls? selectionControls, InputCounterWidgetBuilder? buildCounter, ScrollPhysics? scrollPhysics, Iterable? autofillHints, @@ -276,6 +277,7 @@ class TextFormField extends FormField { scrollPhysics: scrollPhysics, keyboardAppearance: keyboardAppearance, enableInteractiveSelection: enableInteractiveSelection, + selectionControls: selectionControls, buildCounter: buildCounter, autofillHints: autofillHints, ); diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 424d99b996..fda9b9df6b 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -875,6 +875,7 @@ class EditableText extends StatefulWidget { /// value is set to the ambient [ThemeData.textSelectionColor]. final Color? selectionColor; + /// {@template flutter.widgets.editableText.selectionControls} /// Optional delegate for building the text selection handles and toolbar. /// /// The [EditableText] widget used on its own will not trigger the display @@ -889,6 +890,7 @@ class EditableText extends StatefulWidget { /// * [TextField], a Material Design themed wrapper of [EditableText], which /// shows the selection toolbar upon appropriate user events based on the /// user's platform set in [ThemeData.platform]. + /// {@endtemplate} final TextSelectionControls? selectionControls; /// {@template flutter.widgets.editableText.keyboardType} diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index a5c3a5d845..b1ade38b9c 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -29,6 +29,36 @@ class MockClipboard { } } +class MockTextSelectionControls extends TextSelectionControls { + @override + Widget buildHandle(BuildContext context, TextSelectionHandleType type, + double textLineHeight) { + throw UnimplementedError(); + } + + @override + Widget buildToolbar( + BuildContext context, + Rect globalEditableRegion, + double textLineHeight, + Offset position, + List endpoints, + TextSelectionDelegate delegate, + ClipboardStatusNotifier clipboardStatus) { + throw UnimplementedError(); + } + + @override + Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) { + throw UnimplementedError(); + } + + @override + Size getHandleSize(double textLineHeight) { + throw UnimplementedError(); + } +} + class PathBoundsMatcher extends Matcher { const PathBoundsMatcher({ this.rectMatcher, @@ -4123,4 +4153,20 @@ void main() { matchesGoldenFile('text_field_golden.TextSelectionStyle.2.png'), ); }); + + testWidgets('textSelectionControls is passed to EditableText', (WidgetTester tester) async { + final MockTextSelectionControls selectionControl = MockTextSelectionControls(); + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: CupertinoTextField( + selectionControls: selectionControl + ), + ), + ), + ); + + final EditableText widget = tester.widget(find.byType(EditableText)); + expect(widget.selectionControls, equals(selectionControl)); + }); } diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 4f4ab2dbe0..002c33d899 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -6295,6 +6295,84 @@ void main() { expect(find.byType(TextButton), findsNWidgets(4)); }, variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows })); + testWidgets('Custom toolbar test - Android text selection controls', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController( + text: 'Atwater Peel Sherbrooke Bonaventure', + ); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: TextField( + controller: controller, + selectionControls: materialTextSelectionControls + ), + ), + ), + ), + ); + + final Offset textfieldStart = tester.getTopLeft(find.byType(TextField)); + + await tester.tapAt(textfieldStart + const Offset(150.0, 9.0)); + await tester.pump(const Duration(milliseconds: 50)); + + await tester.tapAt(textfieldStart + const Offset(150.0, 9.0)); + await tester.pumpAndSettle(); + + // Selected text shows 4 toolbar buttons: cut, copy, paste, select all + expect(find.byType(TextButton), findsNWidgets(4)); + }, variant: TargetPlatformVariant.all()); + + testWidgets( + 'Custom toolbar test - Cupertino text selection controls', + (WidgetTester tester) async { + final TextEditingController controller = TextEditingController( + text: 'Atwater Peel Sherbrooke Bonaventure', + ); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: TextField( + controller: controller, + selectionControls: cupertinoTextSelectionControls, + ), + ), + ), + ), + ); + + final Offset textfieldStart = tester.getTopLeft(find.byType(TextField)); + + await tester.tapAt(textfieldStart + const Offset(150.0, 9.0)); + await tester.pump(const Duration(milliseconds: 50)); + + await tester.tapAt(textfieldStart + const Offset(150.0, 9.0)); + await tester.pumpAndSettle(); + + // Selected text shows 3 toolbar buttons: cut, copy, paste + expect(find.byType(CupertinoButton), findsNWidgets(3)); + }, variant: TargetPlatformVariant.all()); + + testWidgets('selectionControls is passed to EditableText', + (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Scaffold( + body: TextField( + selectionControls: materialTextSelectionControls, + ), + ), + ), + ), + ); + + final EditableText widget = tester.widget(find.byType(EditableText)); + expect(widget.selectionControls, equals(materialTextSelectionControls)); + }); + testWidgets( 'double tap on top of cursor also selects word', (WidgetTester tester) async { diff --git a/packages/flutter/test/material/text_form_field_test.dart b/packages/flutter/test/material/text_form_field_test.dart index 7ac31bf483..02a788854a 100644 --- a/packages/flutter/test/material/text_form_field_test.dart +++ b/packages/flutter/test/material/text_form_field_test.dart @@ -514,4 +514,21 @@ void main() { ); }, throwsAssertionError); }); + + testWidgets('textSelectionControls is passed to super', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Scaffold( + body: TextFormField( + selectionControls: materialTextSelectionControls, + ), + ), + ), + ), + ); + + final TextField widget = tester.widget(find.byType(TextField)); + expect(widget.selectionControls, equals(materialTextSelectionControls)); + }); } diff --git a/packages/flutter/test/widgets/selectable_text_test.dart b/packages/flutter/test/widgets/selectable_text_test.dart index a5af985996..f1b02e4f74 100644 --- a/packages/flutter/test/widgets/selectable_text_test.dart +++ b/packages/flutter/test/widgets/selectable_text_test.dart @@ -2771,6 +2771,93 @@ void main() { }, ); + testWidgets( + 'long press selects word and shows custom toolbar (Android)', + (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: SelectableText('Atwater Peel Sherbrooke Bonaventure', + selectionControls: cupertinoTextSelectionControls, + ), + ), + ), + ), + ); + + final Offset selectableTextStart = tester.getTopLeft(find.byType(SelectableText)); + + await tester.longPressAt(selectableTextStart + const Offset(50.0, 5.0)); + await tester.pump(); + + final EditableText editableTextWidget = tester.widget(find.byType(EditableText).first); + final TextEditingController controller = editableTextWidget.controller; + + // The longpressed word is selected. + expect( + controller.selection, + const TextSelection( + baseOffset: 0, + extentOffset: 7, + ), + ); + + // Toolbar shows one button. + expect(find.byType(CupertinoButton), findsNWidgets(1)); + }, variant: TargetPlatformVariant.all()); + + testWidgets( + 'long press selects word and shows custom toolbar (iOS)', + (WidgetTester tester) async { + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Center( + child: SelectableText('Atwater Peel Sherbrooke Bonaventure', + selectionControls: materialTextSelectionControls, + ), + ), + ), + ), + ); + + final Offset selectableTextStart = tester.getTopLeft(find.byType(SelectableText)); + + await tester.longPressAt(selectableTextStart + const Offset(50.0, 5.0)); + await tester.pump(); + + final EditableText editableTextWidget = tester.widget(find.byType(EditableText).first); + final TextEditingController controller = editableTextWidget.controller; + + expect( + controller.selection, + const TextSelection(baseOffset: 0, extentOffset: 7), + ); + + // Collapsed toolbar shows 2 buttons: copy, select all + expect(find.byType(TextButton), findsNWidgets(2)); + }, variant: TargetPlatformVariant.all()); + + testWidgets('textSelectionControls is passed to EditableText', + (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Scaffold( + body: SelectableText('Atwater Peel Sherbrooke Bonaventure', + selectionControls: materialTextSelectionControls, + ), + ), + ), + ), + ); + + final EditableText widget = tester.widget(find.byType(EditableText)); + expect(widget.selectionControls, equals(materialTextSelectionControls)); + }); + testWidgets( 'long press tap cannot initiate a double tap', (WidgetTester tester) async {