MaterialStateColor class Null safety

Defines a Color that is also a MaterialStateProperty.

This class exists to enable widgets with Color valued properties to also accept MaterialStateProperty<Color> values. A material state color property represents a color which depends on a widget's "interactive state". This state is represented as a Set of MaterialStates, like MaterialState.pressed, MaterialState.focused and MaterialState.hovered.

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.

If a MaterialStateColor is used for a property or a parameter that doesn't support resolving MaterialStateProperty<Color>s, then its default color value will be used for all states.

To define a const MaterialStateColor, you'll need to extend MaterialStateColor and override its resolve method. You'll also need to provide a defaultValue to the super constructor, so that we can know at compile-time what its default color is.

This class enables existing widget implementations with Color properties to be extended to also effectively support MaterialStateProperty<Color> property values. MaterialStateColor should only be used with widgets that document their support, like TimePickerThemeData.dayPeriodColor.

This example defines a MaterialStateColor with a const constructor.
class MyColor extends MaterialStateColor {
  const MyColor() : super(_defaultColor);

  static const int _defaultColor = 0xcafefeed;
  static const int _pressedColor = 0xdeadbeef;

  @override
  Color resolve(Set<MaterialState> states) {
    if (states.contains(MaterialState.pressed)) {
      return const Color(_pressedColor);
    }
    return const Color(_defaultColor);
  }
}

Inheritance
Implemented types

Constructors

MaterialStateColor(int defaultValue)
Abstract const constructor. This constructor enables subclasses to provide const constructors so that they can be used in const expressions.
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

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
resolve(Set<MaterialState> states) Color
Returns a Color that's to be used when a Material component is in the specified state.
override
toString() String
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 ==(Object other) bool
The equality operator. [...]
inherited

Static Methods

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