selectWordsInRange method

void selectWordsInRange(
  1. {required Offset from,
  2. Offset? to,
  3. required SelectionChangedCause cause}

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

The set of words selected are not strictly bounded by the 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.


void selectWordsInRange({ required Offset from, Offset? to, required SelectionChangedCause cause }) {
  final TextPosition fromPosition = _textPainter.getPositionForOffset(globalToLocal(from - _paintOffset));
  final TextSelection fromWord = getWordAtOffset(fromPosition);
  final TextPosition toPosition = to == null ? fromPosition : _textPainter.getPositionForOffset(globalToLocal(to - _paintOffset));
  final TextSelection toWord = toPosition == fromPosition ? fromWord : getWordAtOffset(toPosition);
  final bool isFromWordBeforeToWord = fromWord.start < toWord.end;

      baseOffset: isFromWordBeforeToWord ? fromWord.base.offset : fromWord.extent.offset,
      extentOffset: isFromWordBeforeToWord ? toWord.extent.offset : toWord.base.offset,
      affinity: fromWord.affinity,