An input gesture in Flutter allows users to interact with an application. Users can control the application by touching the screen, mouse, or keyboard.
onTapDown
onTapUp
onTap
onTapCancel
Double tap: This means tapping the screen twice from the fingertip or double-clicking using a mouse. This gesture has the following events:
onDoubleTap
Long press: Touch the screen from the fingertip or mouse for a long period. This gesture has the following events:
onLongPress
Drag and drop: Press the screen from the fingertip and then drag it to another location. This gesture allows the drag functionalities in two ways:
onHorizontalDragStart
onHorizontalDragUpdate
onHorizontalDragEnd
onVerticalDragStart
onVerticalDragStart
onVerticalDragStart
Pan: Press the screen with the fingertip and move anywhere without releasing it. This gesture has the following events:
onPanStart
onPanUpdate
onPanEnd
Pinch: Zoom in or out with two fingers.
onTapUp
Let’s discuss the example of the onTap
event that will be executed when the user touches the screen:
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _FirstApp createState() => _FirstApp(); } class _FirstApp extends State<MyApp> { Color _rectangle = Colors.green; void _blockColor() { setState(() { _rectangle = _rectangle == Colors.green ? Colors.red : Colors.green; }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: GestureDetector( onTap: _blockColor, child: Container( width: 1000, height: 1000, color: _rectangle, ), ), ), ); } }
Lines 13–17: The _blockColor()
function will be called when the user taps the screen. This function will change the color on each tap from green to red and red to green.
Lines 24–28: We have set the container widget. This widget has a width
of 1000
, a height
of 100
, and a background color
is set to _rectangle
. This _rectangle
will change color on each tap.
Free Resources