Skip to content

Commit e699716

Browse files
Merge pull request #59 from solid-software/bug/set-cursor-with-scroll
Bug/Unable to set the cursor with text scrolling
2 parents 423d6e4 + 736a543 commit e699716

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/core/controllers/colored_text_editing_controller.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class ColoredTextEditingController extends TextEditingController {
3636
/// Reference to the focus of the LanguageTool TextField
3737
FocusNode? focusNode;
3838

39+
/// Represents the scroll offset value of the LanguageTool TextField.
40+
double? scrollOffset;
41+
3942
Object? _fetchError;
4043

4144
/// An error that may have occurred during the API fetch.
@@ -367,10 +370,17 @@ class ColoredTextEditingController extends TextEditingController {
367370
textDirection: TextDirection.ltr,
368371
);
369372
final textFieldWidth = textFieldRenderBox?.size.width ?? 0;
373+
final scrollOffset = this.scrollOffset ?? 0;
374+
375+
double maxWidth = double.infinity;
376+
if (scrollOffset == 0) maxWidth = textFieldWidth;
377+
378+
textPainter.layout(minWidth: textFieldWidth, maxWidth: maxWidth);
370379

371-
textPainter.layout(maxWidth: textFieldWidth);
380+
final adjustedOffset =
381+
Offset(localOffset.dx + scrollOffset, localOffset.dy);
372382

373-
return textPainter.getPositionForOffset(localOffset).offset;
383+
return textPainter.getPositionForOffset(adjustedOffset).offset;
374384
}
375385

376386
/// The `onClosePopup` function is a callback method typically used

lib/presentation/language_tool_text_field.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ class LanguageToolTextField extends StatefulWidget {
4343
}
4444

4545
class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
46-
final focusNode = FocusNode();
46+
final _focusNode = FocusNode();
47+
final _scrollController = ScrollController();
4748

4849
@override
4950
void initState() {
5051
super.initState();
51-
widget.coloredController.focusNode = focusNode;
52+
widget.coloredController.focusNode = _focusNode;
5253
widget.coloredController.popupWidget = widget.mistakePopup;
54+
widget.coloredController.addListener(_textControllerListener);
5355
}
5456

5557
@override
@@ -79,7 +81,8 @@ class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
7981
padding: const EdgeInsets.all(_padding),
8082
child: Center(
8183
child: TextField(
82-
focusNode: focusNode,
84+
scrollController: _scrollController,
85+
focusNode: _focusNode,
8386
controller: widget.coloredController,
8487
style: widget.style,
8588
decoration: inputDecoration,
@@ -92,4 +95,14 @@ class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
9295
},
9396
);
9497
}
98+
99+
void _textControllerListener() =>
100+
widget.coloredController.scrollOffset = _scrollController.offset;
101+
102+
@override
103+
void dispose() {
104+
_focusNode.dispose();
105+
_scrollController.dispose();
106+
super.dispose();
107+
}
95108
}

0 commit comments

Comments
 (0)