WidgetStateMapper<T> class

Uses a WidgetStateMap to resolve to a single value of type T based on the current set of Widget 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;
  },
);

Classes that extend WidgetStateMapper can implement any other interface, but should only be used for fields that document their support for WidgetStateProperty objects.

The only exceptions are classes such as double that are marked as base or final, since they can't be implemented—a double property can't be set up to also accept WidgetStateProperty objects and would need to pick one or the other.

For example, a WidgetStateColor.fromMap object can be passed anywhere that accepts either a Color or a WidgetStateProperty object, but attempting to access a Color field (such as Color.value) on the mapper object throws a FlutterError.

Implemented types
Mixed-in types
Annotations

Constructors

WidgetStateMapper(WidgetStateMap<T> map)
Creates a WidgetStateProperty object that can resolve to a value of type T using the provided map.
const

Properties

hashCode int
The hash code for this object.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

debugFillProperties(DiagnosticPropertiesBuilder properties, {String prefix = ''}) → void
Add additional properties associated with the node.
override
noSuchMethod(Invocation invocation) → Never
Invoked when a nonexistent method or property is accessed.
override
resolve(Set<WidgetState> states) → T
Returns a value of type T that depends on states.
override
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
override
toStringShort() String
A brief description of this object, usually just the runtimeType and the hashCode.
inherited

Operators

operator ==(Object other) bool
The equality operator.
override