fling method Null safety
Attempts a fling gesture starting from the center of the given widget, moving the given distance, reaching the given speed.
The warnIfMissed
argument, if true (the default), causes a warning to be
displayed on the console if the specified Finder indicates a widget and
location that, were a pointer event to be sent to that location, would not
actually send any events to the widget (e.g. because the widget is
obscured, or the location is off-screen, or the widget is transparent to
pointer events).
Set the argument to false to silence that warning if you intend to not actually hit the specified element.
This can pump frames.
Exactly 50 pointer events are synthesized.
The speed
is in pixels per second in the direction given by offset
.
The offset
and speed
control the interval between each pointer event.
For example, if the offset
is 200 pixels down, and the speed
is 800
pixels per second, the pointer events will be sent for each increment
of 4 pixels (200/50), over 250ms (200/800), meaning events will be sent
every 1.25ms (250/200).
To make tests more realistic, frames may be pumped during this time (using
calls to pump). If the total duration is longer than frameInterval
,
then one frame is pumped each time that amount of time elapses while
sending events, or each time an event is synthesized, whichever is rarer.
See LiveTestWidgetsFlutterBindingFramePolicy.benchmarkLive if the method is used in a live environment and accurate time control is important.
The initialOffset
argument, if non-zero, causes the pointer to first
apply that offset, then pump a delay of initialOffsetDelay
. This can be
used to simulate a drag followed by a fling, including dragging in the
opposite direction of the fling (e.g. dragging 200 pixels to the right,
then fling to the left over 200 pixels, ending at the exact point that the
drag started).
A fling is essentially a drag that ends at a particular speed. If you just want to drag and end without a fling, use drag.
Implementation
Future<void> fling(
Finder finder,
Offset offset,
double speed, {
int? pointer,
int buttons = kPrimaryButton,
Duration frameInterval = const Duration(milliseconds: 16),
Offset initialOffset = Offset.zero,
Duration initialOffsetDelay = const Duration(seconds: 1),
bool warnIfMissed = true,
}) {
return flingFrom(
getCenter(finder, warnIfMissed: warnIfMissed, callee: 'fling'),
offset,
speed,
pointer: pointer,
buttons: buttons,
frameInterval: frameInterval,
initialOffset: initialOffset,
initialOffsetDelay: initialOffsetDelay,
);
}