deleteForwardByLine method Null safety

void deleteForwardByLine(
  1. SelectionChangedCause cause
)

Deletes a line in the forward direction from the current selection.

If the selection is collapsed, deletes a line after the cursor.

If the selection is not collapsed, deletes the selection.

If obscureText is true, it treats the whole text content as a single word.

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

See also:

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

Implementation

void deleteForwardByLine(SelectionChangedCause cause) {
  assert(_selection != null);

  if (_readOnly || !_selection!.isValid) {
    return;
  }

  if (!_selection!.isCollapsed) {
    return _deleteSelection(_selection!, cause);
  }

  // When the text is obscured, the whole thing is treated as one big line.
  if (obscureText) {
    return _deleteToEnd(_selection!, cause);
  }

  final String text = textSelectionDelegate.textEditingValue.text;
  String textAfter = _selection!.textAfter(text);
  if (textAfter.isEmpty) {
    return;
  }

  // When there is a line break, it shouldn't do anything.
  final bool isNextCharacterBreakLine = textAfter.codeUnitAt(0) == 0x0A;
  if (isNextCharacterBreakLine) {
    return;
  }

  final String textBefore = _selection!.textBefore(text);
  final TextSelection line = _getLineAtOffset(TextPosition(offset: textBefore.length));
  textAfter = textAfter.substring(line.end - textBefore.length, textAfter.length);

  _setTextEditingValue(
    TextEditingValue(text: textBefore + textAfter, selection: _selection!),
    cause,
  );
}