buildToggleable method

Widget buildToggleable({
  1. FocusNode? focusNode,
  2. ValueChanged<bool>? onFocusChange,
  3. bool autofocus = false,
  4. WidgetStateProperty<MouseCursor>? mouseCursor,
  5. required Size size,
  6. required CustomPainter painter,
})

Typically wraps a painter that draws the actual visuals of the Toggleable with logic to toggle it.

If drawing a radial ink reaction is desired (in Material Design for example), consider providing a subclass of ToggleablePainter as a painter, which implements logic to draw a radial ink reaction for this control. The painter is usually configured with the reaction, position, reactionHoverFade, and reactionFocusFade animation provided by this mixin. It is expected to draw the visuals of the Toggleable based on the current value of these animations. The animations are triggered by this mixin to transition the Toggleable from one state to another.

Material Toggleables must provide a mouseCursor which resolves to a MouseCursor based on the current WidgetState of the Toggleable. Cupertino Toggleables may not provide a mouseCursor. If no mouseCursor is provided, SystemMouseCursors.basic will be used as the mouseCursor across all WidgetStates.

This method must be called from the build method of the State class that uses this mixin. The returned Widget must be returned from the build method - potentially after wrapping it in other widgets.

Implementation

Widget buildToggleable({
  FocusNode? focusNode,
  ValueChanged<bool>? onFocusChange,
  bool autofocus = false,
  WidgetStateProperty<MouseCursor>? mouseCursor,
  required Size size,
  required CustomPainter painter,
}) {
  return FocusableActionDetector(
    actions: _actionMap,
    focusNode: focusNode,
    autofocus: autofocus,
    onFocusChange: onFocusChange,
    enabled: isInteractive,
    onShowFocusHighlight: _handleFocusHighlightChanged,
    onShowHoverHighlight: _handleHoverChanged,
    mouseCursor: mouseCursor?.resolve(states) ?? SystemMouseCursors.basic,
    child: GestureDetector(
      excludeFromSemantics: !isInteractive,
      onTapDown: isInteractive ? _handleTapDown : null,
      onTap: isInteractive ? _handleTap : null,
      onTapUp: isInteractive ? _handleTapEnd : null,
      onTapCancel: isInteractive ? _handleTapEnd : null,
      child: Semantics(
        enabled: isInteractive,
        child: CustomPaint(
          size: size,
          painter: painter,
        ),
      ),
    ),
  );
}