Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created text editing controller which builds TextSpans from list of M… #19

Merged
merged 14 commits into from
Apr 28, 2023
Merged
61 changes: 11 additions & 50 deletions lib/core/controllers/colored_text_editing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ class ColoredTextEditingController extends TextEditingController {
this.highlightStyle = const HighlightStyle(),
});

/// Clear mistakes list when text mas modified
void _handleTextChange(String newValue) {
if (newValue.length != text.length) {
_mistakes.clear();
nazarski marked this conversation as resolved.
Show resolved Hide resolved
}
nazarski marked this conversation as resolved.
Show resolved Hide resolved
}

/// Generates TextSpan from Mistake list
@override
TextSpan buildTextSpan({
required BuildContext context,
TextStyle? style,
required bool withComposing,
}) {
final int textLength = text.length;
final Iterable<TextSpan> spanList =
_generateSpans(textLength: textLength, style: style);
final Iterable<TextSpan> spanList = _generateSpans(
nazarski marked this conversation as resolved.
Show resolved Hide resolved
style: style,
);

return TextSpan(
children: spanList.toList(),
Expand All @@ -41,19 +48,12 @@ class ColoredTextEditingController extends TextEditingController {

/// Generator function to create TextSpan instances
Iterable<TextSpan> _generateSpans({
required int textLength,
// required int textLength,
nazarski marked this conversation as resolved.
Show resolved Hide resolved
TextStyle? style,
}) sync* {
int currentOffset = 0; // enter index

for (final Mistake mistake in _mistakes) {
/// Breaks the loop if iterated Mistake offset is bigger than text
/// length.
if (mistake.offset > textLength ||
mistake.offset + mistake.length > textLength) {
break;
}

/// TextSpan before mistake
yield TextSpan(
text: text.substring(
Expand Down Expand Up @@ -90,45 +90,6 @@ class ColoredTextEditingController extends TextEditingController {
);
}

/// Apply changes to Mistake list while new data being fetched
void _handleTextChange(String newText) {
final int deltaLength = newText.length - text.length;

/// Update the _mistakes list in-place based on the text modifications
_mistakes = _mistakes
.map((mistake) {
int newOffset = mistake.offset;
int newLength = mistake.length;

/// If the text modification starts within the mistake
if (selection.start >= mistake.offset &&
selection.start <= mistake.offset + mistake.length) {
newLength += deltaLength;
}

/// If the text modification starts before the mistake
else if (selection.start < mistake.offset) {
newOffset += deltaLength;
}

/// Return the updated mistake (if the length is greater than 0)
return newLength > 0
? Mistake(
message: mistake.message,
type: mistake.type,
offset: newOffset,
length: newLength,
replacements: mistake.replacements,
)
: null;
})
.whereType<Mistake>()
.toList();

/// Notify listeners to rebuild the widget
notifyListeners();
}

/// A method sets new list of Mistake and triggers buildTextSpan
void highlightMistakes(List<Mistake> list) {
_mistakes = list;
Expand Down
1 change: 0 additions & 1 deletion lib/presentation/language_tool_text_field.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:languagetool_textfield/core/controllers/colored_text_editing_controller.dart';
import 'package:languagetool_textfield/domain/highlight_style.dart';
import 'package:languagetool_textfield/domain/language_check_service.dart';

/// A TextField widget that checks the grammar using the given [langService]
Expand Down