GestureDetector
Catches gestures on its child: tap, double tap and long press, a touch with coordinates, dragging. It draws nothing itself and shows no feedback — unlike InkWell . Use it where the pointer position or dragging matters: a frame drawn with the mouse, a slider, moving a block.
Type:
single-child
GestureDetector onTap:"action:noop"
Text "Tap me"
Parameters
| Parameter | Type | Callback arguments | Description |
|---|---|---|---|
onTap
|
callback | — | Tap |
onDoubleTap
|
callback | — | Double tap |
onLongPress
|
callback | — | Long press |
onTapDown
|
callback with arguments |
localX
,
localY
,
globalX
,
globalY
|
The finger/button went down for a tap |
onTapUp
|
callback with arguments |
localX
,
localY
,
globalX
,
globalY
|
The finger/button went up after a tap |
onPanDown
|
callback with arguments |
localX
,
localY
,
globalX
,
globalY
|
Where the pointer WENT DOWN — before the gesture threshold: the start of a frame drawn with the mouse |
onPanUpdate
|
callback with arguments |
dx
,
dy
,
localX
,
localY
|
Dragging:
dx
/
dy
— the shift since the previous call,
localX
/
localY
— where the pointer is now
|
onPanEnd
|
callback with arguments |
velocityX
,
velocityY
|
Dragging ended; velocity in pixels per second |
onLongPressStart
|
callback with arguments |
localX
,
localY
,
globalX
,
globalY
|
Start of a long press, with the position |
behavior
|
string | — |
How the gesture hit-tests:
opaque
,
translucent
; any other value —
deferToChild
|
key
|
key | — | Widget key |
localX
/
localY
are coordinates inside the
GestureDetector
itself,
globalX
/
globalY
are on the screen.
Callbacks without arguments (
onTap
,
onDoubleTap
,
onLongPress
) are written in the same forms as on
InkWell
: a
{…}
body, an action string or a variable holding a function. Callbacks with arguments are written as
(names){body}
: arguments are passed BY NAME, so the parentheses name those names from the table that are needed —
(dx, dy){…}
,
(localX, localY){…}
.
onPanDown
, not the first
onPanUpdate
onPanUpdate
arrives only after the pointer has moved about 18 px (the gesture threshold). A frame started there is offset by that distance.
onPanDown:(localX, localY)
reports where the pointer went down, before the threshold — that is the real start of the frame.
Examples
Shift while dragging
GestureDetector onPanUpdate:(dx, dy){print(dx)}
ColoredBox color:#4f46e5
SizedBox width:200 height:200
Tap position
GestureDetector onTapDown:(localX, localY){print(localX)}
Text "Tap for position"
A frame drawn with the mouse
@x0:0
@y0:0
@x1:0
@y1:0
GestureDetector behavior:opaque onPanDown:(localX, localY){x0 = localX; y0 = localY; x1 = localX; y1 = localY} onPanUpdate:(dx, dy, localX, localY){x1 = localX; y1 = localY} onPanEnd:(velocityX, velocityY){print(velocityX)}
SizedBox width:400 height:300
Differences between the app and the site
On the site
GestureDetector
only draws its child: its taps and drags never fire. In a
kind:website
project the editor warns about this on the line (
web_no_op
). For a click on the site use
InkWell
or
Button
, for the pointer —
Hovered
/
PointerData
.