StatedWidget.builder constructor

const StatedWidget.builder({
  1. Key? key,
  2. required Widget builder(
    1. BuildContext context,
    2. Set<WidgetState> states
    ),
})

Creates a StatedWidget using a builder function for dynamic state handling.

Provides maximum flexibility by using a builder function that receives the current set of active widget states and returns the appropriate widget. This approach allows for complex state logic, animations, and dynamic visual computations based on state combinations.

The builder function is called whenever the widget states change, allowing for real-time adaptation to state transitions. This is ideal for complex UI that needs to respond to multiple simultaneous states.

Parameters:

  • builder (Function, required): Builder function receiving context and states

Example:

StatedWidget.builder(
  builder: (context, states) {
    if (states.contains(WidgetState.disabled)) {
      return Opacity(opacity: 0.5, child: Icon(Icons.block));
    }
    if (states.contains(WidgetState.selected)) {
      return Icon(Icons.check_circle, color: Colors.green);
    }
    if (states.contains(WidgetState.hovered)) {
      return AnimatedScale(scale: 1.1, child: Icon(Icons.star));
    }
    return Icon(Icons.star_border);
  },
)

Implementation

const factory StatedWidget.builder({
  Key? key,
  required Widget Function(BuildContext context, Set<WidgetState> states)
      builder,
}) = _BuilderStatedWidget;