Disable spell check for obscured text (#186329) Fixes https://github.com/flutter/flutter/issues/185843 When `EditableText` has `obscureText: true`, spell check should not run or keep previous spell-check results. This makes the inferred spell-check configuration disabled for obscured text, recomputes that configuration when `obscureText` changes, refreshes spell check when obscured text becomes visible again, and ignores async spell-check responses that return after spell check has been disabled. Tests: - `../../bin/flutter test test/widgets/editable_text_test.dart --plain-name='Spell check'` - `../../bin/flutter analyze --no-pub lib/src/widgets/editable_text.dart test/widgets/editable_text_test.dart` - `git diff --check`
diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 3a1ea9c..949c758 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart
@@ -2051,6 +2051,11 @@ /// Specifies the [SpellCheckService] used to spell check text input and the /// [TextStyle] used to style text with misspelled words. /// + /// Spell check is disabled for password input, including when [obscureText] + /// is true, [keyboardType] is [TextInputType.visiblePassword], or + /// [autofillHints] contains [AutofillHints.password] or + /// [AutofillHints.newPassword]. + /// /// If the [SpellCheckService] is left null, spell check is disabled by /// default unless the [DefaultSpellCheckService] is supported, in which case /// it is used. It is currently supported only on Android and iOS. @@ -3050,18 +3055,27 @@ /// If spell check is enabled, this will try to infer a value for /// the [SpellCheckService] if left unspecified. static SpellCheckConfiguration _inferSpellCheckConfiguration( - SpellCheckConfiguration? configuration, - ) { + SpellCheckConfiguration? configuration, { + required bool obscureText, + required TextInputType keyboardType, + required Iterable<String>? autofillHints, + }) { final SpellCheckService? spellCheckService = configuration?.spellCheckService; final bool spellCheckAutomaticallyDisabled = - configuration == null || configuration == const SpellCheckConfiguration.disabled(); + _isPasswordInput( + obscureText: obscureText, + keyboardType: keyboardType, + autofillHints: autofillHints, + ) || + configuration == null || + configuration == const SpellCheckConfiguration.disabled(); final bool spellCheckServiceIsConfigured = spellCheckService != null || WidgetsBinding.instance.platformDispatcher.nativeSpellCheckServiceDefined; if (spellCheckAutomaticallyDisabled || !spellCheckServiceIsConfigured) { // Only enable spell check if a non-disabled configuration is provided - // and if that configuration does not specify a spell check service, - // a native spell checker must be supported. + // for non-password input and, if that configuration does not specify a + // spell check service, a native spell checker must be supported. assert(() { if (!spellCheckAutomaticallyDisabled && !spellCheckServiceIsConfigured) { FlutterError.reportError( @@ -3088,6 +3102,19 @@ ); } + static bool _isPasswordInput({ + required bool obscureText, + required TextInputType keyboardType, + required Iterable<String>? autofillHints, + }) { + return obscureText || + keyboardType == TextInputType.visiblePassword || + (autofillHints?.any( + (String hint) => hint == AutofillHints.password || hint == AutofillHints.newPassword, + ) ?? + false); + } + /// Returns the [ContextMenuButtonItem]s for the given [ToolbarOptions]. @Deprecated( 'Use `contextMenuBuilder` instead of `toolbarOptions`. ' @@ -3284,7 +3311,12 @@ widget.controller.addListener(_didChangeTextEditingValue); widget.focusNode.addListener(_handleFocusChanged); _cursorVisibilityNotifier.value = widget.showCursor; - _spellCheckConfiguration = _inferSpellCheckConfiguration(widget.spellCheckConfiguration); + _spellCheckConfiguration = _inferSpellCheckConfiguration( + widget.spellCheckConfiguration, + obscureText: widget.obscureText, + keyboardType: widget.keyboardType, + autofillHints: widget.autofillHints, + ); _appLifecycleListener = AppLifecycleListener(onResume: _onResume); _initProcessTextActions(); } @@ -3493,6 +3525,28 @@ } } + if (oldWidget.spellCheckConfiguration != widget.spellCheckConfiguration || + oldWidget.obscureText != widget.obscureText || + oldWidget.keyboardType != widget.keyboardType || + !listEquals<String>( + oldWidget.autofillHints?.toList(growable: false), + widget.autofillHints?.toList(growable: false), + )) { + _spellCheckConfiguration = _inferSpellCheckConfiguration( + widget.spellCheckConfiguration, + obscureText: widget.obscureText, + keyboardType: widget.keyboardType, + autofillHints: widget.autofillHints, + ); + if (spellCheckEnabled) { + if (textEditingValue.text.isNotEmpty) { + _performSpellCheck(textEditingValue.text); + } + } else { + spellCheckResults = null; + } + } + if (widget.style != oldWidget.style) { // The _textInputConnection will pick up the new style when it attaches in // _openInputConnection. @@ -4614,9 +4668,10 @@ final List<SuggestionSpan>? suggestions = await _spellCheckConfiguration.spellCheckService! .fetchSpellCheckSuggestions(localeForSpellChecking!, text); - if (suggestions == null || !mounted) { + if (suggestions == null || !mounted || !spellCheckEnabled) { // The request to fetch spell check suggestions was canceled due to ongoing request, - // or the widget was unmounted. + // the widget was unmounted, or spell check was disabled before the + // request completed. return; }
diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index aac108e..faad619 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart
@@ -15975,6 +15975,204 @@ ); }); + testWidgets('Spell check disabled when obscureText is true', (WidgetTester tester) async { + final fakeSpellCheckService = FakeSpellCheckService(); + controller.text = 'A'; + + await tester.pumpWidget( + TestWidgetsApp( + home: EditableText( + controller: controller, + focusNode: focusNode, + obscureText: true, + style: const TextStyle(), + cursorColor: const Color(0xFF0000FF), + backgroundCursorColor: const Color(0xFF808080), + cursorOpacityAnimates: true, + autofillHints: null, + spellCheckConfiguration: SpellCheckConfiguration( + spellCheckService: fakeSpellCheckService, + misspelledTextStyle: const TextStyle(decoration: TextDecoration.underline), + ), + ), + ), + ); + + final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isFalse); + expect(state.spellCheckConfiguration, equals(const SpellCheckConfiguration.disabled())); + }); + + testWidgets('Spell check disabled for visible password input type', ( + WidgetTester tester, + ) async { + final fakeSpellCheckService = FakeSpellCheckService(); + controller.text = 'A'; + + await tester.pumpWidget( + TestWidgetsApp( + home: EditableText( + controller: controller, + focusNode: focusNode, + keyboardType: TextInputType.visiblePassword, + style: const TextStyle(), + cursorColor: const Color(0xFF0000FF), + backgroundCursorColor: const Color(0xFF808080), + cursorOpacityAnimates: true, + autofillHints: null, + spellCheckConfiguration: SpellCheckConfiguration( + spellCheckService: fakeSpellCheckService, + misspelledTextStyle: const TextStyle(decoration: TextDecoration.underline), + ), + ), + ), + ); + + final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isFalse); + expect(state.spellCheckConfiguration, equals(const SpellCheckConfiguration.disabled())); + }); + + testWidgets('Spell check disabled for password autofill hints', (WidgetTester tester) async { + final fakeSpellCheckService = FakeSpellCheckService(); + controller.text = 'A'; + + await tester.pumpWidget( + TestWidgetsApp( + home: EditableText( + controller: controller, + focusNode: focusNode, + style: const TextStyle(), + cursorColor: const Color(0xFF0000FF), + backgroundCursorColor: const Color(0xFF808080), + cursorOpacityAnimates: true, + autofillHints: const <String>[AutofillHints.password], + spellCheckConfiguration: SpellCheckConfiguration( + spellCheckService: fakeSpellCheckService, + misspelledTextStyle: const TextStyle(decoration: TextDecoration.underline), + ), + ), + ), + ); + + final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isFalse); + expect(state.spellCheckConfiguration, equals(const SpellCheckConfiguration.disabled())); + }); + + testWidgets('Spell check updates when non-password obscureText changes', ( + WidgetTester tester, + ) async { + const suggestionSpans = <SuggestionSpan>[ + SuggestionSpan(TextRange(start: 0, end: 1), <String>['a']), + ]; + final fakeSpellCheckService = FakeSpellCheckService( + suggestionSpansByText: const <String, List<SuggestionSpan>?>{'A': suggestionSpans}, + ); + controller.text = 'A'; + var obscureText = false; + late StateSetter setState; + + await tester.pumpWidget( + TestWidgetsApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter localSetState) { + setState = localSetState; + return EditableText( + controller: controller, + focusNode: focusNode, + obscureText: obscureText, + style: const TextStyle(), + cursorColor: const Color(0xFF0000FF), + backgroundCursorColor: const Color(0xFF808080), + cursorOpacityAnimates: true, + autofillHints: null, + spellCheckConfiguration: SpellCheckConfiguration( + spellCheckService: fakeSpellCheckService, + misspelledTextStyle: const TextStyle(decoration: TextDecoration.underline), + ), + ); + }, + ), + ), + ); + + void setObscureText(bool value) { + setState(() { + obscureText = value; + }); + } + + EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isTrue); + state.spellCheckResults = const SpellCheckResults('A', suggestionSpans); + + setObscureText(true); + await tester.pump(); + + state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isFalse); + expect(state.spellCheckConfiguration, equals(const SpellCheckConfiguration.disabled())); + expect(state.spellCheckResults, isNull); + expect(fakeSpellCheckService.fetchSpellCheckSuggestionsCallCount, 0); + + setObscureText(false); + await tester.pump(); + + state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isTrue); + expect(fakeSpellCheckService.fetchSpellCheckSuggestionsCallCount, 1); + expect(fakeSpellCheckService.lastSpellCheckText, 'A'); + expect(state.spellCheckResults, const SpellCheckResults('A', suggestionSpans)); + }); + + testWidgets('Spell check stays disabled for visible password when obscureText changes', ( + WidgetTester tester, + ) async { + final fakeSpellCheckService = FakeSpellCheckService(); + controller.text = 'A'; + var obscureText = true; + late StateSetter setState; + + await tester.pumpWidget( + TestWidgetsApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter localSetState) { + setState = localSetState; + return EditableText( + controller: controller, + focusNode: focusNode, + keyboardType: TextInputType.visiblePassword, + obscureText: obscureText, + style: const TextStyle(), + cursorColor: const Color(0xFF0000FF), + backgroundCursorColor: const Color(0xFF808080), + cursorOpacityAnimates: true, + autofillHints: null, + spellCheckConfiguration: SpellCheckConfiguration( + spellCheckService: fakeSpellCheckService, + misspelledTextStyle: const TextStyle(decoration: TextDecoration.underline), + ), + ); + }, + ), + ), + ); + + EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isFalse); + + setState(() { + obscureText = false; + }); + await tester.pump(); + + state = tester.state<EditableTextState>(find.byType(EditableText)); + expect(state.spellCheckEnabled, isFalse); + expect(state.spellCheckConfiguration, equals(const SpellCheckConfiguration.disabled())); + expect(fakeSpellCheckService.fetchSpellCheckSuggestionsCallCount, 0); + }); + testWidgets( 'Spell check disabled when spell check configuration specified but no default spell check service available', (WidgetTester tester) async { @@ -19019,7 +19217,22 @@ bool get attached => hasListeners; } -class FakeSpellCheckService extends DefaultSpellCheckService {} +class FakeSpellCheckService extends DefaultSpellCheckService { + FakeSpellCheckService({this.suggestionSpansByText = const <String, List<SuggestionSpan>?>{}}); + + final Map<String, List<SuggestionSpan>?> suggestionSpansByText; + int fetchSpellCheckSuggestionsCallCount = 0; + String? lastSpellCheckText; + + @override + Future<List<SuggestionSpan>?> fetchSpellCheckSuggestions(Locale locale, String text) async { + fetchSpellCheckSuggestionsCallCount += 1; + lastSpellCheckText = text; + return suggestionSpansByText.containsKey(text) + ? suggestionSpansByText[text] + : const <SuggestionSpan>[]; + } +} class FakeFlutterView extends TestFlutterView { FakeFlutterView(TestFlutterView view, {required this.viewId})