getPositionForOffset method

  1. @override
TextPosition getPositionForOffset(
  1. Offset offset
)
override

Returns the local coordinates of the endpoints of the given selection.

If the selection is collapsed (and therefore occupies a single point), the returned list is of length one. Otherwise, the selection is not collapsed and the returned list is of length two. In this case, however, the two points might actually be co-located (e.g., because of a bidirectional selection that contains some text but whose ends meet in the middle).

Implementation

@override
TextPosition getPositionForOffset(Offset offset) {
  final local = globalToLocal(offset);
  final child = childAtOffset(local);

  final parentData = child.parentData as BoxParentData;
  final localOffset = local - parentData.offset;
  final localPosition = child.getPositionForOffset(localOffset);
  return TextPosition(
    offset: localPosition.offset + child.container.offset,
    affinity: localPosition.affinity,
  );
}