makeSureCaretNotInSpecialText function Null safety

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

Implementation

TextPosition makeSureCaretNotInSpecialText(
    InlineSpan text, TextPosition textPosition) {
  final List<InlineSpan> list = <InlineSpan>[];
  if (list.isNotEmpty) {
    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;

        ///make sure caret is not in text when deleteAll is true
        if (specialTs.deleteAll &&
            caretOffset >= specialTs.start &&
            caretOffset <= specialTs.end) {
          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;
}