Gesture detection and touch event handling

Domains: Flutter

To listen for and respond to gestures, Flutter supports taps, drags, and scaling. The gesture system in Flutter has two separate layers. The first layer includes raw pointer events, which describe the location and movement of pointers, (such as touches, mice, and styli movements), across the screen. The second layer includes gestures, which describe semantic actions that consist of one or more pointer movements.

How do I add a click or press listeners to a widget?

In React Native, listeners are added to components using PanResponder or the Touchable components.

// React Native
  onPress={() => {
  onLongPress={() => {
    console.log('Long Press');
  <Text>Tap or Long Press</Text>

For more complex gestures and combining several touches into a single gesture, PanResponder is used.

// React Native
class App extends Component {

  componentWillMount() {
    this._panResponder = PanResponder.create({
      onMoveShouldSetPanResponder: (event, gestureState) =>
      onPanResponderMove: (event, gestureState) => true,
      onPanResponderRelease: (event, gestureState) => {
        const drag = getDirection(gestureState);
      onPanResponderTerminationRequest: (event, gestureState) => true

  render() {
    return (
      <View style={styles.container} {...this._panResponder.panHandlers}>
        <View style={}>
          <Text>Swipe Horizontally or Vertically</Text>

In Flutter, to add a click (or press) listener to a widget, use a button or a touchable widget that has an onPress: field. Or, add gesture detection to any widget by wrapping it in a GestureDetector.

// Flutter
  child: Scaffold(
    appBar: AppBar(
      title: Text('Gestures'),
    body: Center(
      child: Column(
        children: <Widget>[
          Text('Tap, Long Press, Swipe Horizontally or Vertically '),
  onTap: () {
  onLongPress: () {
    print('Long Pressed');
  onVerticalDragEnd: (DragEndDetails value) {
    print('Swiped Vertically');
  onHorizontalDragEnd: (DragEndDetails value) {
    print('Swiped Horizontally');

For more information, including a list of Flutter GestureDetector callbacks, see the GestureDetector class.



Similar pages

Page structure



Gesture detection and touch event handling