From 2b572ca4095e166f374d986b4cc9b3ac4e7848b5 Mon Sep 17 00:00:00 2001 From: li3317 Date: Fri, 10 Sep 2021 10:24:09 -0400 Subject: [PATCH] fix cursor when line contains image --- lib/src/widgets/cursor.dart | 18 ++++++++++++++++-- lib/src/widgets/text_line.dart | 8 ++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/src/widgets/cursor.dart b/lib/src/widgets/cursor.dart index 0365d317..d315f785 100644 --- a/lib/src/widgets/cursor.dart +++ b/lib/src/widgets/cursor.dart @@ -245,9 +245,23 @@ class CursorPainter { /// Paints cursor on [canvas] at specified [position]. /// [offset] is global top left (x, y) of text line /// [position] is relative (x) in text line - void paint(Canvas canvas, Offset offset, TextPosition position) { - final caretOffset = + void paint(Canvas canvas, Offset offset, TextPosition position, bool lineHasEmbed) { + var caretOffset = editable!.getOffsetForCaret(position, prototype) + offset; + if (lineHasEmbed) { + // relative (x, y) to global offset + var relativeCaretOffset = editable!.getOffsetForCaret(position, prototype); + if (relativeCaretOffset == Offset.zero) { + relativeCaretOffset = editable!.getOffsetForCaret( + TextPosition( + offset: position.offset - 1, affinity: position.affinity), + prototype); + // Hardcoded 6 as estimate of the width of a character + relativeCaretOffset = + Offset(relativeCaretOffset.dx + 6, relativeCaretOffset.dy); + } + caretOffset = relativeCaretOffset + offset; + } var caretRect = prototype.shift(caretOffset); if (style.offset != null) { diff --git a/lib/src/widgets/text_line.dart b/lib/src/widgets/text_line.dart index fb700e26..e4f69a5e 100644 --- a/lib/src/widgets/text_line.dart +++ b/lib/src/widgets/text_line.dart @@ -825,7 +825,7 @@ class RenderEditableTextLine extends RenderEditableBox { cursorCont.show.value && containsCursor() && !cursorCont.style.paintAboveText) { - _paintCursor(context, effectiveOffset); + _paintCursor(context, effectiveOffset, line.hasEmbed); } context.paintChild(_body!, effectiveOffset); @@ -834,7 +834,7 @@ class RenderEditableTextLine extends RenderEditableBox { cursorCont.show.value && containsCursor() && cursorCont.style.paintAboveText) { - _paintCursor(context, effectiveOffset); + _paintCursor(context, effectiveOffset, line.hasEmbed); } } } @@ -847,12 +847,12 @@ class RenderEditableTextLine extends RenderEditableBox { } } - void _paintCursor(PaintingContext context, Offset effectiveOffset) { + void _paintCursor(PaintingContext context, Offset effectiveOffset, bool lineHasEmbed) { final position = TextPosition( offset: textSelection.extentOffset - line.documentOffset, affinity: textSelection.base.affinity, ); - _cursorPainter.paint(context.canvas, effectiveOffset, position); + _cursorPainter.paint(context.canvas, effectiveOffset, position, lineHasEmbed); } @override