replaceLastWhere method

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

Creates a new FWidgetStateMap where only the last value associated with a constraint satisfied by states is replaced with the result of calling replace on the original 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 last matching state modified
final modified = property.replaceLastWhere(
  {WidgetState.pressed, WidgetState.focused},
  (color) => color.withOpacity(0.5),
);
// Only the 'hovered' state's color will be modified, 'pressed' remains unchanged

Implementation

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

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

  return FWidgetStateMap<T>(constraints);
}