Skip to content

Commit

Permalink
Made some minor adjustments and transferred the check service to the …
Browse files Browse the repository at this point in the history
…controller.
  • Loading branch information
nazarski committed Apr 28, 2023
1 parent e11c6f2 commit 8ac8f80
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 58 deletions.
26 changes: 14 additions & 12 deletions example/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@ class _AppState extends State<App> {
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();
}
}
53 changes: 33 additions & 20 deletions lib/core/controllers/colored_text_editing_controller.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Mistake> _mistakes = [];

Expand All @@ -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<void> _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();
}
}

Expand All @@ -37,12 +51,12 @@ class ColoredTextEditingController extends TextEditingController {
TextStyle? style,
required bool withComposing,
}) {
final Iterable<TextSpan> spanList = _generateSpans(
final formattedTextSpans = _generateSpans(
style: style,
);

return TextSpan(
children: spanList.toList(),
children: formattedTextSpans.toList(),
);
}

Expand All @@ -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;
Expand All @@ -90,12 +109,6 @@ class ColoredTextEditingController extends TextEditingController {
);
}

/// A method sets new list of Mistake and triggers buildTextSpan
void highlightMistakes(List<Mistake> list) {
_mistakes = list;
notifyListeners();
}

/// Returns color for mistake TextSpan style
Color _getMistakeColor(MistakeType type) {
switch (type) {
Expand Down
28 changes: 2 additions & 26 deletions lib/presentation/language_tool_text_field.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
Expand All @@ -34,37 +30,17 @@ class LanguageToolTextField extends StatefulWidget {
}

class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
/// Sends API request to get a list of Mistake
Future<void> _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(
padding: const EdgeInsets.all(24.0),
child: Center(
child: TextField(
controller: widget.coloredController,
onChanged: _check,
style: widget.style,
decoration: widget.decoration,
),
),
);
}

@override
void dispose() {
super.dispose();
widget.coloredController.dispose();
}
}

0 comments on commit 8ac8f80

Please sign in to comment.