Flutter - Gestures - GeeksforGeeks

14 Sep, 2020
Gestures are used to interact with an application. It is generally used in touch-based devices to physically interact with the application. It can be as simple as a single tap on the screen to a more complex physical interaction like swiping in a specific direction to scrolling down an application. It is heavily used in gaming and more or less every application requires it to function as devices turn more touch-based than ever. In this article, we will discuss them in detail.
Some widely used gestures are mentioned here :
Tap: Touching the surface of the device with the fingertip for a small duration of time period and finally releasing the fingertip.
Double Tap: Tapping twice in a short time.
Drag: Touching the surface of the device with the fingertip and then moving the fingertip in a steadily and finally releasing the fingertip.
Flick: Similar to dragging, but doing it in a speedier way.
Pinch: Pinching the surface of the device using two fingers.
Zoom: Opposite of pinching.
Panning: Touching the device surface with the fingertip and moving it in the desired direction without releasing the fingertip.
The GestureDetector widget in flutter is used to detect physical interaction with the application on the UI. If a widget is supposed to experience a gesture, it is kept inside the GestureDetector widget. The same widget catches the gesture and returns the appropriate action or response.
Below is the list of gestures and their corresponding events :
onTapDown
onTapUp
onTap
onTapCancel
onDoubleTap
onLongPress
onVerticalDragStart
onVerticalDragUpdate
onVerticalDragEnd
onHorizontalDragStart
onHorizontalDragUpdate
onHorizontalDragEnd
onPanStart
onPanUpdate
onPanEnd
Example:
Dart
import 'package:flutter/material.dart';  void main() => runApp(MyApp());  class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    final title = 'Gestures';      return MaterialApp(      title: title,      home: MyHomePage(title: title),    );  }}  class MyHomePage extends StatelessWidget {  final String title;    MyHomePage({Key key, this.title}) : super(key: key);    @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text('GeeksForGeeks'),        backgroundColor: Colors.green,      ),      body: Center(child: MyButton()),    );  }}  class MyButton extends StatelessWidget {  @override  Widget build(BuildContext context) {    // The GestureDetector wraps the button.    return GestureDetector(      // show snackbar on tap of child      onTap: () {        final snackBar = SnackBar(content: Text(" You just Tapped on the Button"));          Scaffold.of(context).showSnackBar(snackBar);      },      // The tap button      child: Container(        padding: EdgeInsets.all(12.0),        decoration: BoxDecoration(          color: Theme.of(context).buttonColor,          borderRadius: BorderRadius.circular(8.0),        ),        child: Text('Tap Button'),      ),    );  }}
Output:
Flutter supports Listener widgets that are low-level gesture detection mechanism.  It detects user interaction and dispatches one of the below mentioned corresponding events:
PointerDownEvent
PointerMoveEvent
PointerUpEvent
PointerCancelEvent
Flutter also has widgets to do specific as well as advanced gestures. These widgets are as below :
Dismissible: Supports flick gesture to terminate the widget.
Draggable: Supports drag gesture to make the widget mobile.
LongPressDraggable: If the parent widgets are draggable, it supports the drag gesture to move the children widget.
DragTarget: Accepts any Draggable widget
IgnorePointer: When used it hides the widget and its corresponding children from the gesture detection process.
AbsorbPointer: Stops the gesture detection process itself to avoid action dispatch on overlapping widgets.
Scrollable: Makes content available inside the widget scrollable.
android
Flutter
Dart
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flutter - DropDownButton Widget
Listview.builder in Flutter
Flutter - Asset Image
Splash Screen in Flutter
Flutter - Custom Bottom Navigation Bar
Flutter - Checkbox Widget
ListView Class in Flutter
Getter and Setter Methods in Dart
Flutter - Flexible Widget
Flutter - BoxShadow Widget