GestureDetector Widget
The GestureDetector widget is the primary way to handle user interactions in Flitter. It wraps a child widget and detects various gestures and mouse events.
Supported Gestures
Click Gesture
onClick
: Handles click events
GestureDetector({
onClick: () => console.log("Clicked"),
child: /* widget */
})
Mouse Events
onMouseDown
: When mouse button is pressedonMouseMove
: When mouse is movedonMouseUp
: When mouse button is releasedonMouseOver
: When mouse is over the widgetonMouseEnter
: When mouse enters the widgetonMouseLeave
: When mouse leaves the widget
GestureDetector({
onMouseDown: (e) => console.log("Mouse down", e),
onMouseMove: (e) => console.log("Mouse move", e),
onMouseUp: (e) => console.log("Mouse up", e),
onMouseEnter: (e) => console.log("Mouse enter", e),
onMouseLeave: (e) => console.log("Mouse leave", e),
child: /* widget */
})
Drag Gestures
onDragStart
: When drag beginsonDragMove
: During dragonDragEnd
: When drag ends
GestureDetector({
onDragStart: (e) => console.log("Drag start", e),
onDragMove: (e) => console.log("Dragging", e),
onDragEnd: (e) => console.log("Drag end", e),
child: /* widget */
})
Wheel Event
onWheel
: Mouse wheel scroll
GestureDetector({
onWheel: (e) => console.log("Wheel event", e),
child: /* widget */
})
Available Props
onClick
: (event: MouseEvent) => voidonMouseDown
: (event: MouseEvent) => voidonMouseMove
: (event: MouseEvent) => voidonMouseUp
: (event: MouseEvent) => voidonMouseOver
: (event: MouseEvent) => voidonMouseEnter
: (event: MouseEvent) => voidonMouseLeave
: (event: MouseEvent) => voidonDragStart
: (event: MouseEvent) => voidonDragMove
: (event: MouseEvent) => voidonDragEnd
: (event: MouseEvent) => voidonWheel
: (event: WheelEvent) => voidcursor
: Mouse cursor style of Cursor type
Cursor Types
Use the cursor
prop to set the mouse cursor when hovering over the widget:
- “pointer”
- “default”
- “move”
- “text”
- “wait”
- “help”
- “progress”
- “not-allowed”
- “crosshair”
- “grab”
- “grabbing”
- And more…
Example:
GestureDetector({
cursor: "pointer",
child: /* widget */
})
Common Use Cases
Interactive Button
GestureDetector({
onClick: handleClick,
child: CustomButton(),
});
Draggable Item
GestureDetector({
onDragStart: handleDragStart,
onDragMove: handleDragMove,
onDragEnd: handleDragEnd,
child: DraggableItem(),
});
Mouse Interaction
GestureDetector({
onMouseEnter: handleMouseEnter,
onMouseLeave: handleMouseLeave,
child: HoverableWidget(),
});
Best Practices
State Management
- Use StatefulWidget and setState for state management
- Minimize state changes to avoid unnecessary re-renders
Event Handling
- Use appropriate event handlers for different interactions
- Use event.stopPropagation() when needed to prevent event bubbling
Performance
- Avoid heavy computations in event handlers
- Use requestAnimationFrame for performance optimization when needed
Tips and Tricks
Custom Cursors
- Use the cursor prop to provide visual feedback to users
Event Propagation
- Control event bubbling using event.stopPropagation() and event.preventDefault()
Debugging
- Use console.log in event handlers to inspect event data