Simple Spell Checker is a simple but powerful spell checker, that allows to all developers detect and highlight spelling errors in text. The package also allows customization of languages, providing efficient and adaptable spell-checking for various applications.
The package already have a default list of words for these languages:
- German -
de
,de-ch
- English -
en
,en-gb
- Spanish -
es
- Catalan -
ca
- Arabic -
ar
- Danish -
da
- French -
fr
- Bulgarian -
bg
- Dutch -
nl
- Korean -
ko
- Estonian -
et
- Hebrew -
he
- Italian -
it
- Norwegian -
no
- Portuguese -
pt
- Russian -
ru
Add the Dependency:
dependencies:
simple_spell_checker: <latest_version>
Note
You will need to add some of the available dependencies that contains the supported languages by default. Since the 1.3.0
of the package, all the dictionaries was removed and reimplemented into a separate package. Check Current languages supported.
if you use some of these packages to register a language, please, call register functions before use the SimpleSpellChecker
to avoid any unexpected behavior.
Import the necessary components into your Dart
file and initialize the Spell-Checker:
SimpleSpellChecker
is a single language checker.
import 'package:simple_spell_checker/simple_spell_checker.dart';
SimpleSpellChecker spellChecker = SimpleSpellChecker(
language: 'en', // the current language that the user is using
whiteList: <String>[],
caseSensitive: false,
);
You can set your own dictionary using setLanguage
:
import 'package:simple_spell_checker/simple_spell_checker.dart';
SimpleSpellChecker.setLanguage('sk', <String, int>{});
//we can use unlearnWord and learnWord to register/remove a word from a registered language
// to learn a word
SimpleSpellChecker.learnWord('sk', 'word_that_will_be_registered');
// to unlearn a word
SimpleSpellChecker.unlearnWord('sk', 'word_that_will_be_removed');
Registering the default supported languages:
import 'package:simple_spell_checker_en_lan/simple_spell_checker_en_lan.dart';
import 'package:simple_spell_checker_he_lan/simple_spell_checker_he_lan.dart';
import 'package:simple_spell_checker_ru_lan/simple_spell_checker_ru_lan.dart';
// to register the language use
SimpleSpellCheckerHeRegister.registerLan();
SimpleSpellCheckerRuRegister.registerLan();
// for english you can use `en` or `en-ch`
// by default is `en`
SimpleSpellCheckerEnRegister.registerLan(preferEnglish: 'en');
// to remove the language
SimpleSpellCheckerHeRegister.removeLan();
SimpleSpellCheckerRuRegister.removeLan();
SimpleSpellCheckerEnRegister.removeLan();
Use the check()
method to analyze a String
for spelling errors that return a list of spans with misspelled words:
List<TextSpan>? result = spellChecker.check(
'Your text here',
wrongStyle: TextStyle(backgroundColor: Colors.red.withOpacity(0.2)), // set you custom style to the wrong spans
commonStyle: TextStyle(your_normal_styles_for_non_wrong_words),
);
Use the checkBuilder<T>()
method to analyze a String
for spelling errors and build your own widget with the text:
List<Widget>? result = spellChecker.checkBuilder<Widget>(
'Your text here',
builder: (word, isValid) {
return Text(word, style: TextStyle(color: !isValid ? Colors.red : null));
}
);
Use the wordTokenizer
param from constructor to set a custom instance of your Tokenizer
or use setNewTokenizer()
or setWordTokenizerToDefault()
. By default on MultiSpellChecker
and SimpleSpellChecker
only accept Tokenizer
implementations with List<String>
types only.
/// custom tokenizer implemented by the package
class CustomWordTokenizer extends Tokenizer<List<String>> {
CustomWordTokenizer() : super(separatorRegExp: RegExp(r'\S+|\s+'));
@override
bool canTokenizeText(String text) {
return separatorRegExp!.hasMatch(text);
}
/// Divides a string into words
@override
List<String> tokenize(
String content, {
bool removeAllEmptyWords = false,
}) {
final List<String> words = separatorRegExp!.allMatches(content).map((match) => match.group(0)!).toList();
return [...words];
}
}
- setNewLanguageToState(String language): override the current language into the Spell Checker. Only available for
SimpleSpellChecker
instances
- SetNewWhiteList(List words): override the current white list into the Spell Checker.
- addNewWordToWhiteList(String words): add a new word to the white list.
- whiteList: return the current white list state.
- toggleChecker(): activate or deactivate the spell checking. If it is deactivate
check()
methods always will return null - isActiveChecking(): return the state of the spell checker.
- checkBuilder: Use the
checkBuilder()
method for a custom widget-based approach to handling spelling errors. - customLongPressRecognizerOnWrongSpan: Attach custom gesture recognizers to wrong words for tailored interactions.
The SimpleSpellChecker
class provides a stream (stream getter) that broadcasts updates whenever the spell-checker state changes (by now, we just pass the current state of the object list that is always updated when add a new object). This is useful for reactive UI updates.
spellChecker.languageStream.listen((event) {
print("Spell check language state updated.");
});
When the SimpleSpellChecker
is no longer needed, ensure you dispose of it properly:
//Avoid reuse this spellchecker after dispose since it will throws error
spellChecker.dispose();
Or also, if you don't need listen the StreamControllers then you can dispose them:
//Avoid reuse the streams of the spellchecker after dispose since it will throws error
spellChecker.disposeControllers();
It clears any cached data and closes the internal stream to prevent memory leaks.