replaceFirstWhere method

  1. @useResult
FWidgetStateMap<T> replaceFirstWhere(
  1. Set<WidgetState> states,
  2. T replace(
    1. T
    )
)

Creates a new FWidgetStateMap where only the first value associated with a constraint satisfied by states is replaced with the result of calling replace on the original value.

Unlike replaceAllWhere, which modifies all matching values, this method only modifies the first matching value.

To replace values associated with WidgetState.any, pass in an empty set.

Where possible, it is strongly recommended to use the CLI to generate a style and directly modify its FWidgetStateMap fields instead.

Example

final property = FWidgetStateMap<Color>({
  WidgetState.pressed: Colors.blue,
  WidgetState.hovered: Colors.green,
});

// Create a new property with only the first matching state modified.
final modified = property.replaceFirstWhere(
  {WidgetState.pressed, WidgetState.focused},
  (color) => color.withOpacity(0.5),
);

// Only the 'pressed' state's color will be modified.

Implementation

@useResult
FWidgetStateMap<T> replaceFirstWhere(Set<WidgetState> states, T Function(T) replace) {
  final constraints = {..._constraints};

  for (final key in constraints.keys) {
    if (key.isSatisfiedBy(states)) {
      constraints[key] = replace(constraints[key] as T);
      break;
    }
  }

  return FWidgetStateMap<T>(constraints);
}