getSpanForPosition method

TextSpan getSpanForPosition (TextPosition position)

Returns the text span that contains the given position in the text.

Implementation

TextSpan getSpanForPosition(TextPosition position) {
  assert(debugAssertIsValid());
  final TextAffinity affinity = position.affinity;
  final int targetOffset = position.offset;
  int offset = 0;
  TextSpan result;
  visitTextSpan((TextSpan span) {
    assert(result == null);
    final int endOffset = offset + span.text.length;
    if (targetOffset == offset && affinity == TextAffinity.downstream ||
        targetOffset > offset && targetOffset < endOffset ||
        targetOffset == endOffset && affinity == TextAffinity.upstream) {
      result = span;
      return false;
    }
    offset = endOffset;
    return true;
  });
  return result;
}