getPositionAbove method Null safety

  1. @override
TextPosition? getPositionAbove(
  1. TextPosition position
)
override

Returns the position within the text which is on the line above the given position.

The position parameter must be relative to the node content.

Primarily used with multi-line or soft-wrapping text.

Can return null which indicates that the position is at the topmost line in the text already.

Implementation

@override
TextPosition? getPositionAbove(TextPosition position) {
  assert(position.offset < getContainer().length);

  final child = childAtPosition(position);
  final childLocalPosition =
      TextPosition(offset: position.offset - child.getContainer().offset);
  final result = child.getPositionAbove(childLocalPosition);
  if (result != null) {
    return TextPosition(offset: result.offset + child.getContainer().offset);
  }

  final sibling = childBefore(child);
  if (sibling == null) {
    return null;
  }

  final caretOffset = child.getOffsetForCaret(childLocalPosition);
  final testPosition =
      TextPosition(offset: sibling.getContainer().length - 1);
  final testOffset = sibling.getOffsetForCaret(testPosition);
  final finalOffset = Offset(caretOffset.dx, testOffset.dy);
  return TextPosition(
      offset: sibling.getContainer().offset +
          sibling.getPositionForOffset(finalOffset).offset);
}