build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Before building the widget, if there are callback functions for the given events, necessary callback functions with defined touch ripple effect logic will be defined for those events.

Then, the defined callback functions are passed directly to the widget responsible for managing gestures as its child widget.

Implementation

@override
Widget build(BuildContext context) {
  final gestureDetector = createGestureDetector(
    onTap: widget.onTap,
    onTapAsync: widget.onTapAsync,
    onTapAsyncStart: widget.onTapAsyncStart,
    onTapAsyncEnd: widget.onTapAsyncEnd,
    onDoubleTap: widget.onDoubleTap,
    onDoubleTapStart: widget.onDoubleTapStart,
    onDoubleTapEnd: widget.onDoubleTapEnd,
    onLongTap: widget.onLongTap,
    onLongTapStart: widget.onLongTapStart,
    onLongTapEnd: widget.onLongTapEnd,
    onFocusStart: widget.onFocusStart,
    onFocusEnd: widget.onFocusEnd,
    child: TouchRippleStack(
      renderOrder: widget.renderOrder,
      rippleColor: rippleColor,
      rippleScale: widget.rippleScale,
      blurRadius: widget.rippleBlurRadius,
      borderRadius: widget.borderRadius,
      controller: controller,
      child: widget.child,
    ),
  );

  // If current platform is touch-based,
  // don't need to define mouse-related behaviours.
  //
  // Even if hover is considered,
  // the stylus hover listener functionality is no longer
  // supported in Flutter.
  if (Platform.isFuchsia
   || Platform.isAndroid
   || Platform.isIOS) return gestureDetector;

  PointerEnterEventListener? onHoverStartCallBack;
  PointerExitEventListener? onHoverEndCallBack;
  MouseCursor? cursor;

  // The following code block takes the task of defining
  // the mouse hover event callback functions.
  {
    onHoverStartCallBack = (event) {
      if (widget.useHoverEffect == false) return;

      // If hover state already initialised,
      // it will fade back in without creating the state.
      if (controller.hoverState == null) {
        Color getDefaultHoverColor() {
          return rippleColor.withAlpha(
            (rippleColor.alpha * widget.hoverColorRelativeOpacity).toInt()
          );
        }

        final newState = controller.createBackgroundState(
          color: widget.hoverColor ?? getDefaultHoverColor(),
          fadeInDuration: widget.hoverFadeInDuration,
          fadeInCurve: widget.hoverFadeInCurve,
          fadeOutDuration: widget.hoverFadeOutDuration,
          fadeOutCurve: widget.hoverFadeOutCurve,
          onDispatch: () {
            controller.hoverState?.dispose();
            controller.hoverState = null;
          },
        );
        controller.hoverState = newState;
      }
      controller.hoverState?.fadeIn();
      widget.onHoverStart?.call();
    };
    onHoverEndCallBack = (event) {
      controller.hoverState?.fadeOut();

      widget.onHoverEnd?.call();
    };
  }

  if (widget.onTap != null
   || widget.onDoubleTap != null
   || widget.onLongTap != null) {
    cursor = widget.hoverCursor;
  }
  return MouseRegion(
    onEnter: onHoverStartCallBack,
    onExit: onHoverEndCallBack,
    cursor: cursor ?? MouseCursor.defer,
    child: gestureDetector,
  );
}