Feat: update example of spell checker to a better API implementation

pull/2133/head
CatHood0 8 months ago
parent 96b4e5b04a
commit f69add159c
  1. 18
      README.md
  2. 20
      example/lib/screens/quill/quill_screen.dart
  3. 9
      flutter_quill_extensions/lib/src/editor/spell_checker/simple_spell_checker_service.dart
  4. 3
      lib/src/editor/spellchecker/default_spellchecker_service.dart
  5. 3
      lib/src/editor/spellchecker/spellchecker_service.dart

@ -308,7 +308,7 @@ A spell checker is a software tool or feature integrated into various text proce
* **Portuguese** - `pt` (may contain errors or missing words) * **Portuguese** - `pt` (may contain errors or missing words)
* **Swedish** - `sv` (may contain errors or missing words) * **Swedish** - `sv` (may contain errors or missing words)
_**Note**: **If you have knowledge about any of these available languages or the unsupported ones**, you can make a pull request to add support or add words that are not currently in [simple_spell_checker](https://github.com/CatHood0/simple_spell_checker)_. _**Note**: If you have knowledge about any of these available languages or the unsupported ones, you can make a pull request to add support or add words that are not currently in [simple_spell_checker](https://github.com/CatHood0/simple_spell_checker)_.
In order to activate this functionality you can use the following code: In order to activate this functionality you can use the following code:
@ -324,7 +324,8 @@ When you no longer need to have the Spell checker activated you can simply use `
// dispose all service and it cannot be used after this // dispose all service and it cannot be used after this
SpellCheckerServiceProvider.dispose(); SpellCheckerServiceProvider.dispose();
``` ```
If what we want is to **temporarily deactivate the service** without deleting the values that are already stored in it, we can set `onlyPartial` to `true` so that it only closes the internal `streams` and prevents the dictionaries and values already registered from being reset.
If what we want is to **close the StreamControllers** without deleting the values that are already stored in it, we can set `onlyPartial` to `true`.
```dart ```dart
// it can be still used by the editor // it can be still used by the editor
@ -334,16 +335,9 @@ SpellCheckerServiceProvider.dispose(onlyPartial: true);
One use of this would be having the opportunity to **activate and deactivate** the service when we want, we can see this in the example that we have in this package, in which you can see that on each screen, we have a button that dynamically activates and deactivates the service. To do this is pretty simple: One use of this would be having the opportunity to **activate and deactivate** the service when we want, we can see this in the example that we have in this package, in which you can see that on each screen, we have a button that dynamically activates and deactivates the service. To do this is pretty simple:
```dart ```dart
final bool _isActivatedSpellChecker = false; SpellCheckerServiceProvider.toggleState();
if (!_isActivatedSpellChecker) { isActivatedSpellChecker = SpellCheckerServiceProvider.isServiceActive();
FlutterQuillExtensions.useSpellCheckerService(Localizations.localeOf(context).languageCode); setState(() {});
} else {
// close the internal streams without completely closing the service
SpellCheckerServiceProvider.dispose(onlyPartial: true);
// since onlyPartial is true then we must manually disable the service for it to take effect in the UI
SpellCheckerServiceProvider.turnOffService();
}
_isActivatedSpellChecker = !_isActivatedSpellChecker;
``` ```
Open this [page](https://pub.dev/packages/simple_spell_checker) for more information. Open this [page](https://pub.dev/packages/simple_spell_checker) for more information.

@ -14,7 +14,7 @@ import '../shared/widgets/home_screen_button.dart';
import 'my_quill_editor.dart'; import 'my_quill_editor.dart';
import 'my_quill_toolbar.dart'; import 'my_quill_toolbar.dart';
var _isActivatedSpellChecker = false; var _isSpellcheckerActive = false;
@immutable @immutable
class QuillScreenArgs { class QuillScreenArgs {
@ -61,6 +61,11 @@ class _QuillScreenState extends State<QuillScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
_controller.readOnly = _isReadOnly; _controller.readOnly = _isReadOnly;
if (!_isSpellcheckerActive) {
_isSpellcheckerActive = true;
FlutterQuillExtensions.useSpellCheckerService(
Localizations.localeOf(context).languageCode);
}
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Flutter Quill'), title: const Text('Flutter Quill'),
@ -68,19 +73,14 @@ class _QuillScreenState extends State<QuillScreen> {
IconButton( IconButton(
tooltip: 'Spell-checker', tooltip: 'Spell-checker',
onPressed: () { onPressed: () {
if (!_isActivatedSpellChecker) { SpellCheckerServiceProvider.toggleState();
FlutterQuillExtensions.useSpellCheckerService(
Localizations.localeOf(context).languageCode);
} else {
SpellCheckerServiceProvider.dispose(onlyPartial: true);
SpellCheckerServiceProvider.turnOffService();
}
_isActivatedSpellChecker = !_isActivatedSpellChecker;
setState(() {}); setState(() {});
}, },
icon: Icon( icon: Icon(
Icons.document_scanner, Icons.document_scanner,
color: _isActivatedSpellChecker ? Colors.red : null, color: SpellCheckerServiceProvider.isServiceActive()
? Colors.red.withOpacity(0.5)
: null,
), ),
), ),
IconButton( IconButton(

@ -5,7 +5,8 @@ import 'package:simple_spell_checker/simple_spell_checker.dart';
/// SimpleSpellChecker is a simple spell checker for get /// SimpleSpellChecker is a simple spell checker for get
/// all words divide on different objects if them are wrong or not /// all words divide on different objects if them are wrong or not
class SimpleSpellCheckerService extends SpellCheckerService<LanguageIdentifier> { class SimpleSpellCheckerService
extends SpellCheckerService<LanguageIdentifier> {
SimpleSpellCheckerService({required super.language}) SimpleSpellCheckerService({required super.language})
: checker = SimpleSpellChecker( : checker = SimpleSpellChecker(
language: language, language: language,
@ -20,11 +21,13 @@ class SimpleSpellCheckerService extends SpellCheckerService<LanguageIdentifier>
@override @override
List<TextSpan>? checkSpelling( List<TextSpan>? checkSpelling(
String text, { String text, {
LongPressGestureRecognizer Function(String word)? customLongPressRecognizerOnWrongSpan, LongPressGestureRecognizer Function(String word)?
customLongPressRecognizerOnWrongSpan,
}) { }) {
return checker.check( return checker.check(
text, text,
customLongPressRecognizerOnWrongSpan: customLongPressRecognizerOnWrongSpan, customLongPressRecognizerOnWrongSpan:
customLongPressRecognizerOnWrongSpan,
); );
} }

@ -14,7 +14,8 @@ class DefaultSpellCheckerService extends SpellCheckerService<Object?> {
@override @override
List<TextSpan>? checkSpelling( List<TextSpan>? checkSpelling(
String text, { String text, {
LongPressGestureRecognizer Function(String p1)? customLongPressRecognizerOnWrongSpan, LongPressGestureRecognizer Function(String p1)?
customLongPressRecognizerOnWrongSpan,
}) { }) {
return null; return null;
} }

@ -34,5 +34,6 @@ abstract class SpellCheckerService<T> {
/// ///
/// Returns a [List<TextSpan>] with all misspelled words divide from the right words. /// Returns a [List<TextSpan>] with all misspelled words divide from the right words.
List<TextSpan>? checkSpelling(String text, List<TextSpan>? checkSpelling(String text,
{LongPressGestureRecognizer Function(String)? customLongPressRecognizerOnWrongSpan}); {LongPressGestureRecognizer Function(String)?
customLongPressRecognizerOnWrongSpan});
} }

Loading…
Cancel
Save