lerpWhere<T> static method
FWidgetStateMap<T>
lerpWhere<T>(
- FWidgetStateMap<
T> a, - FWidgetStateMap<
T> b, - double t,
- T? lerp(
- T?,
- T?,
- double
Linearly interpolate between two FWidgetStateMaps using the provided lerp
function.
Consider using lerpBoxDecoration, lerpColor, lerpIconThemeData, or lerpTextStyle.
Constraint operand order matters
TL;DR: Always make sure that constraint operands are exactly the same across a
and b
.
WidgetStatesConstraints are not commutative. Reversed constraint operands (like .hovered & .pressed
vs
.pressed & .hovered
) are treated as distinct keys.
For each constraint in either map, lerp
is called with the values from both maps, using null for missing values.
WidgetState.pressed && Widget
final a = FWidgetStateMap<double>({
WidgetState.pressed: 1.0,
WidgetState.hovered & Widget.focused: 0.8,
});
final b = FWidgetStateMap<double>({
WidgetState.pressed: 0.5,
WidgetState.focused & Widget.focused: 0.9,
});
final result = FWidgetStateMap.lerpWhere(a, b, 0.5, lerpDouble);
// Result contains:
// - .pressed: 0.75 (lerp between 1.0 and 0.5)
// - .hovered & .focused: 0.4 (lerp between 0.8 and null)
// - .focused & .hovered: 0.45 (lerp between null and 0.9)
As resolve treats state combinations as equivalent regardless of order, {.hovered & .focused}
will always
return 0.4, the first matching constraint, and never 0.45, the last matching constraint.
Implementation
static FWidgetStateMap<T> lerpWhere<T>(
FWidgetStateMap<T> a,
FWidgetStateMap<T> b,
double t,
T? Function(T?, T?, double) lerp,
) {
final visited = <WidgetStatesConstraint>{};
final constraints = <WidgetStatesConstraint, T>{};
for (final MapEntry(key: constraint, :value) in a._constraints.entries) {
visited.add(constraint);
if (lerp(value, b._constraints[constraint], t) case final lerped?) {
constraints[constraint] = lerped;
}
}
for (final MapEntry(key: constraint, :value) in b._constraints.entries) {
if (!visited.contains(constraint)) {
if (lerp(a._constraints[constraint], value, t) case final lerped?) {
constraints[constraint] = lerped;
}
}
}
return FWidgetStateMap(constraints);
}