Skip to main content

WidgetStateProperty

Trait WidgetStateProperty 

Source
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:

Required Methods§

Source

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.

Source

fn as_any(&self) -> &dyn Any

Downcast support for Dart’s is checks in ==.

Provided Methods§

Source

fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder)

Dart’s WidgetStateProperty.debugFillProperties, from Diagnosticable: a property adds its own fields.

Source

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.

Source

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>

Source

pub fn to_string(&self) -> String

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.

Source

pub fn from_map(map: WidgetStateMap<T>) -> WidgetStatePropertyRef<T>
where T: Clone + Debug + PartialEq,

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.

Source

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).

Source

pub fn resolve_with( callback: impl Fn(&WidgetStates) -> T + 'static, ) -> WidgetStatePropertyRef<T>

Convenience method for creating a WidgetStateProperty from a WidgetPropertyResolver function alone.

Source

pub fn all(value: T) -> WidgetStatePropertyRef<T>
where T: Clone + Debug + PartialEq,

Convenience method for creating a WidgetStateProperty that resolves to a single value for all states.

Prefer using WidgetStatePropertyAll directly.

Source

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>>

Source

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§

Source§

impl<T: 'static> PartialEq for dyn WidgetStateProperty<T>

Source§

fn eq(&self, other: &dyn WidgetStateProperty<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§