diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index 268319aeaf..1276d19d8d 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -784,7 +784,9 @@ class TextPainter { return _caretMetrics.offset; } - /// Returns the tight bounded height of the glyph at the given [position]. + /// {@template flutter.painting.textPainter.getFullHeightForCaret} + /// Returns the strut bounded height of the glyph at the given `position`. + /// {@endtemplate} /// /// Valid only after [layout] has been called. double? getFullHeightForCaret(TextPosition position, Rect caretPrototype) { diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index e5d2507d11..d724dd0082 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -744,6 +744,15 @@ class RenderParagraph extends RenderBox return _textPainter.getOffsetForCaret(position, caretPrototype); } + /// {@macro flutter.painting.textPainter.getFullHeightForCaret} + /// + /// Valid only after [layout]. + double? getFullHeightForCaret(TextPosition position) { + assert(!debugNeedsLayout); + _layoutTextWithConstraints(constraints); + return _textPainter.getFullHeightForCaret(position, Rect.zero); + } + /// Returns a list of rects that bound the given selection. /// /// A given selection might have more than one rect if this text painter diff --git a/packages/flutter/test/rendering/paragraph_test.dart b/packages/flutter/test/rendering/paragraph_test.dart index c9ae0786fe..71ed2fb0de 100644 --- a/packages/flutter/test/rendering/paragraph_test.dart +++ b/packages/flutter/test/rendering/paragraph_test.dart @@ -36,6 +36,17 @@ void main() { expect(offset50.dy, greaterThan(offset5.dy)); }); + test('getFullHeightForCaret control test', () { + final RenderParagraph paragraph = RenderParagraph( + const TextSpan(text: _kText,style: TextStyle(fontSize: 10.0)), + textDirection: TextDirection.ltr, + ); + layout(paragraph); + + final double height5 = paragraph.getFullHeightForCaret(const TextPosition(offset: 5)); + expect(height5, equals(10.0)); + }); + test('getPositionForOffset control test', () { final RenderParagraph paragraph = RenderParagraph( const TextSpan(text: _kText),