getTextPositionBelow method

  1. @override
TextPosition getTextPositionBelow(
  1. TextPosition position
)
override

Returns the TextPosition below the given offset into the text.

If the offset is already on the last line, the offset of the last character will be returned.

Implementation

@override
TextPosition getTextPositionBelow(TextPosition position) {
  final child = childAtPosition(position);
  final localPosition =
      TextPosition(offset: position.offset - child.node.documentOffset);

  TextPosition? newPosition = child.getPositionBelow(localPosition);

  if (newPosition == null) {
    // There was no text above in the current child, check the direct
    // sibling.
    final sibling = childAfter(child);
    if (sibling == null) {
      // reached beginning of the document, move to the
      // last character
      newPosition = TextPosition(offset: _document.length - 1);
    } else {
      final caretOffset = child.getOffsetForCaret(localPosition);
      const textPosition = TextPosition(offset: 0);
      final textOffset = sibling.getOffsetForCaret(textPosition);
      final finalOffset = Offset(caretOffset.dx, textOffset.dy);
      final siblingPosition = sibling.getPositionForOffset(finalOffset);
      newPosition = TextPosition(
          offset: sibling.node.documentOffset + siblingPosition.offset);
    }
  } else {
    newPosition =
        TextPosition(offset: child.node.documentOffset + newPosition.offset);
  }
  return newPosition;
}