diff --git a/example/lib/app.dart b/example/lib/app.dart index 2f518fe..b6b2ad6 100644 --- a/example/lib/app.dart +++ b/example/lib/app.dart @@ -15,31 +15,33 @@ class _AppState extends State { static final LanguageTool _languageTool = LanguageTool(); /// Initialize DebounceLangToolService - final DebounceLangToolService _debouncedLangService; + static final DebounceLangToolService _debouncedLangService = + DebounceLangToolService( + LangToolService(_languageTool), + const Duration(milliseconds: 500), + ); /// Initialize ColoredTextEditingController - final ColoredTextEditingController controller = - ColoredTextEditingController(); - - /// Set DebounceLangToolService - _AppState() - : _debouncedLangService = DebounceLangToolService( - LangToolService(_languageTool), - const Duration(milliseconds: 500), - ); + final ColoredTextEditingController _controller = + ColoredTextEditingController(languageCheckService: _debouncedLangService); @override Widget build(BuildContext context) { return Material( child: LanguageToolTextField( - langService: _debouncedLangService, style: const TextStyle(), decoration: const InputDecoration(), mistakeBuilder: () { return Container(); }, - coloredController: controller, + coloredController: _controller, ), ); } + + @override + void dispose() { + _controller.dispose(); + super.dispose(); + } } diff --git a/lib/core/controllers/colored_text_editing_controller.dart b/lib/core/controllers/colored_text_editing_controller.dart index ffe498c..0bd5f50 100644 --- a/lib/core/controllers/colored_text_editing_controller.dart +++ b/lib/core/controllers/colored_text_editing_controller.dart @@ -1,6 +1,8 @@ + import 'package:flutter/material.dart'; import 'package:languagetool_textfield/core/enums/mistake_type.dart'; import 'package:languagetool_textfield/domain/highlight_style.dart'; +import 'package:languagetool_textfield/domain/language_check_service.dart'; import 'package:languagetool_textfield/domain/mistake.dart'; /// A TextEditingController with overrides buildTextSpan for building @@ -9,6 +11,9 @@ class ColoredTextEditingController extends TextEditingController { /// Color scheme to highlight mistakes final HighlightStyle highlightStyle; + /// Language tool API index + final LanguageCheckService languageCheckService; + /// List which contains Mistake objects spans are built from List _mistakes = []; @@ -20,13 +25,22 @@ class ColoredTextEditingController extends TextEditingController { /// Controller constructor ColoredTextEditingController({ + required this.languageCheckService, this.highlightStyle = const HighlightStyle(), }); - /// Clear mistakes list when text mas modified - void _handleTextChange(String newValue) { - if (newValue.length != text.length) { + /// Clear mistakes list when text mas modified and get a new list of mistakes + /// via API + Future _handleTextChange(String newText) async { + ///set value triggers each time, even when cursor changes its location + ///so this check avoid cleaning Mistake list when text wasn't really changed + if (newText.length != text.length) { _mistakes.clear(); + final mistakes = await languageCheckService.findMistakes(newText); + if (mistakes.isNotEmpty) { + _mistakes = mistakes; + } + notifyListeners(); } } @@ -37,12 +51,12 @@ class ColoredTextEditingController extends TextEditingController { TextStyle? style, required bool withComposing, }) { - final Iterable spanList = _generateSpans( + final formattedTextSpans = _generateSpans( style: style, ); return TextSpan( - children: spanList.toList(), + children: formattedTextSpans.toList(), ); } @@ -68,16 +82,21 @@ class ColoredTextEditingController extends TextEditingController { /// Mistake highlighted TextSpan yield TextSpan( - text: text.substring(mistake.offset, mistake.offset + mistake.length), - mouseCursor: MaterialStateMouseCursor.clickable, - style: style?.copyWith( - backgroundColor: mistakeColor.withOpacity( - highlightStyle.backgroundOpacity, + children: [ + TextSpan( + text: + text.substring(mistake.offset, mistake.offset + mistake.length), + mouseCursor: MaterialStateMouseCursor.clickable, + style: style?.copyWith( + backgroundColor: mistakeColor.withOpacity( + highlightStyle.backgroundOpacity, + ), + decoration: TextDecoration.underline, + decorationColor: mistakeColor, + decorationThickness: highlightStyle.mistakeLineThickness, + ), ), - decoration: TextDecoration.underline, - decorationColor: mistakeColor, - decorationThickness: highlightStyle.mistakeLineThickness, - ), + ], ); currentOffset = mistake.offset + mistake.length; @@ -90,12 +109,6 @@ class ColoredTextEditingController extends TextEditingController { ); } - /// A method sets new list of Mistake and triggers buildTextSpan - void highlightMistakes(List list) { - _mistakes = list; - notifyListeners(); - } - /// Returns color for mistake TextSpan style Color _getMistakeColor(MistakeType type) { switch (type) { diff --git a/lib/presentation/language_tool_text_field.dart b/lib/presentation/language_tool_text_field.dart index 9d073c5..b51523c 100644 --- a/lib/presentation/language_tool_text_field.dart +++ b/lib/presentation/language_tool_text_field.dart @@ -1,12 +1,9 @@ import 'package:flutter/material.dart'; import 'package:languagetool_textfield/core/controllers/colored_text_editing_controller.dart'; -import 'package:languagetool_textfield/domain/language_check_service.dart'; -/// A TextField widget that checks the grammar using the given [langService] +/// A TextField widget that checks the grammar using the given +/// [coloredController] class LanguageToolTextField extends StatefulWidget { - /// A service for checking errors. - final LanguageCheckService langService; - /// A style to use for the text being edited. final TextStyle style; @@ -22,7 +19,6 @@ class LanguageToolTextField extends StatefulWidget { /// Creates a widget that checks grammar errors. const LanguageToolTextField({ Key? key, - required this.langService, required this.style, required this.decoration, this.mistakeBuilder, @@ -34,19 +30,6 @@ class LanguageToolTextField extends StatefulWidget { } class _LanguageToolTextFieldState extends State { - /// Sends API request to get a list of Mistake - Future _check(String text) async { - final mistakes = await widget.langService.findMistakes(text); - if (mistakes.isNotEmpty) { - widget.coloredController.highlightMistakes(mistakes); - } - } - - @override - void initState() { - super.initState(); - } - @override Widget build(BuildContext context) { return Padding( @@ -54,17 +37,10 @@ class _LanguageToolTextFieldState extends State { child: Center( child: TextField( controller: widget.coloredController, - onChanged: _check, style: widget.style, decoration: widget.decoration, ), ), ); } - - @override - void dispose() { - super.dispose(); - widget.coloredController.dispose(); - } }