handleMouseEvent method

void handleMouseEvent(
  1. MouseEvent event,
  2. int localX,
  3. int localY
)

Handles mouse movement, press, and release within this button's bounds.

Implementation

void handleMouseEvent(MouseEvent event, int localX, int localY) {
  final resolvedWidth = widget.width ?? _lastWidth;
  final resolvedHeight = widget.height ?? _lastHeight;
  if (resolvedWidth <= 0 || resolvedHeight <= 0) return;

  final inBounds =
      localX >= 0 &&
      localX < resolvedWidth &&
      localY >= 0 &&
      localY < resolvedHeight;

  if (event.type == MouseEventType.press && inBounds) {
    setState(() {
      _isPressed = true;
      _isHovered = false;

      final clickPoint = Point(localX, localY);
      // Trigger both the radial ripple and the sparkle trail on the click coordinates
      triggerEffect(_ripple, clickPoint);
      triggerEffect(_sparkles, clickPoint);
    });
  } else if (event.type == MouseEventType.release) {
    if (_isPressed) {
      setState(() {
        _isPressed = false;
        _ripple.reset();
        _sparkles.reset();
        if (inBounds) {
          _isHovered = true;

          // Trigger a full layout flash upon successful button activation
          triggerEffect(_flash, Point(localX, localY));
          widget.onPressed();
        }
      });
    }
  } else if (event.type == MouseEventType.move ||
      event.type == MouseEventType.drag) {
    setState(() {
      _isHovered = inBounds && !_isPressed;
    });
  }
}