GestureDetector Widget and Attributes

GestureDetector Widget and Attributes

ยท

2 min read

Flutter's GestureDetector widget is a versatile tool that empowers developers to capture a wide range of gestures, from taps and double taps to swipes and long presses. This comprehensive guide will walk you through the GestureDetector widget and its essential attributes, accompanied by a full example to illustrate its practical application.

What is GestureDetector?

GestureDetector is a Flutter widget that provides a flexible and intuitive way to handle user gestures within your application. It can recognize various touch gestures and trigger corresponding callbacks, allowing you to create interactive and responsive user interfaces.

Attributes of GestureDetector:

1. onTap:

  • The onTap attribute defines a callback function that is triggered when the user taps the widget.

2. onDoubleTap:

  • The onDoubleTap attribute specifies a callback function that is invoked when the user quickly taps the widget twice.

3. onLongPress:

  • The onLongPress attribute defines a callback function triggered when the user presses and holds the widget for a certain duration.

4. onPanUpdate:

  • The onPanUpdate attribute is a callback function invoked when the user pans (drags) their finger across the widget.

5. child:

  • The child attribute is where you define the widget that responds to the gestures. It can be a simple widget or a complex subtree.

Example:

Let's create a simple example where a GestureDetector is used to detect taps, double taps, and long presses on a container, changing its color accordingly.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GestureDetector Example'),
      ),
      body: Center(
        child: GestureDetector(
          onTap: () {
            print('Container tapped!');
          },
          onDoubleTap: () {
            print('Container double tapped!');
          },
          onLongPress: () {
            print('Container long pressed!');
          },
          child: Container(
            width: 200.0,
            height: 200.0,
            color: Colors.blue,
            child: Center(
              child: Text(
                'Tap me!',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

In this example:

  • The GestureDetector widget is used to wrap a container, making it responsive to various gestures.

  • Callback functions (onTap, onDoubleTap, onLongPress) are defined to print messages when the corresponding gestures are detected.

  • The container changes its color to blue and displays text, inviting the user to interact.

With the GestureDetector widget, you can easily add interactivity to your Flutter applications, providing a more engaging and dynamic user experience.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย