extendSelectionLeftByLine method Null safety

void extendSelectionLeftByLine(
  1. SelectionChangedCause cause

Extend the current selection to the start of TextSelection.extentOffset's line.

Uses TextSelection.baseOffset as a pivot point and doesn't change it. If TextSelection.extentOffset is right of TextSelection.baseOffset, then collapses the selection.

If selectionEnabled is false, keeps the selection collapsed and moves it left by line.

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

See also:


void extendSelectionLeftByLine(SelectionChangedCause cause) {
  assert(selection != null);

  if (!selectionEnabled) {
    return moveSelectionLeftByLine(cause);

  // When going left, we want to skip over any whitespace before the line,
  // so we go back to the first non-whitespace before asking for the line
  // bounds, since _getLineAtOffset finds the line boundaries without
  // including whitespace (like the newline).
  final int startPoint = previousCharacter(selection!.extentOffset, _plainText, false);
  final TextSelection selectedLine = _getLineAtOffset(TextPosition(offset: startPoint));

  late final TextSelection nextSelection;
  if (selection!.extentOffset > selection!.baseOffset) {
    nextSelection = selection!.copyWith(
      extentOffset: selection!.baseOffset,
  } else {
    nextSelection = selection!.copyWith(
      extentOffset: selectedLine.baseOffset,

  _setSelection(nextSelection, cause);