diff --git a/packages/flutter/lib/src/material/text_selection.dart b/packages/flutter/lib/src/material/text_selection.dart index e19b971735..c9def00dcd 100644 --- a/packages/flutter/lib/src/material/text_selection.dart +++ b/packages/flutter/lib/src/material/text_selection.dart @@ -234,8 +234,9 @@ class _MaterialTextSelectionControls extends TextSelectionControls { // Android allows SelectAll when selection is not collapsed, unless // everything has already been selected. final TextEditingValue value = delegate.textEditingValue; - return value.text.isNotEmpty && - !(value.selection.start == 0 && value.selection.end == value.text.length); + return delegate.selectAllEnabled && + value.text.isNotEmpty && + !(value.selection.start == 0 && value.selection.end == value.text.length); } } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 24ade7ecd2..85f37afbd4 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -661,6 +661,72 @@ void main() { expect(find.text('CUT'), findsNothing); }); + testWidgets('can dynamically disable select all option in toolbar - cupertino', (WidgetTester tester) async { + // Regression test: https://github.com/flutter/flutter/issues/40711 + await tester.pumpWidget( + MaterialApp( + home: EditableText( + backgroundCursorColor: Colors.grey, + controller: TextEditingController(text: 'blah blah'), + focusNode: focusNode, + toolbarOptions: const ToolbarOptions( + selectAll: false, + ), + style: textStyle, + cursorColor: cursorColor, + selectionControls: cupertinoTextSelectionControls, + ), + ), + ); + + final EditableTextState state = + tester.state(find.byType(EditableText)); + await tester.tap(find.byType(EditableText)); + await tester.pump(); + expect(state.showToolbar(), true); + await tester.pump(); + expect(find.text('Select All'), findsNothing); + expect(find.text('Copy'), findsNothing); + expect(find.text('Paste'), findsNothing); + expect(find.text('Cut'), findsNothing); + }); + + testWidgets('can dynamically disable select all option in toolbar - material', (WidgetTester tester) async { + // Regression test: https://github.com/flutter/flutter/issues/40711 + await tester.pumpWidget( + MaterialApp( + home: EditableText( + backgroundCursorColor: Colors.grey, + controller: TextEditingController(text: 'blah blah'), + focusNode: focusNode, + toolbarOptions: const ToolbarOptions( + copy: true, + selectAll: false, + ), + style: textStyle, + cursorColor: cursorColor, + selectionControls: materialTextSelectionControls, + ), + ), + ); + + final EditableTextState state = + tester.state(find.byType(EditableText)); + + // Select something. Doesn't really matter what. + state.renderEditable.selectWordsInRange( + from: const Offset(0, 0), + cause: SelectionChangedCause.tap, + ); + await tester.pump(); + expect(state.showToolbar(), true); + await tester.pump(); + expect(find.text('SELECT ALL'), findsNothing); + expect(find.text('COPY'), findsOneWidget); + expect(find.text('PASTE'), findsNothing); + expect(find.text('CUT'), findsNothing); + }); + testWidgets('cut and paste are disabled in read only mode even if explicit set', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp(