Skip to main content

State

Trait State 

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

Source

type Widget: StatefulWidget<State = Self>

The widget this state belongs to; Dart’s State<T> type argument.

Required Methods§

Source

fn state_data(self: Handle<Self>, app: &App) -> &StateData<Self::Widget>

The framework’s bookkeeping, held under the field state (state_accessors!).

Source

fn state_data_mut( self: Handle<Self>, app: &mut App, ) -> &mut StateData<Self::Widget>

Source

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 State object changes (e.g., an InheritedWidget referenced by the previous build changes).
  • After calling deactivate and then reinserting the State object 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§

Source

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.

Source

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.

Source

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.

It is an error to call set_state unless mounted is true.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

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

Implementors§

Source§

impl State for ActionListenerState

Source§

impl State for ActionsState

Source§

impl State for AnimatedAlignState

Source§

impl State for AnimatedContainerState

Source§

impl State for AnimatedCrossFadeState

Source§

impl State for AnimatedDefaultTextStyleState

Source§

impl State for AnimatedFractionallySizedBoxState

Source§

impl State for AnimatedOpacityState

Source§

impl State for AnimatedPaddingState

Source§

impl State for AnimatedPhysicalModelState

Source§

impl State for AnimatedPositionedDirectionalState

Source§

impl State for AnimatedPositionedState

Source§

impl State for AnimatedRotationState

Source§

impl State for AnimatedScaleState

Source§

impl State for AnimatedSizeState

Source§

impl State for AnimatedSlideState

Source§

impl State for AnimatedSwitcherState

Source§

impl State for AutofillGroupState

Source§

impl State for AutomaticKeepAliveState

Source§

impl State for BannerState

Source§

impl State for DismissibleState

Source§

impl State for DraggableScrollableActuatorState

Source§

impl State for DraggableScrollableSheetState

Source§

impl State for DualTransitionBuilderState

Source§

impl State for EditableTextState

Source§

impl State for ExpansibleState

Source§

impl State for FocusScopeState

Source§

impl State for FocusState

Source§

impl State for FocusTraversalGroupState

Source§

impl State for FocusableActionDetectorState

Source§

impl State for FormState

Source§

impl State for GlowingOverscrollIndicatorState

Source§

impl State for HeroState

Source§

impl State for ImageState

Source§

impl State for LocalizationsState

Source§

impl State for MultiViewAppState

Source§

impl State for NavigatorPopHandlerState

Source§

impl State for NavigatorState

Source§

impl State for OverlayPortalState

Source§

impl State for OverlayState

Source§

impl State for PageViewState

Source§

impl State for PlatformMenuBarState

Source§

impl State for PlatformTrayIconState

Source§

impl State for PopScopeState

Source§

impl State for RawGestureDetectorState

Source§

impl State for RawMenuAnchorGroupState

Source§

impl State for RawMenuAnchorState

Source§

impl State for RawScrollbarState

Source§

impl State for RawTooltipState

Source§

impl State for ReorderableListState

Source§

impl State for RestorationScopeState

Source§

impl State for RootRestorationScopeState

Source§

impl State for ScrollNotificationObserverState

Source§

impl State for ScrollableState

Source§

impl State for SelectableRegionState

Source§

impl State for SelectionContainerState

Source§

impl State for SharedAppDataState

Source§

impl State for ShortcutRegistrarState

Source§

impl State for ShortcutsState

Source§

impl State for SliverReorderableListState

Source§

impl State for StatefulBuilderState

Source§

impl State for StretchingOverscrollIndicatorState

Source§

impl State for SystemContextMenuState

Source§

impl State for TextSelectionGestureDetectorState

Source§

impl State for TickerModeState

Source§

impl State for TitleState

Source§

impl State for ViewState

Source§

impl State for WidgetsAppState

Source§

impl State for WrappingOverlayState

Source§

impl<T: 'static> State for DragTargetState<T>

Source§

impl<T: Clone + 'static> State for FormFieldState<T>

Source§

impl<T: Clone + 'static> State for ValueListenableBuilderState<T>

Source§

impl<T: Clone + PartialEq + 'static, V: ValueListenable<T> + Clone + PartialEq + 'static> State for UndoHistoryState<T, V>

Source§

impl<T: Clone + PartialEq + 'static> State for RadioGroupState<T>

Source§

impl<T: Clone + PartialEq + 'static> State for RawAutocompleteState<T>

Source§

impl<T: Clone + PartialEq + 'static> State for RawRadioState<T>

Source§

impl<W: AnimatedWidget> State for AnimatedState<W>

Source§

impl<W: DraggableWidget> State for DraggableState<W>

Source§

impl<W: StatusTransitionWidget> State for StatusTransitionState<W>