@@ -65,18 +65,35 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
6565 @override
6666 Rect ? getCursorRectInPosition (Position position) {
6767 final textPosition = TextPosition (offset: position.offset);
68- final cursorOffset =
69- _renderParagraph.getOffsetForCaret (textPosition, Rect .zero);
70- final cursorHeight = widget.cursorHeight ??
71- _renderParagraph.getFullHeightForCaret (textPosition) ??
72- _placeholderRenderParagraph.getFullHeightForCaret (textPosition) ??
73- 16.0 ; // default height
7468
69+ var cursorHeight = _renderParagraph.getFullHeightForCaret (textPosition);
70+ var cursorOffset =
71+ _renderParagraph.getOffsetForCaret (textPosition, Rect .zero);
72+ if (cursorHeight == null ) {
73+ cursorHeight =
74+ _placeholderRenderParagraph.getFullHeightForCaret (textPosition);
75+ cursorOffset = _placeholderRenderParagraph.getOffsetForCaret (
76+ textPosition, Rect .zero);
77+ }
78+ if (cursorHeight != null ) {
79+ // workaround: Calling the `getFullHeightForCaret` function will return
80+ // the full height of rich text component instead of the plain text
81+ // if we set the line height.
82+ // So need to divide by the line height to get the expected value.
83+ //
84+ // And the default height of plain text is too short. Add a magic height
85+ // to expand it.
86+ const magicHeight = 3.0 ;
87+ cursorOffset = cursorOffset.translate (
88+ 0 , (cursorHeight - cursorHeight / _lineHeight) / 2.0 );
89+ cursorHeight /= _lineHeight;
90+ cursorHeight += magicHeight;
91+ }
7592 final rect = Rect .fromLTWH (
7693 cursorOffset.dx - (widget.cursorWidth / 2 ),
7794 cursorOffset.dy,
7895 widget.cursorWidth,
79- cursorHeight,
96+ widget. cursorHeight ?? cursorHeight ?? 16.0 ,
8097 );
8198 return rect;
8299 }
0 commit comments