From 85ec874260a5f7ce5e6ac4b93f30f66f988deb8f Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Tue, 25 Apr 2023 11:33:45 -0700 Subject: [PATCH] iOS TextField spell check style (#125432) When using a Material TextField on iOS, the spell check selection style now defaults to the red iOS-style, not Android's blue. --- .../flutter/lib/src/cupertino/text_field.dart | 36 ++++++++---- .../flutter/lib/src/material/text_field.dart | 50 +++++++++++----- .../test/material/text_field_test.dart | 58 +++++++++++++++++++ 3 files changed, 119 insertions(+), 25 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index f470385bc1..4c4b940fce 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -887,6 +887,27 @@ class CupertinoTextField extends StatefulWidget { return null; } }); + + /// Returns a new [SpellCheckConfiguration] where the given configuration has + /// had any missing values replaced with their defaults for the iOS platform. + static SpellCheckConfiguration inferIOSSpellCheckConfiguration( + SpellCheckConfiguration? configuration, + ) { + if (configuration == null + || configuration == const SpellCheckConfiguration.disabled()) { + return const SpellCheckConfiguration.disabled(); + } + + return configuration.copyWith( + misspelledTextStyle: configuration.misspelledTextStyle + ?? CupertinoTextField.cupertinoMisspelledTextStyle, + misspelledSelectionColor: configuration.misspelledSelectionColor + ?? CupertinoTextField.kMisspelledSelectionColor, + spellCheckSuggestionsToolbarBuilder: + configuration.spellCheckSuggestionsToolbarBuilder + ?? CupertinoTextField.defaultSpellCheckSuggestionsToolbarBuilder, + ); + } } class _CupertinoTextFieldState extends State with RestorationMixin, AutomaticKeepAliveClientMixin implements TextSelectionGestureDetectorBuilderDelegate, AutofillClient { @@ -1298,18 +1319,9 @@ class _CupertinoTextFieldState extends State with Restoratio // ensure that configuration uses Cupertino text style for misspelled words // unless a custom style is specified. final SpellCheckConfiguration spellCheckConfiguration = - widget.spellCheckConfiguration != null && - widget.spellCheckConfiguration != const SpellCheckConfiguration.disabled() - ? widget.spellCheckConfiguration!.copyWith( - misspelledTextStyle: widget.spellCheckConfiguration!.misspelledTextStyle - ?? CupertinoTextField.cupertinoMisspelledTextStyle, - misspelledSelectionColor: widget.spellCheckConfiguration!.misspelledSelectionColor - ?? CupertinoTextField.kMisspelledSelectionColor, - spellCheckSuggestionsToolbarBuilder: - widget.spellCheckConfiguration!.spellCheckSuggestionsToolbarBuilder - ?? CupertinoTextField.defaultSpellCheckSuggestionsToolbarBuilder, - ) - : const SpellCheckConfiguration.disabled(); + CupertinoTextField.inferIOSSpellCheckConfiguration( + widget.spellCheckConfiguration, + ); final Widget paddedEditable = Padding( padding: widget.padding, diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index d5e410ee42..d36d6e666d 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -833,6 +833,25 @@ class TextField extends StatefulWidget { } } + /// Returns a new [SpellCheckConfiguration] where the given configuration has + /// had any missing values replaced with their defaults for the Android + /// platform. + static SpellCheckConfiguration inferAndroidSpellCheckConfiguration( + SpellCheckConfiguration? configuration, + ) { + if (configuration == null + || configuration == const SpellCheckConfiguration.disabled()) { + return const SpellCheckConfiguration.disabled(); + } + return configuration.copyWith( + misspelledTextStyle: configuration.misspelledTextStyle + ?? TextField.materialMisspelledTextStyle, + spellCheckSuggestionsToolbarBuilder: + configuration.spellCheckSuggestionsToolbarBuilder + ?? TextField.defaultSpellCheckSuggestionsToolbarBuilder, + ); + } + @override State createState() => _TextFieldState(); @@ -1236,19 +1255,24 @@ class _TextFieldState extends State with RestorationMixin implements ]; // Set configuration as disabled if not otherwise specified. If specified, - // ensure that configuration uses Material text style for misspelled words - // unless a custom style is specified. - final SpellCheckConfiguration spellCheckConfiguration = - widget.spellCheckConfiguration != null && - widget.spellCheckConfiguration != const SpellCheckConfiguration.disabled() - ? widget.spellCheckConfiguration!.copyWith( - misspelledTextStyle: widget.spellCheckConfiguration!.misspelledTextStyle - ?? TextField.materialMisspelledTextStyle, - spellCheckSuggestionsToolbarBuilder: - widget.spellCheckConfiguration!.spellCheckSuggestionsToolbarBuilder - ?? TextField.defaultSpellCheckSuggestionsToolbarBuilder, - ) - : const SpellCheckConfiguration.disabled(); + // ensure that configuration uses the correct style for misspelled words for + // the current platform, unless a custom style is specified. + final SpellCheckConfiguration spellCheckConfiguration; + switch (defaultTargetPlatform) { + case TargetPlatform.iOS: + case TargetPlatform.macOS: + spellCheckConfiguration = + CupertinoTextField.inferIOSSpellCheckConfiguration( + widget.spellCheckConfiguration, + ); + case TargetPlatform.android: + case TargetPlatform.fuchsia: + case TargetPlatform.linux: + case TargetPlatform.windows: + spellCheckConfiguration = TextField.inferAndroidSpellCheckConfiguration( + widget.spellCheckConfiguration, + ); + } TextSelectionControls? textSelectionControls = widget.selectionControls; final bool paintCursorAboveText; diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 4556653293..222a10cbee 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -16257,6 +16257,64 @@ void main() { expect(spellCheckToolbar, isA()); } }, variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.iOS })); + + testWidgets('Builds the corresponding default spell check configuration by platform', (WidgetTester tester) async { + tester.binding.platformDispatcher.nativeSpellCheckServiceDefinedTestValue = + true; + + final SpellCheckConfiguration expectedConfiguration; + switch (defaultTargetPlatform) { + case TargetPlatform.iOS: + case TargetPlatform.macOS: + expectedConfiguration = SpellCheckConfiguration( + misspelledTextStyle: CupertinoTextField.cupertinoMisspelledTextStyle, + misspelledSelectionColor: CupertinoTextField.kMisspelledSelectionColor, + spellCheckService: DefaultSpellCheckService(), + spellCheckSuggestionsToolbarBuilder: CupertinoTextField.defaultSpellCheckSuggestionsToolbarBuilder, + ); + case TargetPlatform.android: + case TargetPlatform.fuchsia: + case TargetPlatform.linux: + case TargetPlatform.windows: + expectedConfiguration = SpellCheckConfiguration( + misspelledTextStyle: TextField.materialMisspelledTextStyle, + spellCheckService: DefaultSpellCheckService(), + spellCheckSuggestionsToolbarBuilder: TextField.defaultSpellCheckSuggestionsToolbarBuilder, + ); + } + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: Center( + child: TextField( + autofocus: true, + spellCheckConfiguration: SpellCheckConfiguration(), + ), + ), + ), + ), + ); + + final EditableTextState editableTextState = + tester.state(find.byType(EditableText)); + + expect( + editableTextState.spellCheckConfiguration.misspelledTextStyle, + expectedConfiguration.misspelledTextStyle, + ); + expect( + editableTextState.spellCheckConfiguration.misspelledSelectionColor, + expectedConfiguration.misspelledSelectionColor, + ); + expect( + editableTextState.spellCheckConfiguration.spellCheckService.runtimeType, + expectedConfiguration.spellCheckService.runtimeType, + ); + expect( + editableTextState.spellCheckConfiguration.spellCheckSuggestionsToolbarBuilder, + expectedConfiguration.spellCheckSuggestionsToolbarBuilder, + ); + }, variant: const TargetPlatformVariant({ TargetPlatform.android, TargetPlatform.iOS })); } /// A Simple widget for testing the obscure text.