convertTextPainterPostionToTextInputPostion function Null safety

TextPosition? convertTextPainterPostionToTextInputPostion(
  1. InlineSpan text,
  2. TextPosition? textPosition,
  3. {bool? end}
)

Implementation

TextPosition? convertTextPainterPostionToTextInputPostion(
    InlineSpan text, TextPosition? textPosition,
    {bool? end}) {
  if (textPosition != null) {
    int caretOffset = textPosition.offset;
    if (caretOffset <= 0) {
      return textPosition;
    }
    int textOffset = 0;
    text.visitChildren((InlineSpan ts) {
      if (ts is SpecialInlineSpanBase) {
        final SpecialInlineSpanBase specialTs = ts as SpecialInlineSpanBase;
        final int length = specialTs.actualText.length;
        caretOffset += length - getInlineOffset(ts);

        ///make sure caret is not in text when deleteAll is true
        if (specialTs.deleteAll &&
            caretOffset >= specialTs.start &&
            caretOffset <= specialTs.end) {
          if (end != null) {
            caretOffset = end ? specialTs.end : specialTs.start;
          } else {
            if (caretOffset >
                (specialTs.end - specialTs.start) / 2.0 + specialTs.start) {
              //move caretOffset to end
              caretOffset = specialTs.end;
            } else {
              caretOffset = specialTs.start;
            }
          }
          return false;
        }
      }
      textOffset += getInlineOffset(ts);
      if (textOffset >= textPosition.offset) {
        return false;
      }
      return true;
    });

    if (caretOffset != textPosition.offset) {
      return TextPosition(offset: caretOffset, affinity: textPosition.affinity);
    }
  }
  return textPosition;
}