Flutter GestureDetector Example – Flutter Tutorial



Flutter’s GestureDetector is a powerful widget that allows you to recognize various types of user interactions with your app. With GestureDetector, you can detect taps, drags, scrolls, and many other user gestures.

To use GestureDetector, you first need to wrap it around the widget that you want to make interactive. For example, if you want to detect taps on an image, you can wrap GestureDetector around the Image widget like this:

GestureDetector(
  onTap: () {
    // Do something when the user taps the image
  },
  child: Image.asset('assets/my_image.png'),
);

In this example, we’ve specified an onTap callback that will be called whenever the user taps on the image. You can also specify other callbacks to detect other types of gestures, like onDoubleTap, onLongPress, onPanStart, onScaleUpdate, and many others.

One of the powerful features of GestureDetector is that it allows you to recognize complex gestures that involve multiple touch points. For example, you can detect a user pinch gesture (i.e. when the user uses two fingers to zoom in or out) using the onScaleUpdate callback:

GestureDetector(
  onScaleUpdate: (ScaleUpdateDetails details) {
    // Do something when the user pinches the widget
  },
  child: MyZoomableWidget(),
);

In this example, we’ve specified an onScaleUpdate callback that will be called whenever the user performs a pinch gesture on MyZoomableWidget.

Another, Flutter GestureDetector Example:

GestureDetector(
      onTap: () {
        setState(() {
          clicked = !clicked; // here must need setState()
        });
      },
      child: Center(child: clicked ? Text("First Text") : Text("Second Text")),
    );

Flutter GestureDetector Widget

GestureDetector

tutorial source code example:

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(body: HomePage()),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool clicked = false;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          clicked = !clicked; // here must need setState()
        });
      },
      child: Center(child: clicked ? Text("First Text") : Text("Second Text")),
    );
  }
}
  • How to Conduct YouTube Keyword Research Easily?
    Introduction YouTube is the second largest search engine in the world, making it a valuable platform for content creators and businesses alike. To maximize your visibility and reach on YouTube, it is crucial to conduct proper keyword research. By identifying the right keywords, you can optimize your videos for search and increase your chances of …

    Read more …

  • How to upgrade the Dart SDK version in flutter
    If you’re a Flutter developer, you know how important it is to stay up-to-date with the latest version of the Dart SDK. Upgrading the Dart SDK ensures that you have access to the latest features, bug fixes, and performance improvements. Step 1: Check Your Current Dart SDK Version Before you begin the upgrade process, it’s …

    Read more …

  • Why Do Advertisers Use Testimonials?
    In the ever-competitive business landscape, nurturing strong connections with your customers remains paramount for enhancing your credibility and positioning yourself as an industry authority. Establishing credibility is the bedrock upon which trust is built, ultimately translating into increased sales. To bolster your company’s credibility, integrating testimonial advertising into your marketing strategy is essential. So, what …

    Read more …

  • How To Earn $5 Fast Method!
    Looking to boost your income by $25 in less than an hour? This isn’t the start of a clichéd sales pitch; it’s about discovering several quick and simple ways to earn $5 online within minutes. For instance, you can make five bucks by completing tasks such as taking brief surveys, participating in short website usability …

    Read more …

  • What Does BMF Mean On TikTok? Latest Social Media Acronyms
    In the fast-paced world of social media, acronyms are the language of choice, enabling users to communicate swiftly without the need for lengthy phrases. While classics like LOL and OMG are still in use, the younger generation on TikTok has introduced a new acronym into the mix: BMF. So, what exactly does BMF mean, and …

    Read more …

Read also, Flutter Hero Widget Example. and Flutter Transform widget?

Overall, GestureDetector is a very flexible and powerful widget that can help you create highly interactive and engaging user interfaces in your Flutter app.

Leave a Comment