convertTextInputPostionToTextPainterPostion function Null safety

TextPosition convertTextInputPostionToTextPainterPostion(
  1. InlineSpan text,
  2. TextPosition textPosition
)

Implementation

TextPosition convertTextInputPostionToTextPainterPostion(
    InlineSpan text, TextPosition textPosition) {
  int caretOffset = textPosition.offset;
  int textOffset = 0;
  text.visitChildren((InlineSpan ts) {
    if (ts is SpecialInlineSpanBase) {
      final int length = (ts as SpecialInlineSpanBase).actualText.length;
      caretOffset -= length - getInlineOffset(ts);
      textOffset += length;
    } else {
      textOffset += getInlineOffset(ts);
    }
    if (textOffset >= textPosition.offset) {
      return false;
    }
    return true;
  });
  if (caretOffset != textPosition.offset) {
    return TextPosition(
        offset: max(0, caretOffset), affinity: textPosition.affinity);
  }

  return textPosition;
}