Unverified Commit 5e76e119 authored by Camille Simon's avatar Camille Simon Committed by GitHub

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

Allow spell check to fail silently on unsupported platforms & spell checkers
parent e6d6471d
......@@ -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].
......
......@@ -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(
......
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