backspace method

bool backspace({
  1. int length = 1,
})

length want to delete char count. If there is a selection, just delete selection and ignore length

Implementation

bool backspace({int length = 1}) {
  final text = editingValue.text;
  final textSelection = editingValue.selection;
  final selectionLength = textSelection.end - textSelection.start;

  // There is a selection.
  if (selectionLength > 0) {
    final newText =
        text.replaceRange(textSelection.start, textSelection.end, '');
    editingValue = editingValue.copyWith(
        text: newText,
        selection: textSelection.copyWith(
          baseOffset: textSelection.start,
          extentOffset: textSelection.start,
        ));
    // Request the attached client to update accordingly.
    widget.embedTextInput.updateEditingValue(editingValue);
    return true;
  }

  // The cursor is at the beginning.
  if (textSelection.start == 0) {
    return true;
  }

  // Delete the previous character
  var newStart = textSelection.start - length;
  if (newStart < 0) {
    newStart = 0;
  }
  final newEnd = textSelection.start;
  final newText = text.replaceRange(newStart, newEnd, '');
  editingValue = editingValue.copyWith(
      text: newText,
      selection: textSelection.copyWith(
        baseOffset: newStart,
        extentOffset: newStart,
      ));
  // Request the attached client to update accordingly.
  widget.embedTextInput.updateEditingValue(editingValue);
  return true;
}