selectWordsInRange method

void selectWordsInRange ({@required Offset from, Offset to, @required SelectionChangedCause cause })

Selects the set words of a paragraph in a given range of global positions.

The first and last endpoints of the selection will always be at the beginning and end of a word respectively.

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 selectWordsInRange({ @required Offset from, Offset to, @required SelectionChangedCause cause }) {
  assert(cause != null);
  assert(from != null);
  _layoutText(constraints.maxWidth);
  if (onSelectionChanged != null) {
    final TextPosition firstPosition = _textPainter.getPositionForOffset(globalToLocal(from - _paintOffset));
    final TextSelection firstWord = _selectWordAtOffset(firstPosition);
    final TextSelection lastWord = to == null ?
      firstWord : _selectWordAtOffset(_textPainter.getPositionForOffset(globalToLocal(to - _paintOffset)));

    _handlePotentialSelectionChange(
      TextSelection(
        baseOffset: firstWord.base.offset,
        extentOffset: lastWord.extent.offset,
        affinity: firstWord.affinity,
      ),
      cause,
    );
  }
}