Explain it simply
Stateless vs Stateful, in plain language
Put simply, Flutter widgets can be split into ones that don't change and ones that can. StatelessWidget is used for UI whose data/state never changes, while StatefulWidget is used when the UI needs to change in response to things like user actions, timers, or API data.
Problem → Why → Solution
The problem — why this concept matters
If you change a state variable without calling setState(), the value may update, but the UI often won't re-render.
A common mistake
Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.
Core concept — understand how it works
Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.
- event
- state owner and notifier
- rebuilt widgets
Explain the widget, state, and lifecycle behavior of Stateless vs Stateful
Core syntax or pattern
class GreetingText extends StatelessWidget {
const GreetingText({super.key});
@override
Widget build(BuildContext context) {
return const Text('Hello Flutter');
}
}
class TapCounter extends StatefulWidget {
const TapCounter({super.key});
@override
State<TapCounter> createState() => _TapCounterState();
}
class _TapCounterState extends State<TapCounter> {
int taps = 0;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
setState(() {
taps++;
});
},
child: Text('Tapped $taps times'),
);
}
}Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.
Read the code step by step
- event
class GreetingText extends StatelessWidget {Put simply, Flutter widgets can be split into ones that don't change and ones that can. StatelessWidget is used for UI whose data/state never changes, while StatefulWidget is used when the UI needs to change in response to things like user actions, timers, or API data.
- state owner and notifier
int taps = 0;Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.
- rebuilt widgets
}Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
- event
- state owner and notifier
- rebuilt widgets
Success
Explain the widget, state, and lifecycle behavior of Stateless vs Stateful
Failure
If you change a state variable without calling setState(), the value may update, but the UI often won't re-render.
Put simply, Flutter widgets can be split into ones that don't change and ones that can. StatelessWidget is used for UI whose data/state never changes, while StatefulWidget is used when the UI needs to change in response to things like user actions, timers, or API data.
class GreetingText extends StatelessWidget {
const GreetingText({super.key});
@override
Widget build(BuildContext context) {
return const Text('Hello Flutter');
}
}
class TapCounter extends StatefulWidget {
const TapCounter({super.key});
@override
State<TapCounter> createState() => _TapCounterState();
}
class _TapCounterState extends State<TapCounter> {
int taps = 0;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
setState(() {
taps++;
});
},
child: Text('Tapped $taps times'),
);
}
}GreetingText shows unchanging text, and each time you tap the TapCounter button, the count goes up.What's Next
In the Basic Widgets lesson, get hands-on with the UI widgets you'll use the most.
Real-world usage
How to apply it in a real project
Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Mini challenge
Build it yourself
Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
class GreetingText extends StatelessWidget {
const GreetingText({super.key});
@override
Widget build(BuildContext context) {
return const Text('Hello Flutter');
}
}
class TapCounter extends StatefulWidget {
const TapCounter({super.key});
@override
State<TapCounter> createState() => _TapCounterState();
}
class _TapCounterState extends State<TapCounter> {
int taps = 0;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
setState(() {
taps++;
});
},
child: Text('Tapped $taps times'),
);
}
}Expected: Explain the widget, state, and lifecycle behavior of Stateless vs Stateful
Show hint
Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- event → state owner and notifier → rebuilt widgets
- Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.
- If you change a state variable without calling setState(), the value may update, but the UI often won't re-render.
- Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.
Flutter documentation — Flutter