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.container.documentOffset,
  );

  var 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 testPosition = TextPosition(offset: 0);
      final testOffset = sibling.getOffsetForCaret(testPosition);
      final finalOffset = Offset(caretOffset.dx, testOffset.dy);
      final siblingPosition = sibling.getPositionForOffset(finalOffset);
      newPosition = TextPosition(
        offset: sibling.container.documentOffset + siblingPosition.offset,
      );
    }
  } else {
    newPosition = TextPosition(
      offset: child.container.documentOffset + newPosition.offset,
    );
  }
  return newPosition;
}