WidgetStatesConstraint mixin
This mixin allows WidgetState enum values to be combined
using the &, |, and ~ operators.
A Map with WidgetStatesConstraint objects as keys can be used in the WidgetStateProperty.fromMap constructor to resolve to one of its values, based on the first key that isSatisfiedBy the current set of states.
Example:
// This WidgetStateMap<Color?> resolves to null if no keys match.
WidgetStateProperty<Color?>.fromMap(<WidgetStatesConstraint, Color?>{
WidgetState.error: Colors.red,
WidgetState.hovered & WidgetState.focused: Colors.blueAccent,
WidgetState.focused: Colors.blue,
~WidgetState.disabled: Colors.black,
});
// The same can be accomplished with a WidgetPropertyResolver,
// but it's more verbose:
WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
if (states.contains(WidgetState.error)) {
return Colors.red;
} else if (states.contains(WidgetState.hovered) && states.contains(WidgetState.focused)) {
return Colors.blueAccent;
} else if (states.contains(WidgetState.focused)) {
return Colors.blue;
} else if (!states.contains(WidgetState.disabled)) {
return Colors.black;
}
return null;
});
A widget state combination can be stored in a variable, and WidgetState.any can be used for non-nullable types to ensure that there's a match:
final WidgetStatesConstraint selectedError = WidgetState.selected & WidgetState.error;
final WidgetStateProperty<Color> color = WidgetStateProperty<Color>.fromMap(
<WidgetStatesConstraint, Color>{
selectedError & WidgetState.hovered: Colors.redAccent,
selectedError: Colors.red,
WidgetState.any: Colors.black,
},
);
// The (more verbose) WidgetPropertyResolver implementation:
final WidgetStateProperty<Color> colorResolveWith = WidgetStateProperty.resolveWith<Color>(
(Set<WidgetState> states) {
if (states.containsAll(<WidgetState>{WidgetState.selected, WidgetState.error})) {
if (states.contains(WidgetState.hovered)) {
return Colors.redAccent;
}
return Colors.red;
}
return Colors.black;
},
);
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
isSatisfiedBy(
Set< WidgetState> states) → bool -
Whether the provided
statessatisfy this object's criteria. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator &(
WidgetStatesConstraint other) → WidgetStatesConstraint - Combines two WidgetStatesConstraint values using logical "and".
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator |(
WidgetStatesConstraint other) → WidgetStatesConstraint - Combines two WidgetStatesConstraint values using logical "or".
-
operator ~(
) → WidgetStatesConstraint - Takes a WidgetStatesConstraint and applies the logical "not".