Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Stateless and Stateful Widgets | Core UI Building
Flutter App Foundations

Stateless and Stateful Widgets

Svep för att visa menyn

Note
Definition

A stateless widget is a widget that does not store mutable state.

Understanding stateless widgets is essential for building efficient user interfaces in Flutter. You should use a stateless widget when the part of the UI you are building does not depend on any data that might change during the lifetime of the widget. If your widget only displays static information or reacts only to external changes coming from its parent, a stateless widget is the right choice. This approach helps keep your code simple and performant.

main.dart

main.dart

While stateless widgets are great for static content, many apps need to update the UI in response to user actions or other events. This is where stateful widgets come in. A stateful widget can store mutable state, and when that state changes, the widget rebuilds itself to reflect the new data. Stateful widgets have a lifecycle: they are created, can update their state, and are eventually disposed of when no longer needed. This makes them ideal for features like counters, forms, or anything that changes over time within the widget itself.

main.dart

main.dart

Note
Note

Prefer stateless widgets when possible for better performance. Use stateful widgets only when your UI needs to update in response to internal state changes.

question mark

When should you use a StatefulWidget instead of a StatelessWidget?

Vänligen välj det korrekta svaret

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 3. Kapitel 1
some-alt