getLocalRectForCaret method

Rect getLocalRectForCaret(
  1. TextPosition caretPosition
)

Returns the Rect in local coordinates for the caret at the given text position.

See also:

Implementation

Rect getLocalRectForCaret(TextPosition caretPosition) {
  _computeTextMetricsIfNeeded();
  final Rect caretPrototype = _caretPrototype;
  final Offset caretOffset = _textPainter.getOffsetForCaret(caretPosition, caretPrototype);
  Rect caretRect = caretPrototype.shift(caretOffset + cursorOffset);
  final double scrollableWidth = math.max(_textPainter.width + _caretMargin, size.width);

  final double caretX = clampDouble(caretRect.left, 0, math.max(scrollableWidth - _caretMargin, 0));
  caretRect = Offset(caretX, caretRect.top) & caretRect.size;

  final double caretHeight = cursorHeight;
  switch (defaultTargetPlatform) {
    case TargetPlatform.iOS:
    case TargetPlatform.macOS:
      final double fullHeight = _textPainter.getFullHeightForCaret(caretPosition, caretPrototype) ?? _textPainter.preferredLineHeight;
      final double heightDiff = fullHeight - caretRect.height;
      // Center the caret vertically along the text.
      caretRect = Rect.fromLTWH(
        caretRect.left,
        caretRect.top + heightDiff / 2,
        caretRect.width,
        caretRect.height,
      );
    case TargetPlatform.android:
    case TargetPlatform.fuchsia:
    case TargetPlatform.linux:
    case TargetPlatform.windows:
      // Override the height to take the full height of the glyph at the TextPosition
      // when not on iOS. iOS has special handling that creates a taller caret.
      // TODO(garyq): See the TODO for _computeCaretPrototype().
      caretRect = Rect.fromLTWH(
        caretRect.left,
        caretRect.top - _kCaretHeightOffset,
        caretRect.width,
        caretHeight,
      );
  }

  caretRect = caretRect.shift(_paintOffset);
  return caretRect.shift(_snapToPhysicalPixel(caretRect.topLeft));
}