Skip to content

Commit dc66cf1

Browse files
committed
fix cursor at end when line contains image
1 parent 2b88eb1 commit dc66cf1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/src/widgets/cursor.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,21 @@ class CursorPainter {
243243
final double devicePixelRatio;
244244

245245
/// Paints cursor on [canvas] at specified [position].
246+
/// [offset] is global top left (x, y) of text line
247+
/// [position] is relative (x) in text line
246248
void paint(Canvas canvas, Offset offset, TextPosition position) {
247-
final caretOffset =
248-
editable!.getOffsetForCaret(position, prototype) + offset;
249+
// relative (x, y) to global offset
250+
var relativeCaretOffset = editable!.getOffsetForCaret(position, prototype);
251+
if (relativeCaretOffset == Offset.zero) {
252+
relativeCaretOffset = editable!.getOffsetForCaret(
253+
TextPosition(
254+
offset: position.offset - 1, affinity: position.affinity),
255+
prototype);
256+
// Hardcoded 6 as estimate of the width of a character
257+
relativeCaretOffset =
258+
Offset(relativeCaretOffset.dx + 6, relativeCaretOffset.dy);
259+
}
260+
final caretOffset = relativeCaretOffset + offset;
249261
var caretRect = prototype.shift(caretOffset);
250262
if (style.offset != null) {
251263
caretRect = caretRect.shift(style.offset!);

0 commit comments

Comments
 (0)