getRectForComposingRange method Null safety

Rect? getRectForComposingRange(
  1. TextRange range
)

Returns the smallest Rect, in the local coordinate system, that covers the text within the TextRange specified.

This method is used to calculate the approximate position of the IME bar on iOS.

Returns null if TextRange.isValid is false for the given range, or the given range is collapsed.

Implementation

Rect? getRectForComposingRange(TextRange range) {
  if (!range.isValid || range.isCollapsed)
    return null;
  _computeTextMetricsIfNeeded();

  final List<ui.TextBox> boxes = _textPainter.getBoxesForSelection(
    TextSelection(baseOffset: range.start, extentOffset: range.end),
    boxHeightStyle: selectionHeightStyle,
    boxWidthStyle: selectionWidthStyle,
  );

  return boxes.fold(
    null,
    (Rect? accum, TextBox incoming) => accum?.expandToInclude(incoming.toRect()) ?? incoming.toRect(),
  )?.shift(_paintOffset);
}