getClosestGlyphForOffset method

GlyphInfo? getClosestGlyphForOffset(
  1. Offset offset
)

Returns the GlyphInfo of the glyph closest to the given offset in the paragraph coordinate system, or null if the text is empty, or is entirely clipped or ellipsized away.

This method first finds the line closest to offset.dy, and then returns the GlyphInfo of the closest glyph(s) within that line.

Implementation

ui.GlyphInfo? getClosestGlyphForOffset(Offset offset) {
  assert(_debugAssertTextLayoutIsValid);
  assert(!_debugNeedsRelayout);
  final _TextPainterLayoutCacheWithOffset cachedLayout = _layoutCache!;
  final ui.GlyphInfo? rawGlyphInfo = cachedLayout.paragraph.getClosestGlyphInfoForOffset(offset - cachedLayout.paintOffset);
  if (rawGlyphInfo == null || cachedLayout.paintOffset == Offset.zero) {
    return rawGlyphInfo;
  }
  return ui.GlyphInfo(rawGlyphInfo.graphemeClusterLayoutBounds.shift(cachedLayout.paintOffset), rawGlyphInfo.graphemeClusterCodeUnitRange, rawGlyphInfo.writingDirection);
}