Skip to main content

State

Trait State 

Source
pub trait State: Sized + 'static {
    type Widget: StatefulWidget<State = Self>;

    // Required method
    fn build(&self, cx: &StateContext<'_, '_, Self>) -> Box<dyn Widget>;

    // Provided methods
    fn init_state(&mut self, _cx: &StateContext<'_, '_, Self>) { ... }
    fn did_update_widget(
        &mut self,
        _old: &Self::Widget,
        _cx: &StateContext<'_, '_, Self>,
    ) { ... }
    fn dispose(&mut self) { ... }
}
Expand description

What a StatefulWidget keeps, and how it draws itself from it.

Required Associated Types§

Source

type Widget: StatefulWidget<State = Self>

The widget this is the state of.

Required Methods§

Source

fn build(&self, cx: &StateContext<'_, '_, Self>) -> Box<dyn Widget>

The widget this state stands for. Called whenever the tree is rebuilt.

It only reads the state. What a handler changes goes through StateContext::callback or StateContext::handle; changing the state from inside build itself panics, because a build that changes what it builds from never settles.

Provided Methods§

Source

fn init_state(&mut self, _cx: &StateContext<'_, '_, Self>)

Called once, right after the state is made. Start what has to run for as long as the widget is there — a listener, a timer — here, and stop it in State::dispose.

Source

fn did_update_widget( &mut self, _old: &Self::Widget, _cx: &StateContext<'_, '_, Self>, )

Called when the tree is rebuilt and the widget above changed, or was made again. old is the configuration it replaces; the new one is StateContext::widget.

Source

fn dispose(&mut self)

Called once, when the widget leaves the tree. The state is never used again.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§