Sub topic 8
Choosing Between Stateless and Stateful
Assume you know basic Dart classes, constructors, named parameters, and null safety. You also have a working Flutter app. This chapter focuses on deciding where changing data should live.
The decision
Use a StatelessWidget when the widget can render its UI from values supplied by its constructor. A stateless widget has no mutable state of its own.
Use a StatefulWidget when the widget must own changing data during its lifetime. Examples include a selected tab, an animation controller, a text field controller, or a counter that changes after a tap.
“Stateless” does not mean the widget can never change on screen. Its parent can rebuild it with different constructor values. It means the widget does not store the changing value itself.
| Question | Choose |
|---|---|
| Can the UI be calculated from constructor values? | `StatelessWidget` |
| Does this widget own changing data or a resource? | `StatefulWidget` |
| Does another widget already own the data? | Usually `StatelessWidget` |
A simple rule
Ask: Who should be responsible for this value?
A favorite button needs to display whether an item is liked. If the screen owns that boolean, the button should usually be stateless. The screen passes isFavorite down and receives an onToggle callback.
A counter that manages its own number can be stateful:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: CounterPage()));
}
class CounterPage extends StatelessWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: CounterPanel()),
);
}
}
class CounterPanel extends StatefulWidget {
const CounterPanel({super.key});
@override
State<CounterPanel> createState() => _CounterPanelState();
}
class _CounterPanelState extends State<CounterPanel> {
int _count = 0;
void _increment() {
setState(() {
_count++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Count: $_count'),
ElevatedButton(
onPressed: _increment,
child: const Text('Add'),
),
],
);
}
}CounterPage is stateless because it only composes the screen. CounterPanel is stateful because it owns _count. Calling setState tells Flutter that the state changed and that the widget should build again.
Prefer state ownership that matches the data
Keep state local when no other widget needs it. A temporary menu selection or an animation progress value usually belongs inside the widget that uses it.
Move state to a parent when multiple children need the same value. This pattern is called lifting state up. The parent owns the value, passes data down through constructor parameters, and passes events up through callbacks.
Common mistakes
Do not choose StatefulWidget merely because a widget rebuilds. Any widget can rebuild when its parent changes.
Do not change a field without setState and expect the screen to update. Flutter needs the notification to schedule a rebuild.
Do not initialize changing values in build. That method can run many times and would reset the value. Initialize long-lived resources in initState, and release them in dispose.
Exercise
A child displays a loading spinner based on a boolean owned by its parent. Which widget type should the child use, and how should the parent communicate the value?
Show answer
The child should usually be a StatelessWidget. The parent should pass the boolean as a constructor argument and rebuild the child when the value changes.