selectWordEdge method

void selectWordEdge({
  1. required SelectionChangedCause cause,
})

Move the selection to the beginning or end of a word.

This method is mainly used to translatef user inputs in global positions into a TextSelection. When used in conjunction with a EditableText, the selection change is fed back into TextEditingController.selection.

If you have a TextEditingController, it's generally easier to programmatically manipulate its value or selection directly.

Implementation

void selectWordEdge({required SelectionChangedCause cause}) {
  layoutText(minWidth: constraints.minWidth, maxWidth: constraints.maxWidth);
  assert(lastTapDownPosition != null);
  if (onSelectionChanged == null) {
    return;
  }
  final TextPosition position = textPainter.getPositionForOffset(
      globalToLocal(lastTapDownPosition! - paintOffset));
  final TextRange word = textPainter.getWordBoundary(position);
  final TextRange lineBoundary = textPainter.getLineBoundary(position);
  final bool endOfLine = lineBoundary.end == position.offset;
  TextSelection selection;

  ///zmt
  if (position.offset - word.start <= 1) {
    selection = TextSelection.collapsed(
        offset: word.start,
        affinity: endOfLine ? position.affinity : TextAffinity.downstream);
  } else {
    selection = TextSelection.collapsed(
        offset: word.end,
        affinity: endOfLine ? position.affinity : TextAffinity.upstream);
  }

  selection = hasSpecialInlineSpanBase
      ? convertTextPainterSelectionToTextInputSelection(text!, selection)
      : selection;

  _handleSelectionChange(selection, cause);
}