selectWordEdge method

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

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

@override
void selectWordEdge({required SelectionChangedCause cause}) {
  // _layoutText(minWidth: constraints.minWidth, maxWidth: constraints.maxWidth);
  assert(_lastTapDownPosition != null);
  if (onSelectionChanged == null) {
    return;
  }
  final position = getPositionForOffset(_lastTapDownPosition!);
  final child = childAtPosition(position);
  final nodeOffset = child.node.offset;
  final localPosition = TextPosition(
    offset: position.offset - nodeOffset,
    affinity: position.affinity,
  );
  final localWord = child.getWordBoundary(localPosition);
  final word = TextRange(
    start: localWord.start + nodeOffset,
    end: localWord.end + nodeOffset,
  );
  if (position.offset - word.start <= 1) {
    _handleSelectionChange(
      TextSelection.collapsed(
          offset: word.start, affinity: TextAffinity.downstream),
      cause,
    );
  } else {
    _handleSelectionChange(
      TextSelection.collapsed(
          offset: word.end, affinity: TextAffinity.upstream),
      cause,
    );
  }
}