timedDragFrom method Null safety

Future<void> timedDragFrom(
  1. Offset startLocation,
  2. Offset offset,
  3. Duration duration,
  4. {int? pointer,
  5. int buttons = kPrimaryButton,
  6. double frequency = 60.0}
)

Attempts a series of PointerEvents to simulate a drag operation in the duration time.

This is the timed version of dragFrom. This may or may not result in a flingFrom or ballistic animation, depending on the speed from offset/duration.

The move events are sent at a given frequency in Hz (or events per second). It defaults to 60Hz.

The movement is linear in time.

See also LiveTestWidgetsFlutterBindingFramePolicy.benchmarkLive for more accurate time control.

Implementation

Future<void> timedDragFrom(
  Offset startLocation,
  Offset offset,
  Duration duration, {
  int? pointer,
  int buttons = kPrimaryButton,
  double frequency = 60.0,
}) {
  assert(frequency > 0);
  final int intervals = duration.inMicroseconds * frequency ~/ 1E6;
  assert(intervals > 1);
  pointer ??= _getNextPointer();
  final List<Duration> timeStamps = <Duration>[
    for (int t = 0; t <= intervals; t += 1)
      duration * t ~/ intervals,
  ];
  final List<Offset> offsets = <Offset>[
    startLocation,
    for (int t = 0; t <= intervals; t += 1)
      startLocation + offset * (t / intervals),
  ];
  final List<PointerEventRecord> records = <PointerEventRecord>[
    PointerEventRecord(Duration.zero, <PointerEvent>[
        PointerAddedEvent(
          position: startLocation,
        ),
        PointerDownEvent(
          position: startLocation,
          pointer: pointer,
          buttons: buttons,
        ),
      ]),
    ...<PointerEventRecord>[
      for(int t = 0; t <= intervals; t += 1)
        PointerEventRecord(timeStamps[t], <PointerEvent>[
          PointerMoveEvent(
            timeStamp: timeStamps[t],
            position: offsets[t+1],
            delta: offsets[t+1] - offsets[t],
            pointer: pointer,
            buttons: buttons,
          ),
        ]),
    ],
    PointerEventRecord(duration, <PointerEvent>[
      PointerUpEvent(
        timeStamp: duration,
        position: offsets.last,
        pointer: pointer,
        // The PointerData received from the engine with
        // change = PointerChange.up, which translates to PointerUpEvent,
        // doesn't provide the button field.
        // buttons: buttons,
      ),
    ]),
  ];
  return TestAsyncUtils.guard<void>(() async {
    await handlePointerEventRecord(records);
  });
}