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) { ... }
fn save(&self) -> Option<String> { ... }
fn restore(&mut self, _saved: &str) { ... }
}Expand description
What a StatefulWidget keeps, and how it draws itself from it.
Required Associated Types§
Sourcetype Widget: StatefulWidget<State = Self>
type Widget: StatefulWidget<State = Self>
The widget this is the state of.
Required Methods§
Sourcefn build(&self, cx: &StateContext<'_, '_, Self>) -> Box<dyn Widget>
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§
Sourcefn init_state(&mut self, _cx: &StateContext<'_, '_, Self>)
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.
Sourcefn did_update_widget(
&mut self,
_old: &Self::Widget,
_cx: &StateContext<'_, '_, Self>,
)
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.
Sourcefn dispose(&mut self)
fn dispose(&mut self)
Called once, when the widget leaves the tree. The state is never used again.
Sourcefn save(&self) -> Option<String>
fn save(&self) -> Option<String>
What this state keeps across a restart under frus run (R, or r): the new
build of the application starts with it, given back through State::restore. Any
text will do — a number, a few fields joined, a JSON document.
None, the default, keeps nothing: the state starts afresh, as on a first launch.
Nothing is saved outside frus run.
fn save(&self) -> Option<String> {
Some(self.count.to_string())
}
fn restore(&mut self, saved: &str) {
self.count = saved.parse().unwrap_or(0);
}Sourcefn restore(&mut self, _saved: &str)
fn restore(&mut self, _saved: &str)
Takes back what State::save returned before a restart. Called once, right after
StatefulWidget::create_state and before State::init_state, and only for the
component at the same place, of the same type. What no longer fits — the code changed
in between — can simply be ignored.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".