startDocumentDragFromPosition method

Future<TestGesture> startDocumentDragFromPosition({
  1. required DocumentPosition from,
  2. Alignment startAlignmentWithinPosition = Alignment.center,
  3. Finder? superEditorFinder,
  4. PointerDeviceKind deviceKind = PointerDeviceKind.mouse,
})

Simulates a user drag that begins at the from DocumentPosition and returns the simulated gesture for further control.

Make sure to remove the pointer when you're done with the TestGesture.

Implementation

Future<TestGesture> startDocumentDragFromPosition({
  required DocumentPosition from,
  Alignment startAlignmentWithinPosition = Alignment.center,
  Finder? superEditorFinder,
  PointerDeviceKind deviceKind = PointerDeviceKind.mouse,
}) async {
  final documentLayout = _findDocumentLayout(superEditorFinder);

  // Find the global offset to start the drag gesture.
  Rect dragStartRect = documentLayout.getRectForPosition(from)!.deflate(1);
  final globalDocTopLeft = documentLayout.getGlobalOffsetFromDocumentOffset(Offset.zero);
  dragStartRect = dragStartRect.translate(globalDocTopLeft.dx, globalDocTopLeft.dy);
  final dragStartOffset = startAlignmentWithinPosition.withinRect(dragStartRect);

  // Simulate the drag.
  final gesture = await startGesture(dragStartOffset, kind: deviceKind);
  await pump();

  // Move a tiny amount to start the pan gesture.
  await gesture.moveBy(const Offset(2, 2));
  await pump();

  return gesture;
}