diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index fa99754af2..130678b9ac 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2409,21 +2409,35 @@ class EditableTextState extends State 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]. diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 3fe5f729ea..0c919c7839 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -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()); - tester.binding.platformDispatcher.clearNativeSpellCheckServiceDefined(); + expect(tester.takeException(), isA()); + final EditableTextState state = + tester.state(find.byType(EditableText)); + expect(state.spellCheckConfiguration, equals(const SpellCheckConfiguration.disabled())); + tester.binding.platformDispatcher.clearNativeSpellCheckServiceDefined(); }); testWidgets(