getGlyphHeights method

({double endGlyphHeight, double startGlyphHeight}) getGlyphHeights()

Gets the line heights at the start and end of the selection for the given EditableTextState.

See also:

Implementation

({double startGlyphHeight, double endGlyphHeight}) getGlyphHeights() {
  final TextSelection selection = textEditingValue.selection;

  // Only calculate handle rects if the text in the previous frame
  // is the same as the text in the current frame. This is done because
  // widget.renderObject contains the renderEditable from the previous frame.
  // If the text changed between the current and previous frames then
  // widget.renderObject.getRectForComposingRange might fail. In cases where
  // the current frame is different from the previous we fall back to
  // renderObject.preferredLineHeight.
  final InlineSpan span = renderEditable.text!;
  final String prevText = span.toPlainText();
  final String currText = textEditingValue.text;
  if (prevText != currText || !selection.isValid || selection.isCollapsed) {
    return (
      startGlyphHeight: renderEditable.preferredLineHeight,
      endGlyphHeight: renderEditable.preferredLineHeight,
    );
  }

  final String selectedGraphemes = selection.textInside(currText);
  final int firstSelectedGraphemeExtent = selectedGraphemes.characters.first.length;
  final Rect? startCharacterRect = renderEditable.getRectForComposingRange(TextRange(
    start: selection.start,
    end: selection.start + firstSelectedGraphemeExtent,
  ));
  final int lastSelectedGraphemeExtent = selectedGraphemes.characters.last.length;
  final Rect? endCharacterRect = renderEditable.getRectForComposingRange(TextRange(
    start: selection.end - lastSelectedGraphemeExtent,
    end: selection.end,
  ));
  return (
    startGlyphHeight: startCharacterRect?.height ?? renderEditable.preferredLineHeight,
    endGlyphHeight: endCharacterRect?.height ?? renderEditable.preferredLineHeight,
  );
}