getTextBoundaryAt method

  1. @override
TextRange getTextBoundaryAt(
  1. int position
)
override

Returns the text boundary range that encloses the input position.

The returned TextRange may contain -1, which indicates no boundaries can be found in that direction.

Implementation

@override
TextRange getTextBoundaryAt(int position) {
  if (position < 0) {
    return TextRange(start: -1, end: getTrailingTextBoundaryAt(position) ?? -1);
  } else if (position >= _text.length) {
    return TextRange(start: getLeadingTextBoundaryAt(position) ?? -1, end: -1);
  }
  final CharacterRange rangeAtPosition = CharacterRange.at(_text, position);
  return rangeAtPosition.isNotEmpty
    ? TextRange(start: rangeAtPosition.stringBeforeLength, end: rangeAtPosition.stringBeforeLength + rangeAtPosition.current.length)
    // rangeAtPosition is empty means `position` is a grapheme boundary.
    : TextRange(start: rangeAtPosition.stringBeforeLength, end: getTrailingTextBoundaryAt(position) ?? -1);
}