Skip to main content

StatefulWidget

Trait StatefulWidget 

Source
pub trait StatefulWidget {
    type State;

    // Required method
    fn render(&self, area: Rect, frame: &mut Frame<'_>, state: &mut Self::State);
}
Expand description

A widget that renders based on mutable state.

Use StatefulWidget when the widget needs to:

  • Update scroll position during render
  • Track selection state
  • Cache computed layout information
  • Synchronize view with external model

§Example

pub struct ListState {
    pub selected: Option<usize>,
    pub offset: usize,
}

impl StatefulWidget for List<'_> {
    type State = ListState;

    fn render(&self, area: Rect, frame: &mut Frame, state: &mut Self::State) {
        // Adjust offset to keep selection visible
        if let Some(sel) = state.selected {
            if sel < state.offset {
                state.offset = sel;
            }
        }
        // Render items starting from offset...
    }
}

§Stateful vs Stateless

Prefer stateless Widget when possible. Use StatefulWidget only when the render pass genuinely needs to modify state (e.g., scroll adjustment).

Required Associated Types§

Source

type State

The state type associated with this widget.

Required Methods§

Source

fn render(&self, area: Rect, frame: &mut Frame<'_>, state: &mut Self::State)

Render the widget into the frame, potentially modifying state.

State modifications should be limited to:

  • Scroll offset adjustments
  • Selection clamping
  • Layout caching

Implementors§

Source§

impl StatefulWidget for FilePicker

Source§

impl StatefulWidget for Help

Source§

impl StatefulWidget for LogViewer

Source§

impl StatefulWidget for Dialog

Source§

impl StatefulWidget for Stopwatch<'_>

Source§

impl StatefulWidget for TextArea

Source§

impl StatefulWidget for Timer<'_>

Source§

impl StatefulWidget for ValidationErrorDisplay

Source§

impl<'a> StatefulWidget for List<'a>

Source§

impl<'a> StatefulWidget for Scrollbar<'a>

Source§

impl<'a> StatefulWidget for Spinner<'a>

Source§

impl<'a> StatefulWidget for Table<'a>

Source§

impl<T: RenderItem> StatefulWidget for VirtualizedList<'_, T>

Source§

impl<W: StatefulWidget + Widget> StatefulWidget for Budgeted<W>

Source§

impl<W: StatefulWidget> StatefulWidget for Align<W>

Source§

impl<W: StatefulWidget> StatefulWidget for Padding<W>

Source§

impl<W: Widget> StatefulWidget for CustomErrorBoundary<W>

Source§

impl<W: Widget> StatefulWidget for ErrorBoundary<W>

Source§

impl<W: Widget, K: CacheKey<W>> StatefulWidget for CachedWidget<W, K>