getTextPositionAbove method

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

Returns the TextPosition above the given offset into the text.

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

Implementation

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

  var newPosition = child.getPositionAbove(localPosition);

  if (newPosition == null) {
    // There was no text above in the current child, check the direct
    // sibling.
    final sibling = childBefore(child);
    if (sibling == null) {
      // reached beginning of the document, move to the
      // first character
      newPosition = const TextPosition(offset: 0);
    } else {
      final caretOffset = child.getOffsetForCaret(localPosition);
      final testPosition = TextPosition(offset: sibling.container.length - 1);
      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;
}