MaterialStateColor typedef
- @Deprecated('Use WidgetStateColor instead. ' 'Moved to the Widgets layer to make code available outside of Material. ' 'This feature was deprecated after v3.19.0-0.3.pre.')
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.
MaterialStateColor should only be used with widgets that document their support, like TimePickerThemeData.dayPeriodColor.
To use a MaterialStateColor, you can either:
- Create a subclass of MaterialStateColor and implement the abstract
resolvemethod. - 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 example defines a MaterialStateColor with a const constructor.
// ignore: deprecated_member_use
class MyColor extends MaterialStateColor {
const MyColor() : super(_defaultColor);
static const int _defaultColor = 0xcafefeed;
static const int _pressedColor = 0xdeadbeef;
@override
// ignore: deprecated_member_use
Color resolve(Set<MaterialState> states) {
// ignore: deprecated_member_use
if (states.contains(MaterialState.pressed)) {
return const Color(_pressedColor);
}
return const Color(_defaultColor);
}
}
See also
- WidgetStateColor, the non-Material version that can be used
interchangeably with
MaterialStateColor.
Implementation
// TODO(framework): Add unit tests to this code snippet.
// https://github.com/flutter/flutter/issues/188530
///
/// This example defines a [MaterialStateColor] with a const constructor.
///
/// ```dart
/// // ignore: deprecated_member_use
/// class MyColor extends MaterialStateColor {
/// const MyColor() : super(_defaultColor);
///
/// static const int _defaultColor = 0xcafefeed;
/// static const int _pressedColor = 0xdeadbeef;
///
/// @override
/// // ignore: deprecated_member_use
/// Color resolve(Set<MaterialState> states) {
/// // ignore: deprecated_member_use
/// if (states.contains(MaterialState.pressed)) {
/// return const Color(_pressedColor);
/// }
/// return const Color(_defaultColor);
/// }
/// }
/// ```
///
/// </callout-box>
///
/// See also
///
/// * [WidgetStateColor], the non-Material version that can be used
/// interchangeably with `MaterialStateColor`.
@Deprecated(
'Use WidgetStateColor instead. '
'Moved to the Widgets layer to make code available outside of Material. '
'This feature was deprecated after v3.19.0-0.3.pre.',
)
typedef MaterialStateColor = WidgetStateColor;