findCaretOffsetInDocument static method

Offset findCaretOffsetInDocument([
  1. Finder? finder
])

Returns the (x,y) offset for the caret that's currently visible in the document.

Implementation

static Offset findCaretOffsetInDocument([Finder? finder]) {
  final desktopCaretBox = find.byKey(primaryCaretKey).evaluate().singleOrNull?.renderObject as RenderBox?;
  if (desktopCaretBox != null) {
    final globalCaretOffset = desktopCaretBox.localToGlobal(Offset.zero);
    final documentLayout = _findDocumentLayout(finder);
    final globalToDocumentOffset = documentLayout.getGlobalOffsetFromDocumentOffset(Offset.zero);
    return globalCaretOffset - globalToDocumentOffset;
  }

  final androidControls = find.byType(AndroidDocumentTouchEditingControls).evaluate().lastOrNull?.widget
      as AndroidDocumentTouchEditingControls?;
  if (androidControls != null) {
    return androidControls.editingController.caretTop!;
  }

  final iOSControls =
      find.byType(IosDocumentTouchEditingControls).evaluate().lastOrNull?.widget as IosDocumentTouchEditingControls?;
  if (iOSControls != null) {
    return iOSControls.editingController.caretTop!;
  }

  throw Exception('Could not locate caret in document');
}