text
stringlengths
1
474
(and subsequent events for that pointer) are then dispatched
to the innermost widget found by the hit test.
From there, the events bubble up the tree and are dispatched
to all the widgets on the path from the innermost
widget to the root of the tree. There is no mechanism for
canceling or stopping pointer events from being dispatched further.To listen to pointer events directly from the widgets layer, use a
Listener widget. However, generally,
consider using gestures (as discussed below) instead.<topic_end>
<topic_start>
Gestures
Gestures represent semantic actions (for example, tap, drag,
and scale) that are recognized from multiple individual pointer
events, potentially even multiple individual pointers.
Gestures can dispatch multiple events, corresponding to the
lifecycle of the gesture (for example, drag start,
drag update, and drag end):TapDouble tapLong pressVertical dragHorizontal dragPan<topic_end>
<topic_start>
Adding gesture detection to widgets
To listen to gestures from the widgets layer,
use a GestureDetector.info Note
To learn more, watch this short
Widget of the Week video on the GestureDetector widget:If you’re using Material Components,
many of those widgets already respond to taps or gestures.
For example, IconButton and TextButton
respond to presses (taps), and ListView
responds to swipes to trigger scrolling.
If you aren’t using those widgets, but you want the
“ink splash” effect on a tap, you can use InkWell.<topic_end>
<topic_start>
Gesture disambiguation
At a given location on screen,
there might be multiple gesture detectors.
For example:All of these gesture detectors listen to the stream
of pointer events as they flow past and attempt to recognize
specific gestures. The GestureDetector widget decides
which gestures to attempt to recognize based on which of its
callbacks are non-null.When there is more than one gesture recognizer for a given
pointer on the screen, the framework disambiguates which
gesture the user intends by having each recognizer join
the gesture arena. The gesture arena determines which
gesture wins using the following rules:At any time, a recognizer can eliminate itself and leave the
arena. If there’s only one recognizer left in the arena,
that recognizer wins.At any time, a recognizer can declare itself the winner,
causing all of the remaining recognizers to lose.For example, when disambiguating horizontal and vertical dragging,
both recognizers enter the arena when they receive the pointer
down event. The recognizers observe the pointer move events.
If the user moves the pointer more than a certain number of
logical pixels horizontally, the horizontal recognizer declares
the win and the gesture is interpreted as a horizontal drag.
Similarly, if the user moves more than a certain number of logical
pixels vertically, the vertical recognizer declares itself the winner.The gesture arena is beneficial when there is only a horizontal
(or vertical) drag recognizer. In that case, there is only one
recognizer in the arena and the horizontal drag is recognized
immediately, which means the first pixel of horizontal movement
can be treated as a drag and the user won’t need to wait for
further gesture disambiguation.
<topic_end>
<topic_start>Handle taps
You not only want to display information to users,
you want users to interact with your app.
Use the GestureDetector widget to respond
to fundamental actions, such as tapping and dragging.info Note
To learn more, watch this short Widget of the Week video on the GestureDetector widget:This recipe shows how to make a custom button that shows
a snackbar when tapped with the following steps:
<code_start>// The GestureDetector wraps the button.
GestureDetector(
// When the child is tapped, show a snackbar.
onTap: () {
const snackBar = SnackBar(content: Text('Tap'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
// The custom button
child: Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.lightBlue,
borderRadius: BorderRadius.circular(8),
),
child: const Text('My Button'),
),
)<code_end>
<topic_end>
<topic_start>
Notes
<topic_end>
<topic_start>
Interactive example
<code_start>import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const title = 'Gesture Demo';
return const MaterialApp(
title: title,
home: MyHomePage(title: title),
);
}
}