MaterialStateColor class

Defines a Color whose value depends on a set of MaterialStates which represent the interactive state of a component.

This is useful for improving the accessibility of text in different states of a component. For example, in a FlatButton with blue text, the text will become more difficult to read when the button is hovered, focused, or pressed, because the contrast ratio between the button and the text will decrease. To solve this, you can use MaterialStateColor to make the text darker when the FlatButton is hovered, focused, or pressed.

To use a MaterialStateColor, you can either:

  1. Create a subclass of MaterialStateColor and implement the abstract resolve method.
  2. Use MaterialStateColor.resolveWith and pass in a callback that will be used to resolve the color in the given states.

This should only be used as parameters when they are documented to take MaterialStateColor, otherwise only the default state will be used.

This example shows how you could pass a MaterialStateColor to FlatButton.textColor. Here, the text color will be Colors.blue[900] when the button is being pressed, hovered, or focused. Otherwise, the text color will be Colors.blue[600].
Color getTextColor(Set<MaterialState> states) {
  const Set<MaterialState> interactiveStates = <MaterialState>{
    MaterialState.pressed,
    MaterialState.hovered,
    MaterialState.focused,
  };
  if (states.any(interactiveStates.contains)) {
    return Colors.blue[900];
  }
  return Colors.blue[600];
}

FlatButton(
  child: Text('FlatButton'),
  onPressed: () {},
  textColor: MaterialStateColor.resolveWith(getTextColor),
),

Inheritance
Implemented types

Constructors

MaterialStateColor(int defaultValue)
Creates a MaterialStateColor. [...]
const

Properties

alpha int
The alpha channel of this color in an 8 bit value. [...]
read-only, inherited
blue int
The blue channel of this color in an 8 bit value.
read-only, inherited
green int
The green channel of this color in an 8 bit value.
read-only, inherited
hashCode int
The hash code for this object. [...]
read-only, inherited
opacity double
The alpha channel of this color as a double. [...]
read-only, inherited
red int
The red channel of this color in an 8 bit value.
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
value int
A 32 bit value representing this color. [...]
final, inherited

Methods

resolve(Set<MaterialState> states) Color
Returns a Color that's to be used when a Material component is in the specified state.
override
computeLuminance() double
Returns a brightness value between 0 for darkest and 1 for lightest. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited
withAlpha(int a) Color
Returns a new color that matches this color with the alpha channel replaced with a (which ranges from 0 to 255). [...]
inherited
withBlue(int b) Color
Returns a new color that matches this color with the blue channel replaced with b (which ranges from 0 to 255). [...]
inherited
withGreen(int g) Color
Returns a new color that matches this color with the green channel replaced with g (which ranges from 0 to 255). [...]
inherited
withOpacity(double opacity) Color
Returns a new color that matches this color with the alpha channel replaced with the given opacity (which ranges from 0.0 to 1.0). [...]
inherited
withRed(int r) Color
Returns a new color that matches this color with the red channel replaced with r (which ranges from 0 to 255). [...]
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited

Static Methods

resolveWith(MaterialPropertyResolver<Color> callback) MaterialStateColor
Creates a MaterialStateColor from a MaterialPropertyResolver<Color> callback function. [...]
override