Skip to main content

WidgetStatesController

Struct WidgetStatesController 

Source
pub struct WidgetStatesController { /* private fields */ }
Expand description

Manages a set of WidgetStates and notifies listeners of changes.

Used by widgets that expose their internal state for the sake of extensions that add support for additional states. See TextButton for an example.

The controller’s value is its current set of states. Listeners are notified whenever the value changes. The value should only be changed with update; it should not be modified directly.

The controller’s value represents the set of states that a widget’s visual properties, typically WidgetStateProperty values, are resolved against. It is not the intrinsic state of the widget. The widget is responsible for ensuring that the controller’s value tracks its intrinsic state. For example one cannot request the keyboard focus for a widget by adding WidgetState::Focused to its controller. When the widget gains the or loses the focus it will update its controller’s value and notify listeners of the change.

When calling set_state in a WidgetStatesController listener, use the SchedulerBinding::add_post_frame_callback to delay the call to set_state after the frame has been rendered. It’s generally prudent to use the SchedulerBinding::add_post_frame_callback because some of the widgets that depend on WidgetStatesController may call update in their build method. In such cases, listener’s that call set_state - during the build phase - will cause an error.

Dart’s ValueNotifier<Set<WidgetState>> superclass is the change_notifier and value fields here, with its value / set_value / dispose; the handle is a Listenable.

See also:

  • MaterialStatesController, the Material specific version of WidgetStatesController.

Implementations§

Source§

impl WidgetStatesController

Source

pub fn new( app: &mut App, value: Option<WidgetStates>, ) -> Ref<WidgetStatesController>

Creates a WidgetStatesController.

Source

pub fn value(&self) -> &WidgetStates

The current set of states.

Source

pub fn set_value(self: Handle<Self>, app: &mut App, new_value: WidgetStates)

Replaces the current set of states, notifying listeners when it differs (ValueNotifier’s setter). Prefer update.

Source

pub fn dispose(self: Handle<Self>, app: &mut App)

Discards any resources used by the object; see ChangeNotifierData::dispose.

Source

pub fn to_string(self: Handle<Self>, app: &App) -> String

Dart’s WidgetStatesController.toString, which it inherits from ValueNotifier<Set<WidgetState>>: the identity and the set.

Source

pub fn update(self: Handle<Self>, app: &mut App, state: WidgetState, add: bool)

Adds state to value if add is true, and removes it otherwise, and notifies listeners if value has changed.

Trait Implementations§

Source§

impl ChangeNotifier for WidgetStatesController

Source§

fn change_notifier_data(&self) -> &ChangeNotifierData

Source§

fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData

Source§

fn will_add_listener(self: Handle<Self>, app: &mut App)

Runs before the listener is stored. Dart overrides of addListener that work before super.addListener go here.
Source§

fn did_remove_listener(self: Handle<Self>, app: &mut App)

Runs after the listener is removed. Dart overrides of removeListener that work after super.removeListener go here.
Source§

impl Debug for WidgetStatesController

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ListenableObject for T
where T: ChangeNotifier,

Source§

fn add_listener(self: Handle<T>, app: &mut App, listener: Listener)

Register a closure to be called when the object notifies its listeners.
Source§

fn remove_listener(self: Handle<T>, app: &mut App, listener: &Listener)

Remove a previously registered closure from the list of closures that the object notifies.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>