onTap method

GestureDetector onTap(
  1. VoidCallback onTap, {
  2. HitTestBehavior? behavior,
  3. bool excludeFromSemantics = false,
  4. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  5. bool trackpadScrollCausesScale = false,
  6. Offset trackpadScrollToScaleFactor = kDefaultTrackpadScrollToScaleFactor,
  7. Set<PointerDeviceKind>? supportedDevices,
})

Wraps the widget in a GestureDetector that responds to tap gestures.

This is a convenience method for the most common gesture - a simple tap. It creates a GestureDetector with only the tap callback configured, making it perfect for buttons, clickable cards, and other interactive elements.

Parameters:

  • onTap: The callback to execute when the widget is tapped.
  • behavior: How the gesture detector should behave during hit testing. Defaults to null (uses Flutter's default behavior).
  • excludeFromSemantics: Whether to exclude from semantic tree. Defaults to false.
  • dragStartBehavior: When drag gestures should start. Defaults to DragStartBehavior.start.
  • trackpadScrollCausesScale: Whether trackpad scrolling causes scaling. Defaults to false.
  • trackpadScrollToScaleFactor: Scale factor for trackpad scroll conversion.
  • supportedDevices: Set of supported pointer device kinds.

Returns a GestureDetector widget that responds to tap gestures.

Example:

Container(
  padding: EdgeInsets.all(16),
  color: Colors.blue,
  child: Text('Tap me!'),
).onTap(() {
  print('Container was tapped!');
  // Handle tap action
});

// With custom behavior
Icon(Icons.favorite)
  .onTap(
    () => toggleFavorite(),
    behavior: HitTestBehavior.opaque,
  );

Implementation

GestureDetector onTap(
  VoidCallback onTap, {
  HitTestBehavior? behavior,
  bool excludeFromSemantics = false,
  DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  bool trackpadScrollCausesScale = false,
  Offset trackpadScrollToScaleFactor = kDefaultTrackpadScrollToScaleFactor,
  Set<PointerDeviceKind>? supportedDevices,
}) {
  return GestureDetector(
    onTap: onTap,
    behavior: behavior,
    excludeFromSemantics: excludeFromSemantics,
    dragStartBehavior: dragStartBehavior,
    trackpadScrollCausesScale: trackpadScrollCausesScale,
    trackpadScrollToScaleFactor: trackpadScrollToScaleFactor,
    supportedDevices: supportedDevices,
    child: this,
  );
}