Complete the code to detect a tap gesture on the container.
GestureDetector(
onTap: () {
print('Container tapped');
},
child: Container(
width: 100,
height: 100,
color: Colors.blue[1],
),
)The withOpacity method changes the transparency of the color, making the blue container visible with some transparency.
Complete the code to detect a double tap gesture and print a message.
GestureDetector( [1]: () { print('Double tapped'); }, child: Container( width: 100, height: 100, color: Colors.red, ), )
onTap which only detects single taps.onLongPress which detects long presses, not double taps.The onDoubleTap callback detects when the user taps twice quickly on the widget.
Fix the error in the GestureDetector code to detect a vertical drag update.
GestureDetector(
onPanUpdate: (details) {
if (details.delta.dy [1] 0) {
print('Dragging down');
}
},
child: Container(
width: 150,
height: 150,
color: Colors.green,
),
)The details.delta.dy is positive when dragging down, so the condition should check if it is greater than zero.
Fill both blanks to detect a long press and change the container color.
GestureDetector( [1]: () { setState(() { color = Colors.[2]; }); }, child: Container( width: 120, height: 120, color: color, ), )
onTap instead of onLongPress.onLongPress detects a long press gesture. Changing color to Colors.red updates the container color.
Fill all three blanks to detect horizontal drag start and print the position.
GestureDetector( [1]: (details) { print('Drag started at: ' + details.[2].toString()); }, child: Container( width: 200, height: 100, color: Colors.orange, ), behavior: [3], )
onVerticalDragStart instead of horizontal.localPosition instead of globalPosition if global is needed.onHorizontalDragStart detects the start of a horizontal drag. globalPosition gives the drag start position. HitTestBehavior.opaque ensures the GestureDetector catches taps even on transparent areas.