selectWordEdge method

void selectWordEdge ({@required SelectionChangedCause cause })

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

This method is mainly used to translate 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 }) {
  assert(cause != null);
  _layoutText(constraints.maxWidth);
  assert(_lastTapDownPosition != null);
  if (onSelectionChanged != null) {
    final TextPosition position = _textPainter.getPositionForOffset(globalToLocal(_lastTapDownPosition - _paintOffset));
    final TextRange word = _textPainter.getWordBoundary(position);
    if (position.offset - word.start <= 1) {
      _handlePotentialSelectionChange(
        TextSelection.collapsed(offset: word.start, affinity: TextAffinity.downstream),
        cause,
      );
    } else {
      _handlePotentialSelectionChange(
        TextSelection.collapsed(offset: word.end, affinity: TextAffinity.upstream),
        cause,
      );
    }
  }
}