pub trait WidgetStateProperty<T>: Debug + 'static {
// Required methods
fn resolve(&self, states: &WidgetStates) -> T;
fn as_any(&self) -> &dyn Any;
// Provided methods
fn debug_fill_properties(
&self,
properties: &mut DiagnosticPropertiesBuilder,
) { ... }
fn as_mapper(&self) -> Option<&WidgetStateMapper<T>> { ... }
fn eq_property(&self, other: &dyn WidgetStateProperty<T>) -> bool { ... }
}Expand description
Interface for classes that resolve to a value of type T based
on a widget’s interactive “state”, which is defined as a set
of WidgetStates.
Widget state properties represent values that depend on a widget’s “state”.
The state is encoded as a set of WidgetState values, like
WidgetState::Focused, WidgetState::Hovered, WidgetState::Pressed. For
example the InkWell.overlayColor defines the color that fills the ink well
when it’s pressed (the “splash color”), focused, or hovered. The InkWell
uses the overlay color’s resolve method to compute the color for the
ink well’s current state.
ButtonStyle, which is used to configure the appearance of
buttons like TextButton, ElevatedButton, and OutlinedButton,
has many material state properties. The button widgets keep track
of their current material state and resolve the button style’s
material state properties when their value is needed.
§Performance Consideration
In order for constructed WidgetStateProperty objects to be recognized as
equivalent, they need to have overrides for ==
(eq_property).
This comes into play when, for instance, two ThemeData objects are being
compared for equality.
For a concrete WidgetStateProperty object that supports stable
equality checks, consider using WidgetStateMapper.
A property is stored erased as a WidgetStatePropertyRef, whose == is Dart’s;
Dart’s static members (from_map, resolve_as, resolve_with, all, lerp) are on
<dyn WidgetStateProperty<T>>.
See also:
MaterialStateProperty, the Material specific version ofWidgetStateProperty.WidgetStateColorandWidgetStateMouseCursor, the value types that implement it.
Required Methods§
Sourcefn resolve(&self, states: &WidgetStates) -> T
fn resolve(&self, states: &WidgetStates) -> T
Returns a value of type T that depends on states.
Widgets like TextButton and ElevatedButton apply this method to their
current WidgetStates to compute colors and other visual parameters
at build time.
Provided Methods§
Sourcefn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder)
fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder)
Dart’s WidgetStateProperty.debugFillProperties, from Diagnosticable: a property
adds its own fields.
Sourcefn as_mapper(&self) -> Option<&WidgetStateMapper<T>>
fn as_mapper(&self) -> Option<&WidgetStateMapper<T>>
Dart’s other is WidgetStateMapper<T>: the mapper this property is, if it is one,
as the private mapper subclasses of WidgetStateColor and the rest are.
Sourcefn eq_property(&self, other: &dyn WidgetStateProperty<T>) -> bool
fn eq_property(&self, other: &dyn WidgetStateProperty<T>) -> bool
Dart’s ==. Identity unless a class overrides it, as WidgetStatePropertyAll and
WidgetStateMapper do.
Implementations§
Source§impl<T: 'static> dyn WidgetStateProperty<T>
impl<T: 'static> dyn WidgetStateProperty<T>
Sourcepub fn to_string(&self) -> Stringwhere
T: WidgetStatePropertyValue + Debug,
pub fn to_string(&self) -> Stringwhere
T: WidgetStatePropertyValue + Debug,
Dart’s WidgetStateProperty.toString: WidgetStatePropertyAll(value) for one, the
mapper’s own text, Instance of '_WidgetStatePropertyWith<T>' for a resolveWith, and
the property’s Debug for another implementation.
Sourcepub fn from_map(map: WidgetStateMap<T>) -> WidgetStatePropertyRef<T>
pub fn from_map(map: WidgetStateMap<T>) -> WidgetStatePropertyRef<T>
Creates a property that resolves using a WidgetStateMap.
This constructor’s resolve method finds the first
entry whose key is satisfied by the set of states, and returns its associated value.
Panics if no keys match (see WidgetStateMapper).
To prevent a situation where each WidgetStatesConstraint
isn’t satisfied by the given set of states, consider adding
WidgetState::any as the final WidgetStateMap key.
Sourcepub fn resolve_as<V: MaybeWidgetStateProperty<T>>(
value: &V,
states: &WidgetStates,
) -> V
pub fn resolve_as<V: MaybeWidgetStateProperty<T>>( value: &V, states: &WidgetStates, ) -> V
Resolves the value for the given set of states if value is a
WidgetStateProperty, otherwise returns the value itself.
This is useful for widgets that have parameters which can optionally be a
WidgetStateProperty. For example, InkWell.mouseCursor can be a
MouseCursor or a WidgetStateProperty<MouseCursor>.
Dart’s T is the value’s own type here (AnyColor for a Color,
MouseCursorRef for a MouseCursor, or an Option of one):
<dyn WidgetStateProperty<Color>>::resolve_as(&color, &states).
Sourcepub fn resolve_with(
callback: impl Fn(&WidgetStates) -> T + 'static,
) -> WidgetStatePropertyRef<T>
pub fn resolve_with( callback: impl Fn(&WidgetStates) -> T + 'static, ) -> WidgetStatePropertyRef<T>
Convenience method for creating a WidgetStateProperty from a
WidgetPropertyResolver function alone.
Sourcepub fn all(value: T) -> WidgetStatePropertyRef<T>
pub fn all(value: T) -> WidgetStatePropertyRef<T>
Convenience method for creating a WidgetStateProperty that resolves
to a single value for all states.
Prefer using WidgetStatePropertyAll directly.
Sourcepub fn lerp(
a: Option<WidgetStatePropertyRef<T>>,
b: Option<WidgetStatePropertyRef<T>>,
t: f64,
lerp_function: impl Fn(Option<T>, Option<T>, f64) -> Option<T> + 'static,
) -> Option<WidgetStatePropertyRef<Option<T>>>
pub fn lerp( a: Option<WidgetStatePropertyRef<T>>, b: Option<WidgetStatePropertyRef<T>>, t: f64, lerp_function: impl Fn(Option<T>, Option<T>, f64) -> Option<T> + 'static, ) -> Option<WidgetStatePropertyRef<Option<T>>>
Linearly interpolate between two WidgetStatePropertys.
Source§impl<T: 'static> dyn WidgetStateProperty<Option<T>>
impl<T: 'static> dyn WidgetStateProperty<Option<T>>
Sourcepub fn lerp_optional(
a: Option<WidgetStatePropertyRef<Option<T>>>,
b: Option<WidgetStatePropertyRef<Option<T>>>,
t: f64,
lerp_function: impl Fn(Option<T>, Option<T>, f64) -> Option<T> + 'static,
) -> Option<WidgetStatePropertyRef<Option<T>>>
pub fn lerp_optional( a: Option<WidgetStatePropertyRef<Option<T>>>, b: Option<WidgetStatePropertyRef<Option<T>>>, t: f64, lerp_function: impl Fn(Option<T>, Option<T>, f64) -> Option<T> + 'static, ) -> Option<WidgetStatePropertyRef<Option<T>>>
Linearly interpolate between two WidgetStatePropertys of a nullable value.
This is Dart’s WidgetStateProperty.lerp<T> with T nullable, such as the
WidgetStateProperty<Color?> fields of a ButtonStyle: Dart’s T?? is T?, so the
lerped property resolves to Option<T>, where lerp over
Option<T> would resolve to Option<Option<T>>. A side that resolves to None is
handed to lerp_function as None, as Dart hands it null.
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".