Unverified Commit 85ec8742 authored by Justin McCandless's avatar Justin McCandless Committed by GitHub

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.
parent 8cd11e91
...@@ -887,6 +887,27 @@ class CupertinoTextField extends StatefulWidget { ...@@ -887,6 +887,27 @@ class CupertinoTextField extends StatefulWidget {
return null; 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<CupertinoTextField> with RestorationMixin, AutomaticKeepAliveClientMixin<CupertinoTextField> implements TextSelectionGestureDetectorBuilderDelegate, AutofillClient { class _CupertinoTextFieldState extends State<CupertinoTextField> with RestorationMixin, AutomaticKeepAliveClientMixin<CupertinoTextField> implements TextSelectionGestureDetectorBuilderDelegate, AutofillClient {
...@@ -1298,18 +1319,9 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio ...@@ -1298,18 +1319,9 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
// ensure that configuration uses Cupertino text style for misspelled words // ensure that configuration uses Cupertino text style for misspelled words
// unless a custom style is specified. // unless a custom style is specified.
final SpellCheckConfiguration spellCheckConfiguration = final SpellCheckConfiguration spellCheckConfiguration =
widget.spellCheckConfiguration != null && CupertinoTextField.inferIOSSpellCheckConfiguration(
widget.spellCheckConfiguration != const SpellCheckConfiguration.disabled() widget.spellCheckConfiguration,
? widget.spellCheckConfiguration!.copyWith( );
misspelledTextStyle: widget.spellCheckConfiguration!.misspelledTextStyle
?? CupertinoTextField.cupertinoMisspelledTextStyle,
misspelledSelectionColor: widget.spellCheckConfiguration!.misspelledSelectionColor
?? CupertinoTextField.kMisspelledSelectionColor,
spellCheckSuggestionsToolbarBuilder:
widget.spellCheckConfiguration!.spellCheckSuggestionsToolbarBuilder
?? CupertinoTextField.defaultSpellCheckSuggestionsToolbarBuilder,
)
: const SpellCheckConfiguration.disabled();
final Widget paddedEditable = Padding( final Widget paddedEditable = Padding(
padding: widget.padding, padding: widget.padding,
......
...@@ -833,6 +833,25 @@ class TextField extends StatefulWidget { ...@@ -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 @override
State<TextField> createState() => _TextFieldState(); State<TextField> createState() => _TextFieldState();
...@@ -1236,19 +1255,24 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements ...@@ -1236,19 +1255,24 @@ class _TextFieldState extends State<TextField> with RestorationMixin implements
]; ];
// Set configuration as disabled if not otherwise specified. If specified, // Set configuration as disabled if not otherwise specified. If specified,
// ensure that configuration uses Material text style for misspelled words // ensure that configuration uses the correct style for misspelled words for
// unless a custom style is specified. // the current platform, unless a custom style is specified.
final SpellCheckConfiguration spellCheckConfiguration = final SpellCheckConfiguration spellCheckConfiguration;
widget.spellCheckConfiguration != null && switch (defaultTargetPlatform) {
widget.spellCheckConfiguration != const SpellCheckConfiguration.disabled() case TargetPlatform.iOS:
? widget.spellCheckConfiguration!.copyWith( case TargetPlatform.macOS:
misspelledTextStyle: widget.spellCheckConfiguration!.misspelledTextStyle spellCheckConfiguration =
?? TextField.materialMisspelledTextStyle, CupertinoTextField.inferIOSSpellCheckConfiguration(
spellCheckSuggestionsToolbarBuilder: widget.spellCheckConfiguration,
widget.spellCheckConfiguration!.spellCheckSuggestionsToolbarBuilder );
?? TextField.defaultSpellCheckSuggestionsToolbarBuilder, case TargetPlatform.android:
) case TargetPlatform.fuchsia:
: const SpellCheckConfiguration.disabled(); case TargetPlatform.linux:
case TargetPlatform.windows:
spellCheckConfiguration = TextField.inferAndroidSpellCheckConfiguration(
widget.spellCheckConfiguration,
);
}
TextSelectionControls? textSelectionControls = widget.selectionControls; TextSelectionControls? textSelectionControls = widget.selectionControls;
final bool paintCursorAboveText; final bool paintCursorAboveText;
......
...@@ -16257,6 +16257,64 @@ void main() { ...@@ -16257,6 +16257,64 @@ void main() {
expect(spellCheckToolbar, isA<SpellCheckSuggestionsToolbar>()); expect(spellCheckToolbar, isA<SpellCheckSuggestionsToolbar>());
} }
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.iOS })); }, variant: const TargetPlatformVariant(<TargetPlatform>{ 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<EditableTextState>(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>{ TargetPlatform.android, TargetPlatform.iOS }));
} }
/// A Simple widget for testing the obscure text. /// A Simple widget for testing the obscure text.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment