avatarDevTechie

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3202

Abstract

e onTap (see below example) callback will not take any parameters, but onTapUp and onTapDown callback will take relevant parameters. It is not possible in this article to list the details of parameters of all possible callback functions. Hence, as an example consider the signature of onTapUp and onTapDown callbacks.</p><div id="c7cc"><pre>onTapUp: (TapUpDetails tapUpDetails) { <span class="hljs-comment">// Write the code-block for actions here when onTapUp event fires</span> }</pre></div><div id="cc42"><pre>onTapDown: (TapDownDetails tapDownDetails) { <span class="hljs-comment">// Write the code-block for actions here when onTapDown event fires</span> }</pre></div><p id="7841">You can see that <i>onTapUp</i> takes a parameter of type <code><i>TapUpDetails</i></code>, but <i>onTapDown </i>takes a parameter of type <code><i>TapDownDetails</i></code></p><h1 id="3534">Example (demo) app</h1><p id="ef47">The below code-snippet shows the code of the example app.</p><div id="c691"><pre><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;</pre></div><div id="b4de"><pre><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span>()</span> { runApp(<span class="hljs-function"><span class="hljs-keyword">const</span> <span class="hljs-title">MyApp</span>())</span>; }</pre></div><div id="1b58"><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{ const <span class="hljs-type">MyApp</span>({<span class="hljs-keyword">super</span>.key});</pre></div><div id="7b87"><pre> <span class="hljs-comment">// This widget is the root of your application.</span> <span class="hljs-variable">@override</span> Widget <span class="hljs-built_in">build</span>(BuildContext context) { <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">MaterialApp</span>( <span class="hljs-attribute">title</span>: <span class="hljs-string">'Gesture Detector'</span>, <span class="hljs-attribute">theme</span>: <span class="hljs-built_in">ThemeData</span>( <span class="hljs-comment">// This is the theme of your application.</span> <span class="hljs-attribute">primarySwatch</span>: Colors.blue, ), <span class="hljs-attribute">home</span>: const <span class="hljs-built_in">AppHomePage</span>(<span class="hljs-attribute">title</span>: <span class="hljs-string">'Gesture Detector Demo'</span>), ); } }</pre></div><div id="9881"><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppHomePage</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{ const <span class="hljs-type">AppHomePage</span>({<span class="hljs-keyword">super</span>.key, required <span class="hljs-keyword">this</span>.title}); <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> title;</pre></div><div id="b1f4"><pre> <span class="hljs-variable">@override</span> Widget <span

Options

class="hljs-built_in">build</span>(BuildContext context) { <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">Scaffold</span>( <span class="hljs-attribute">appBar</span>: <span class="hljs-built_in">AppBar</span>( <span class="hljs-attribute">title</span>: Text (title), ), <span class="hljs-attribute">body</span>: <span class="hljs-built_in">GestureDetector</span>( <span class="hljs-attribute">child</span>: <span class="hljs-built_in">Container</span>( <span class="hljs-attribute">color</span>: Colors.green, <span class="hljs-attribute">height</span>: <span class="hljs-number">200</span>, <span class="hljs-attribute">width</span>: <span class="hljs-number">200</span>, <span class="hljs-attribute">margin</span>: const EdgeInsets.<span class="hljs-built_in">all</span>(<span class="hljs-number">50</span>), ), <span class="hljs-attribute">onTap</span>: () { print (<span class="hljs-string">"Container Tapped"</span>); Feedback.<span class="hljs-built_in">forTap</span>(context); <span class="hljs-comment">// plays small sound when Tapped</span> }, <span class="hljs-attribute">onLongPress</span>: () { print (<span class="hljs-string">"Container long pressed"</span>); Feedback.<span class="hljs-built_in">forLongPress</span>(context); <span class="hljs-comment">// plays small sound when long-pressed</span> }, )); } }</pre></div><h2 id="6fa0">Code explanation</h2><p id="e114">I created a container which has a green background, a width and height of 200 pixels. The container is placed at a margin of 50 pixels in each direction within the parent (GestureDetector) widget.</p><p id="e67e">The container is wrapped inside a GestureDectetor widget, i.e., the Container widget is made a child of GestureDectetor widget for gesture detection.</p><p id="348d">onTap() property of GestureDetecor widget is passed a function which is called back when user taps the Container. For demonstration purposes, I have just printed a message<i> “Container Tapped”</i> in the console window (see below video). The statement <code>Feedback.forTap(context)</code>plays a small sound as feedback of a tap gesture to the user.</p><p id="a38a" type="7">In production apps, some action such as displaying a new screen (page) of the app is initiated within the callback function.</p><p id="26a3">The preview video is shown below.</p><figure id="b0d2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_k2YNgFr-6dQdhe-7rpq9A.gif"><figcaption></figcaption></figure><h1 id="8ae1">Recapitulate</h1><p id="7c99">The article has demonstrated gesture detection using the <code>GestureDetector</code> widget. Another article will discuss what happens when — (1) <code>GestureDetector</code>has no child widget and (2) has a non-visible child widget.</p><p id="f398" type="7">With that we have reached the end of this article. Thank you once again for reading. Don’t forget to 👏 and follow 😍. Also subscribe our newsletter at https://www.devtechie.com</p></article></body>

Responding to Gestures Using GestureDetector widget in Flutter

Responding to Gestures Using GestureDetector widget in Flutter

This article presents an example of how to respond to gestures in Flutter apps using a specific widget — the GestureDetector. In Flutter, the GestureDetector widget is not the only way for gesture detection and response. However, this article presents an example using the GestureDetector widget for greeting gestures in Flutter apps.

Introduction

Applications must respond to gestures, i.e., user reactions such as touch and drag. In fact, applications initiate some action as a response to gestures such as tap, long-press etc. Acknowledging gestures is what makes an app interactive. Flutter has various widgets to make an app interactive, i.e., to respond to gestures on touch-based devices.

In this article, I will present an example of detecting, receiving and responding to gestures using the GestureDetector widget.

The GestureDetector widget is designed for gesture detection, hence it enables many widgets such as Container, Row, Column, Text, etc. to detect gestures since these widgets do not have built-in capability of detecting gestures.

As the GestureDetector widget is used for gesture detection, it is not a visible widget.

The GestureDetector widget is an effortless way to detect and respond to various gestures. What you have to do is just wrap the widget on which you would like to receive gestures inside the GestureDetector widget. For example, if you want to greet gestures on a Text widget, then make the Text widget a child of the GestureDetector widget.

The GestureDetector widget is a stateless widget and receives different callback functions to respond to different gestures. For example, its onTap property takes a callback function to respond to Tap gestures of its child widget. This means, the callback function’s code block will be executed when the user taps on the child widget. GestureDetector widget.

The properties of GestureDetector

child

This property takes a widget.

The GestureDetector is a single-child widget, i.e., it can only have one child widget. However, if that only child is a multi-child widget such as Row, Column, and Stack, etc., it can layout multiple widgets

onTap

This property and many more such as onTapDown, onTapUp, onTapCancel, onDoubleTap, onLongPress, onLongPressStart, onLongPressEnd, onLongPressUp, onHorizontalDragStart, onHorizontalDragUpdate, onHorizontalDragEnd, onVerticalDragStart, onVerticalDragUpdate, onVerticalDragEnd etc.;takes a callback function to be executed when onTap (or any other) event occurs.

However, all of them are not the same, for example onTap (see below example) callback will not take any parameters, but onTapUp and onTapDown callback will take relevant parameters. It is not possible in this article to list the details of parameters of all possible callback functions. Hence, as an example consider the signature of onTapUp and onTapDown callbacks.

onTapUp: (TapUpDetails tapUpDetails) {
    // Write the code-block for actions here when onTapUp event fires
  }
onTapDown: (TapDownDetails tapDownDetails) {
    // Write the code-block for actions here when onTapDown event fires
  }

You can see that onTapUp takes a parameter of type TapUpDetails, but onTapDown takes a parameter of type TapDownDetails

Example (demo) app

The below code-snippet shows the code of the example app.

import 'package:flutter/material.dart';
void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Gesture Detector',
      theme: ThemeData(
        // This is the theme of your application.
        primarySwatch: Colors.blue,
      ),
      home: const AppHomePage(title: 'Gesture Detector Demo'),
    );
  }
}
class AppHomePage extends StatelessWidget {
  const AppHomePage({super.key, required this.title});
  final String title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text (title),
        ),
        body: GestureDetector(
          child: Container(
            color: Colors.green,
            height: 200,
            width: 200,
            margin: const EdgeInsets.all(50),
          ),
          onTap: () {
            print ("Container Tapped");
            Feedback.forTap(context); // plays small sound when Tapped
          },
          onLongPress: () {
            print ("Container long pressed");
            Feedback.forLongPress(context); // plays small sound when long-pressed
          },
        ));
  }
}

Code explanation

I created a container which has a green background, a width and height of 200 pixels. The container is placed at a margin of 50 pixels in each direction within the parent (GestureDetector) widget.

The container is wrapped inside a GestureDectetor widget, i.e., the Container widget is made a child of GestureDectetor widget for gesture detection.

onTap() property of GestureDetecor widget is passed a function which is called back when user taps the Container. For demonstration purposes, I have just printed a message “Container Tapped” in the console window (see below video). The statement Feedback.forTap(context)plays a small sound as feedback of a tap gesture to the user.

In production apps, some action such as displaying a new screen (page) of the app is initiated within the callback function.

The preview video is shown below.

Recapitulate

The article has demonstrated gesture detection using the GestureDetector widget. Another article will discuss what happens when — (1) GestureDetectorhas no child widget and (2) has a non-visible child widget.

With that we have reached the end of this article. Thank you once again for reading. Don’t forget to 👏 and follow 😍. Also subscribe our newsletter at https://www.devtechie.com

Flutter
iOS
Andorid
Dart
Cross Platform
Recommended from ReadMedium