Merge pull request #885 from LucasXu0/improve_cursor_display_style

chore: improve cursor display style
This commit is contained in:
Nathan.fooo 2022-08-22 22:14:07 +08:00 committed by GitHub
commit f224bf8087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,18 +65,35 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
@override
Rect? getCursorRectInPosition(Position position) {
final textPosition = TextPosition(offset: position.offset);
final cursorOffset =
_renderParagraph.getOffsetForCaret(textPosition, Rect.zero);
final cursorHeight = widget.cursorHeight ??
_renderParagraph.getFullHeightForCaret(textPosition) ??
_placeholderRenderParagraph.getFullHeightForCaret(textPosition) ??
16.0; // default height
var cursorHeight = _renderParagraph.getFullHeightForCaret(textPosition);
var cursorOffset =
_renderParagraph.getOffsetForCaret(textPosition, Rect.zero);
if (cursorHeight == null) {
cursorHeight =
_placeholderRenderParagraph.getFullHeightForCaret(textPosition);
cursorOffset = _placeholderRenderParagraph.getOffsetForCaret(
textPosition, Rect.zero);
}
if (cursorHeight != null) {
// workaround: Calling the `getFullHeightForCaret` function will return
// the full height of rich text component instead of the plain text
// if we set the line height.
// So need to divide by the line height to get the expected value.
//
// And the default height of plain text is too short. Add a magic height
// to expand it.
const magicHeight = 3.0;
cursorOffset = cursorOffset.translate(
0, (cursorHeight - cursorHeight / _lineHeight) / 2.0);
cursorHeight /= _lineHeight;
cursorHeight += magicHeight;
}
final rect = Rect.fromLTWH(
cursorOffset.dx - (widget.cursorWidth / 2),
cursorOffset.dy,
widget.cursorWidth,
cursorHeight,
widget.cursorHeight ?? cursorHeight ?? 16.0,
);
return rect;
}