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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData( theme: ThemeData(
// This is the theme of your application. // This is the theme of your application.
// //
@ -64,11 +63,6 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( 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(), body: _buildBody(),
floatingActionButton: _buildExpandableFab(), floatingActionButton: _buildExpandableFab(),
); );

View File

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