Allow spell check to fail silently on unsupported platforms & spell checkers (#122715)

Allow spell check to fail silently on unsupported platforms & spell checkers
This commit is contained in:
Camille Simon
2023-03-27 08:38:54 -07:00
committed by GitHub
parent e6d6471d45
commit 5e76e119d5
2 changed files with 48 additions and 28 deletions

View File

@@ -2409,21 +2409,35 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
/// If spell check is enabled, this will try to infer a value for
/// the [SpellCheckService] if left unspecified.
static SpellCheckConfiguration _inferSpellCheckConfiguration(SpellCheckConfiguration? configuration) {
if (configuration == null || configuration == const SpellCheckConfiguration.disabled()) {
final SpellCheckService? spellCheckService = configuration?.spellCheckService;
final bool spellCheckAutomaticallyDisabled = configuration == null || configuration == const SpellCheckConfiguration.disabled();
final bool spellCheckServiceIsConfigured = spellCheckService != null || 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.
assert(() {
if (!spellCheckAutomaticallyDisabled && !spellCheckServiceIsConfigured) {
FlutterError.reportError(
FlutterErrorDetails(
exception: FlutterError(
'Spell check was enabled with spellCheckConfiguration, but the '
'current platform does not have a supported spell check '
'service, and none was provided. Consider disabling spell '
'check for this platform or passing a SpellCheckConfiguration '
'with a specified spell check service.',
),
library: 'widget library',
stack: StackTrace.current,
),
);
}
return true;
}());
return const SpellCheckConfiguration.disabled();
}
SpellCheckService? spellCheckService = configuration.spellCheckService;
assert(
spellCheckService != null
|| WidgetsBinding.instance.platformDispatcher.nativeSpellCheckServiceDefined,
'spellCheckService must be specified for this platform because no default service available',
);
spellCheckService = spellCheckService ?? DefaultSpellCheckService();
return configuration.copyWith(spellCheckService: spellCheckService);
return configuration.copyWith(spellCheckService: spellCheckService ?? DefaultSpellCheckService());
}
/// Returns the [ContextMenuButtonItem]s for the given [ToolbarOptions].

View File

@@ -14838,28 +14838,34 @@ testWidgets('Floating cursor ending with selection', (WidgetTester tester) async
});
testWidgets(
'Error thrown when spell check enabled but no default spell check service available',
'Spell check disabled when spell check configuration specified but no default spell check service available',
(WidgetTester tester) async {
tester.binding.platformDispatcher.nativeSpellCheckServiceDefinedTestValue =
false;
await tester.pumpWidget(
EditableText(
controller: TextEditingController(text: 'A'),
focusNode: FocusNode(),
style: const TextStyle(),
cursorColor: Colors.blue,
backgroundCursorColor: Colors.grey,
cursorOpacityAnimates: true,
autofillHints: null,
spellCheckConfiguration:
const SpellCheckConfiguration(
misspelledTextStyle: TextField.materialMisspelledTextStyle,
),
));
MaterialApp(
home: EditableText(
controller: TextEditingController(text: 'A'),
focusNode: FocusNode(),
style: const TextStyle(),
cursorColor: Colors.blue,
backgroundCursorColor: Colors.grey,
cursorOpacityAnimates: true,
autofillHints: null,
spellCheckConfiguration:
const SpellCheckConfiguration(
misspelledTextStyle: TextField.materialMisspelledTextStyle,
),
),
)
);
expect(tester.takeException(), isA<AssertionError>());
tester.binding.platformDispatcher.clearNativeSpellCheckServiceDefined();
expect(tester.takeException(), isA<AssertionError>());
final EditableTextState state =
tester.state<EditableTextState>(find.byType(EditableText));
expect(state.spellCheckConfiguration, equals(const SpellCheckConfiguration.disabled()));
tester.binding.platformDispatcher.clearNativeSpellCheckServiceDefined();
});
testWidgets(