Pointer and touch
Elements that hear the pointer (mouse, finger, stylus) below the level of gestures, publish its state into variables or, on the contrary, cut it off. For taps and drags GestureDetector and InkWell are usually enough; here is what they lack:
-
hover and pointer position
—
Hovered,PointerData,MouseRegion; -
raw touch events
—
Listener; -
blocking taps
—
AbsorbPointer,IgnorePointer; -
ready-made gestures
— swipe a row away (
Dismissible), zoom and pan (InteractiveViewer).
Hovered
Writes into a variable whether the pointer is over the child. It draws nothing itself: the hover effect (lift, highlight, shadow) is built from ordinary elements that read the variable.
Type:
wrapper
Hovered hovered:@over
AnimatedContainer duration:180ms curve:easeOut color:{over ? 0xFFEEF2FF : 0xFFFFFFFF}
#padding all 16
Text "Card"
| Parameter | Type | Default | Description |
|---|---|---|---|
hovered
|
@
variable
|
— |
Gets
true
while the pointer is over the child and
false
when it leaves. Declared right here:
hovered:@over
|
A touch screen has no hover: the variable stays
false
, and a person with a phone sees only the resting state. Design that one first.
Row spacing:16
Hovered hovered:@over
AnimatedContainer duration:200ms curve:easeOutCubic width:180 height:100 alignment:center
#decoration color:{over ? 0xFF4F46E5 : 0xFFF1F5F9}
#borderRadius circular {over ? 20 : 8}
Text {over ? "Over the card" : "Hover me"}
#style color:{over ? 0xFFFFFFFF : 0xFF0F172A}
PointerData
Writes into variables where the pointer is relative to the center of the child, in pixels: right and down are positive. When the pointer leaves, both variables return to zero. This is how you make a "magnetic" button that pulls toward the cursor, or a card that tilts after it.
Type:
wrapper
PointerData x:@dx y:@dy
Text $"${round(dx)}, ${round(dy)}"
| Parameter | Type | Default | Description |
|---|---|---|---|
x
|
@
variable
|
— | Horizontal distance from the center, px |
y
|
@
variable
|
— | Vertical distance from the center, px |
-
Pass whatever moves through
`Animated`
.
The number changes on every pointer step and drops to zero as soon as the pointer leaves: a
Transformthat reads it directly jerks and jumps home. The analyzer flags this aspointer_unsmoothed. - The center is measured before the transforms below: a button pulled toward the cursor does not move the point the next step is measured from.
- On a touch screen the variables stay at zero.
PointerData x:@dx y:@dy
Animated of:{dx * 0.25} value:@tx duration:250 curve:easeOutCubic
Animated of:{dy * 0.25} value:@ty duration:250 curve:easeOutCubic
Transform translateX:$tx translateY:$ty
Container width:200 height:96 alignment:center color:0xFF4F46E5
Text "Pulling to the cursor"
#style color:0xFFFFFFFF
MouseRegion
The cursor over the child and mouse events: entered, moving, exited.
Type:
single-child
MouseRegion cursor:click
Text "Over this text the cursor is a hand"
| Parameter | Type | Default | Description |
|---|---|---|---|
cursor
|
string |
defer
|
Cursor:
click
,
basic
,
text
,
forbidden
,
wait
,
grab
,
grabbing
,
move
,
precise
,
help
,
zoomIn
,
zoomOut
,
resizeLeftRight
,
resizeUpDown
,
none
…
defer
— the child decides
|
onEnter
|
callback with arguments | — |
The pointer entered. Arguments:
x
,
y
,
localX
,
localY
|
onHover
|
callback with arguments | — | The pointer moves over the child (without pressing). Same arguments |
onExit
|
callback with arguments | — | The pointer left. Same arguments |
opaque
|
bool | true | Whether the region covers the regions below it |
hitTestBehavior
|
string | — |
How hits are tested:
opaque
,
translucent
|
x
/
y
are coordinates in the window,
localX
/
localY
— inside the
MouseRegion
itself. Arguments are named in the parentheses:
onHover:(localX, localY){…}
.
To just know whether the pointer is over something, `Hovered` is simpler: it works on the site as well.
Listener
Raw pointer events: down, moved, up, cancelled. Unlike GestureDetector , it does not wait for a gesture threshold and does not tell a tap from a drag — every event arrives as it is.
Type:
single-child
@x0:0
Listener behavior:opaque onPointerDown:(x, y){x0 = x}
SizedBox width:300 height:200
| Parameter | Type | Default | Description |
|---|---|---|---|
onPointerDown
|
callback with arguments | — |
The pointer went down. Arguments:
x
,
y
(inside the element),
globalX
,
globalY
(on screen)
|
onPointerMove
|
callback with arguments | — |
The pointer moves while down. Arguments:
x
,
y
,
dx
,
dy
(offset since the previous event)
|
onPointerUp
|
callback with arguments | — |
The pointer went up. Arguments:
x
,
y
|
onPointerCancel
|
callback | — | The system cancelled the event |
behavior
|
string |
deferToChild
|
How hits are tested:
opaque
— catches over the whole area,
translucent
— catches and passes on; otherwise — only where there is a child
|
AbsorbPointer
Blocks taps on everything inside: touches do not reach the children and do not go further either — whatever lies under the element does not get them.
Type:
single-child
@busy:true
AbsorbPointer absorbing:$busy
FilledButton onPressed:{busy = true}
Text "Send"
| Parameter | Type | Default | Description |
|---|---|---|---|
absorbing
|
bool | true |
Whether to block taps;
false
— the element changes nothing
|
IgnorePointer
Makes the child "transparent" to taps: touches pass through it to what lies below. Use it for decorative layers over content (a scrim, a badge, an animation).
Type:
single-child
@taps:0
SizedBox width:240 height:120
Stack
InkWell onTap:{taps = taps + 1}
ColoredBox color:0xFFE2E8F0
IgnorePointer
Center
Text "The label on top does not block the tap"
| Parameter | Type | Default | Description |
|---|---|---|---|
ignoring
|
bool | true |
Whether to let taps through;
false
— the element changes nothing
|
Dismissible
A row that can be swiped aside: delete an email, complete a task. There are two kinds of children: the content (the first child without an
id
) and the background under it that shows while swiping (
id:background
, and for the opposite direction —
id:secondaryBackground
).
Type:
multi-child
@shown:true
Column
Dismissible key:"task-1" direction:endToStart onDismissed:{shown = false} if:$shown
ColoredBox id:background color:0xFFDC2626
Align alignment:centerRight
Padding
#padding only right:16
Icon delete color:0xFFFFFFFF
ListTile
Text id:title "Swipe left to delete"
| Parameter | Type | Default | Description |
|---|---|---|---|
key
|
string | random |
Unique key of the row. Set it explicitly, especially in lists:
key:{item['id']}
|
onDismissed
|
callback | — | The row was swiped away. Remove it from the data here |
direction
|
string |
horizontal
|
Where it can be swiped:
horizontal
,
endToStart
,
startToEnd
,
vertical
,
up
,
down
,
none
|
movementDuration
|
Duration |
200ms
|
How long the row takes to slide away or back into place |
resizeDuration
|
Duration |
300ms
|
How long the space collapses after the swipe |
crossAxisEndOffset
|
double | 0 | How far the row moves sideways across the direction, in fractions of its size |
| Child | Description |
|---|---|
first without
id
|
Row content |
id:background
|
Background under the row while swiping; if only this one is set — for both directions |
id:secondaryBackground
|
Background for the opposite direction (
endToStart
,
up
); works only together with
id:background
|
A swiped row must leave the markup: in
onDismissed
remove the record from the data (or turn off
if:
, as in the example). Flutter does not allow a dismissed
Dismissible
to stay in the tree.
InteractiveViewer
Lets you zoom the child with a pinch (on a computer — with the mouse wheel) and pan it with a finger: a map, a diagram, a photo.
Type:
single-child
InteractiveViewer minScale:0.5 maxScale:4
SizedBox width:600 height:400
ColoredBox color:0xFF4F46E5
| Parameter | Type | Default | Description |
|---|---|---|---|
minScale
|
double | 0.8 | Smallest scale |
maxScale
|
double | 2.5 | Largest scale |
panEnabled
|
bool | true | Whether panning is allowed |
scaleEnabled
|
bool | true | Whether zooming is allowed |
constrained
|
bool | true |
Whether the child gets the size of the area.
false
— the child keeps its own size, larger than the area, and is panned inside it
|
clipBehavior
|
string |
hardEdge
|
Clipping at the edges of the area:
none
,
hardEdge
,
antiAlias
|
alignment
|
alignment | — | Origin of the transform |
Differences between the app and the site
| Element | On the site |
|---|---|
Hovered
|
Works: the variable follows the pointer |
PointerData
|
Works the same way, from the center, returning to zero |
MouseRegion
|
The cursor is drawn (
cursor:
);
onEnter
/
onHover
/
onExit
are not called
|
Listener
|
Only the child is drawn; events do not arrive |
AbsorbPointer
|
Only the child is drawn; taps are not blocked |
IgnorePointer
|
Mouse and touches pass through (
pointer-events: none
), including conditionally. A tappable element inside is still reachable from the keyboard (Tab, Enter)
|
Dismissible
|
Only the content is drawn; it cannot be swiped,
onDismissed
is not called
|
InteractiveViewer
|
Only the child is drawn; no zoom and no pan |
On the site, use
IgnorePointer
rather than
AbsorbPointer
for "cannot be tapped", and
Hovered
rather than
MouseRegion
for hover.
See also
- GestureDetector — taps, long press, dragging
- InkWell — a tap with visible feedback
-
Animated and Ticker
— smoothing
PointerData, loops thatHoveredstops -
Implicit animations
— smooth change of look by
Hovered - Focus and keyboard — hover and focus for the keyboard