fix: the height of the selection rects in same line is not same

This commit is contained in:
Lucas.Xu 2022-08-11 10:59:59 +08:00
parent 50f8e1f5d0
commit 9b00a25004
2 changed files with 17 additions and 13 deletions

View File

@ -21,7 +21,6 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
@ -64,11 +63,6 @@ class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: _buildExpandableFab(),
);

View File

@ -1,3 +1,5 @@
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
@ -17,7 +19,7 @@ class FlowyRichText extends StatefulWidget {
const FlowyRichText({
Key? key,
this.cursorHeight,
this.cursorWidth = 2.0,
this.cursorWidth = 1.0,
this.textSpanDecorator,
this.placeholderText = ' ',
this.placeholderTextSpanDecorator,
@ -41,7 +43,8 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
final _textKey = GlobalKey();
final _placeholderTextKey = GlobalKey();
final lineHeight = 1.5;
final _lineHeight = 1.5;
double? _cursorHeight;
RenderParagraph get _renderParagraph =>
_textKey.currentContext?.findRenderObject() as RenderParagraph;
@ -54,6 +57,13 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
return _buildRichText(context);
}
@override
void didUpdateWidget(covariant FlowyRichText oldWidget) {
super.didUpdateWidget(oldWidget);
_cursorHeight = null;
}
@override
Position start() => Position(path: widget.textNode.path, offset: 0);
@ -66,7 +76,7 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
final textPosition = TextPosition(offset: position.offset);
final cursorOffset =
_renderParagraph.getOffsetForCaret(textPosition, Rect.zero);
final cursorHeight = widget.cursorHeight ??
_cursorHeight ??= widget.cursorHeight ??
_renderParagraph.getFullHeightForCaret(textPosition) ??
_placeholderRenderParagraph.getFullHeightForCaret(textPosition) ??
18.0; // default height
@ -74,7 +84,7 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
cursorOffset.dx - (widget.cursorWidth / 2),
cursorOffset.dy,
widget.cursorWidth,
cursorHeight,
_cursorHeight!,
);
}
@ -105,7 +115,7 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
extentOffset: selection.end.offset,
);
return _renderParagraph
.getBoxesForSelection(textSelection)
.getBoxesForSelection(textSelection, boxHeightStyle: BoxHeightStyle.max)
.map((box) => box.toRect())
.toList();
}
@ -147,7 +157,7 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
? Colors.transparent
: Colors.grey,
fontSize: baseFontSize,
height: lineHeight,
height: _lineHeight,
),
),
],
@ -203,7 +213,7 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
.map((insert) => RichTextStyle(
attributes: insert.attributes ?? {},
text: insert.content,
height: lineHeight,
height: _lineHeight,
).toTextSpan())
.toList(growable: false),
);