pub trait State: Sized + 'static {
type Widget: StatefulWidget<State = Self>;
Show 14 methods
// Required methods
fn state_data(self: Handle<Self>, app: &App) -> &StateData<Self::Widget>;
fn state_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut StateData<Self::Widget>;
fn build(
self: Handle<Self>,
app: &mut App,
context: BuildContext,
) -> WidgetRef;
// Provided methods
fn widget(self: Handle<Self>, app: &App) -> &Self::Widget { ... }
fn context(self: Handle<Self>, app: &App) -> BuildContext { ... }
fn mounted(self: Handle<Self>, app: &App) -> bool { ... }
fn init_state(self: Handle<Self>, app: &mut App) { ... }
fn did_update_widget(
self: Handle<Self>,
app: &mut App,
old_widget: &Self::Widget,
) { ... }
fn set_state(self: Handle<Self>, app: &mut App, f: impl FnOnce(&mut Self)) { ... }
fn deactivate(self: Handle<Self>, app: &mut App) { ... }
fn activate(self: Handle<Self>, app: &mut App) { ... }
fn dispose(self: Handle<Self>, app: &mut App) { ... }
fn did_change_dependencies(self: Handle<Self>, app: &mut App) { ... }
fn debug_fill_properties(
self: Handle<Self>,
app: &App,
properties: &mut DiagnosticPropertiesBuilder,
) { ... }
}Expand description
The logic and internal state for a StatefulWidget.
State is information that (1) can be read synchronously when the widget is built and (2)
might change during the lifetime of the widget. It is the responsibility of the widget
implementer to ensure that the State is promptly notified when such state changes,
using set_state.
State objects are created by the framework by calling the
StatefulWidget::create_state method when inflating a StatefulWidget to insert it
into the tree. Because a given StatefulWidget instance can be inflated multiple times
(e.g., the widget is incorporated into the tree in multiple places at once), there might
be more than one State object associated with a given StatefulWidget instance.
Similarly, if a StatefulWidget is removed from the tree and later inserted in to the
tree again, the framework will call StatefulWidget::create_state again to create a
fresh State object, simplifying the lifecycle of State objects.
Methods take self: Handle<Self>: the state lives in the arena, and widget
/ context read the element it is attached to.
§When a widget needs one
A State is for what a widget owns as a Flutter object across its builds: a
TextEditingController, a FocusNode, an
AnimationController it drives. The app’s own data is not
state: it lives in an Entity, which a
StatelessWidget reads in build and is rebuilt by when it
notifies. A stateful widget whose only job is to hold a counter or a fetched list is app data
in the wrong place.
Required Associated Types§
Sourcetype Widget: StatefulWidget<State = Self>
type Widget: StatefulWidget<State = Self>
The widget this state belongs to; Dart’s State<T> type argument.
Required Methods§
Sourcefn state_data(self: Handle<Self>, app: &App) -> &StateData<Self::Widget>
fn state_data(self: Handle<Self>, app: &App) -> &StateData<Self::Widget>
The framework’s bookkeeping, held under the field state
(state_accessors!).
Sourcefn state_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut StateData<Self::Widget>
fn state_data_mut( self: Handle<Self>, app: &mut App, ) -> &mut StateData<Self::Widget>
See state_data.
Sourcefn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef
fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef
Describes the part of the user interface represented by this widget.
The framework calls this method in a number of different situations. For example:
- After calling
init_state. - After calling
did_update_widget. - After receiving a call to
set_state. - After a dependency of this
Stateobject changes (e.g., anInheritedWidgetreferenced by the previousbuildchanges). - After calling
deactivateand then reinserting theStateobject into the tree at another location.
This method can potentially be called in every frame and should not have any side effects beyond building a widget.
Provided Methods§
Sourcefn widget(self: Handle<Self>, app: &App) -> &Self::Widget
fn widget(self: Handle<Self>, app: &App) -> &Self::Widget
The current configuration.
A State object’s configuration is the corresponding StatefulWidget instance.
This property is initialized by the framework before calling
init_state. If the parent updates this location in the tree to a
new widget with the same runtimeType and Widget::key as the
current configuration, the framework will update this property to refer to the new
widget and then call did_update_widget, passing the old
configuration as an argument.
Sourcefn context(self: Handle<Self>, app: &App) -> BuildContext
fn context(self: Handle<Self>, app: &App) -> BuildContext
The location in the tree where this widget builds.
The framework associates State objects with a BuildContext after creating them
with StatefulWidget::create_state and before calling
init_state. The association is permanent: the State object
will never change its BuildContext. However, the BuildContext itself can be
moved around the tree.
After calling dispose, the framework severs the State object’s
connection with the BuildContext.
Sourcefn mounted(self: Handle<Self>, app: &App) -> bool
fn mounted(self: Handle<Self>, app: &App) -> bool
Whether this State object is currently in a tree.
After creating a State object and before calling init_state,
the framework “mounts” the State object by associating it with a BuildContext.
The State object remains mounted until the framework calls
dispose, after which time the framework will never ask the
State object to build again.
Sourcefn init_state(self: Handle<Self>, app: &mut App)
fn init_state(self: Handle<Self>, app: &mut App)
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which
this object was inserted into the tree (i.e., context) or on the
widget used to configure this object (i.e., widget).
You should not use BuildContext::depend_on_inherited_widget_of_exact_type from this
method. However, did_change_dependencies will be
called immediately following this method, and
BuildContext::depend_on_inherited_widget_of_exact_type can be used there.
Sourcefn did_update_widget(
self: Handle<Self>,
app: &mut App,
old_widget: &Self::Widget,
)
fn did_update_widget( self: Handle<Self>, app: &mut App, old_widget: &Self::Widget, )
Called whenever the widget configuration changes.
If the parent widget rebuilds and requests that this location in the tree update to
display a new widget with the same runtimeType and Widget::key,
the framework will update the widget property of this State
object to refer to the new widget and then call this method with the previous widget
as an argument.
Override this method to respond when the widget changes (e.g., to
start implicit animations).
The framework always calls build after calling
did_update_widget, which means any calls to
set_state in did_update_widget are
redundant.
Sourcefn set_state(self: Handle<Self>, app: &mut App, f: impl FnOnce(&mut Self))
fn set_state(self: Handle<Self>, app: &mut App, f: impl FnOnce(&mut Self))
Notify the framework that the internal state of this object has changed.
Whenever you change the internal state of a State object, make the change in a
function that you pass to set_state:
self.set_state(app, |state| { state.my_state = new_value; });The provided callback is immediately called synchronously. It must not return a
future (the callback cannot be async), since then it would be unclear when the
state was actually being set.
Calling set_state notifies the framework that the internal state
of this object has changed in a way that might impact the user interface in this
subtree, which causes the framework to schedule a build for this
State object.
If you just change the state directly without calling
set_state, the framework might not schedule a
build and the user interface for this subtree might not be updated
to reflect the new state.
Sourcefn deactivate(self: Handle<Self>, app: &mut App)
fn deactivate(self: Handle<Self>, app: &mut App)
Called when this object is removed from the tree.
The framework calls this method whenever it removes this State object from the
tree. In some cases, the framework will reinsert the State object into another
part of the tree (e.g., if the subtree containing this State object is grafted
from one location in the tree to another due to the use of a GlobalKey). If that
happens, the framework will call activate to give the State
object a chance to reacquire any resources that it released in
deactivate. It will then also call build to give
the State object a chance to adapt to its new location in the tree. If the
framework does reinsert this subtree, it will do so before the end of the animation
frame in which the subtree was removed from the tree. For this reason, State
objects can defer releasing most resources until the framework calls their
dispose method.
Sourcefn activate(self: Handle<Self>, app: &mut App)
fn activate(self: Handle<Self>, app: &mut App)
Called when this object is reinserted into the tree after having been removed via
deactivate.
In most cases, after a State object has been deactivated, it is not reinserted
into the tree, and its dispose method will be called to signal that
it is ready to be garbage collected.
In some cases, however, after a State object has been deactivated, the framework
will reinsert it into another part of the tree (e.g., if the subtree containing this
State object is grafted from one location in the tree to another due to the use
of a GlobalKey). If that happens, the framework will call
activate to give the State object a chance to reacquire any
resources that it released in deactivate. It will then also call
build to give the object a chance to adapt to its new location in the
tree.
Sourcefn dispose(self: Handle<Self>, app: &mut App)
fn dispose(self: Handle<Self>, app: &mut App)
Called when this object is removed from the tree permanently.
The framework calls this method when this State object will never build again.
After the framework calls dispose, the State object is
considered unmounted and the mounted property is false. It is an
error to call set_state at this point.
Subclasses should override this method to release any resources retained by this object (e.g., stop any active animations).
Sourcefn did_change_dependencies(self: Handle<Self>, app: &mut App)
fn did_change_dependencies(self: Handle<Self>, app: &mut App)
Called when a dependency of this State object changes.
For example, if the previous call to build referenced an
InheritedWidget that later changed, the framework would call this method to notify
this object about the change.
This method is also called immediately after init_state. It is
safe to call BuildContext::depend_on_inherited_widget_of_exact_type from this method.
Sourcefn debug_fill_properties(
self: Handle<Self>,
app: &App,
properties: &mut DiagnosticPropertiesBuilder,
)
fn debug_fill_properties( self: Handle<Self>, app: &App, properties: &mut DiagnosticPropertiesBuilder, )
Dart’s debugFillProperties: the properties this state shows where its element’s dump
names it (state: ScrollableState#1a2b3(offset: 0.0)), after the lifecycle and mounting
properties State’s base adds, which the framework adds first.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".