selectWordAtOffset method
Implementation
TextSelection selectWordAtOffset(TextPosition position) {
assert(
textLayoutLastMaxWidth == constraints.maxWidth &&
textLayoutLastMinWidth == constraints.minWidth,
'Last width ($textLayoutLastMinWidth, $textLayoutLastMaxWidth) not the same as max width constraint (${constraints.minWidth}, ${constraints.maxWidth}).');
final TextRange word = textPainter.getWordBoundary(position);
TextSelection? selection;
// When long-pressing past the end of the text, we want a collapsed cursor.
if (position.offset >= word.end) {
selection = TextSelection.fromPosition(position);
}
// If text is obscured, the entire sentence should be treated as one word.
else if (obscureText) {
selection = TextSelection(baseOffset: 0, extentOffset: plainText.length);
}
// If the word is a space, on iOS try to select the previous word instead.
// On Android try to select the previous word instead only if the text is read only.
else if (text?.toPlainText() != null &&
isWhitespace(text!.toPlainText().codeUnitAt(position.offset)) &&
position.offset > 0) {
final TextRange? previousWord = _getPreviousWord(word.start);
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
selection = TextSelection(
baseOffset: previousWord!.start,
extentOffset: position.offset,
);
break;
case TargetPlatform.android:
if (readOnly) {
selection = TextSelection(
baseOffset: previousWord!.start,
extentOffset: position.offset,
);
}
break;
case TargetPlatform.fuchsia:
case TargetPlatform.macOS:
case TargetPlatform.linux:
case TargetPlatform.windows:
break;
}
}
selection ??= TextSelection(baseOffset: word.start, extentOffset: word.end);
/// zmt
return hasSpecialInlineSpanBase
? convertTextPainterSelectionToTextInputSelection(text!, selection,
selectWord: true)
: selection;
}