deleteForward method Null safety

void deleteForward(
  1. SelectionChangedCause cause
)

Deletes in the forward direction, from the current selection in textSelectionDelegate.

This method operates on the text/selection contained in textSelectionDelegate, and does not depend on selection.

If the selection is collapsed, deletes a single character after the cursor.

If the selection is not collapsed, deletes the selection.

The given SelectionChangedCause indicates the cause of this change and will be passed to onSelectionChanged.

See also:

  • delete, which is same but in the opposite direction.

Implementation

void deleteForward(SelectionChangedCause cause) {
  // TODO(LongCatIsLooong): remove this method from RenderEditable.
  // https://github.com/flutter/flutter/issues/80226.
  final TextEditingValue controllerValue = textSelectionDelegate.textEditingValue;
  final TextSelection selection = controllerValue.selection;

  if (!selection.isValid || _readOnly || _deleteNonEmptySelection(cause)) {
    return;
  }

  assert(selection.isCollapsed);
  final String textAfter = selection.textAfter(controllerValue.text);
  if (textAfter.isEmpty) {
    return;
  }

  final String textBefore = selection.textBefore(controllerValue.text);
  final int characterBoundary = nextCharacter(0, textAfter);
  final TextRange composing = controllerValue.composing;
  final TextRange newComposingRange = !composing.isValid || composing.isCollapsed
    ? TextRange.empty
    : TextRange(
      start: composing.start - (composing.start - textBefore.length).clamp(0, characterBoundary),
      end: composing.end - (composing.end - textBefore.length).clamp(0, characterBoundary),
    );
  _setTextEditingValue(
    TextEditingValue(
      text: textBefore + textAfter.substring(characterBoundary),
      selection: selection,
      composing: newComposingRange,
    ),
    cause,
  );
}