handleSpecialTextSpanDelete function Null safety

TextEditingValue handleSpecialTextSpanDelete(
  1. TextEditingValue value,
  2. TextEditingValue? oldValue,
  3. InlineSpan oldTextSpan,
  4. TextInputConnection? textInputConnection
)

Implementation

TextEditingValue handleSpecialTextSpanDelete(
    TextEditingValue value,
    TextEditingValue? oldValue,
    InlineSpan oldTextSpan,
    TextInputConnection? textInputConnection) {
  final String? oldText = oldValue?.text;
  String newText = value.text;

  ///take care of image span
  if (oldText != null && oldText.length > newText.length) {
    final int difStart = value.selection.extentOffset;
    //int difEnd = oldText.length - 1;
    // for (; difStart < newText.length; difStart++) {
    //   if (oldText[difStart] != newText[difStart]) {
    //     break;
    //   }
    // }

    int caretOffset = value.selection.extentOffset;
    if (difStart > 0) {
      oldTextSpan.visitChildren((InlineSpan span) {
        if (span is SpecialInlineSpanBase &&
            (span as SpecialInlineSpanBase).deleteAll) {
          final SpecialInlineSpanBase specialTs = span as SpecialInlineSpanBase;
          if (difStart > specialTs.start && difStart < specialTs.end) {
            //difStart = ts.start;
            newText = newText.replaceRange(specialTs.start, difStart, '');
            caretOffset -= difStart - specialTs.start;
            return false;
          }
        }
        return true;
      });

      if (newText != value.text) {
        value = TextEditingValue(
            text: newText,
            selection: value.selection.copyWith(
                baseOffset: caretOffset,
                extentOffset: caretOffset,
                affinity: value.selection.affinity,
                isDirectional: value.selection.isDirectional));
        textInputConnection?.setEditingState(value);
      }
    }
  }

  return value;
}