Sub topic 3
Push state down: limit the scope of `setState`
Assume you know basic Dart classes, StatelessWidget, StatefulWidget, setState, and the build method. This chapter focuses on deciding where changing state should live.
What `setState` marks for rebuilding
setState tells Flutter that a State object has changed. Flutter marks that state's element as dirty and calls its build method again during the next frame.
The rebuild starts at the widget associated with that State. Therefore, state placed high in the widget tree can cause a large part of a screen to be rebuilt:
ProfileScreen
├── ExpensiveHeader
├── LikeCounter
└── ExpensiveFooterIf ProfileScreen owns the like count, tapping the button calls ProfileScreen's setState. Flutter calls the whole ProfileScreen.build method again. The header and footer may not need new data, but they are inside that build method.
A rebuild is not automatically a performance problem. Flutter can reuse unchanged widget instances, and a simple build method is usually cheap. The problem appears when a frequently changing value sits above expensive layouts, animations, lists, or complex dependencies.
Push frequently changing state downward
A useful rule is to keep state as close as possible to the widgets that read it. This is called pushing state down. It reduces the number of widgets that depend on a change and makes the ownership of that state clearer.
The following complete example keeps the like count inside LikeCounter, rather than inside the entire screen:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: ProfileScreen(),
);
}
}
class ProfileScreen extends StatelessWidget {
const ProfileScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: const Padding(
padding: EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ExpensiveHeader(),
SizedBox(height: 24),
LikeCounter(),
SizedBox(height: 24),
ExpensiveFooter(),
],
),
),
);
}
}
class LikeCounter extends StatefulWidget {
const LikeCounter({super.key});
@override
State<LikeCounter> createState() => _LikeCounterState();
}
class _LikeCounterState extends State<LikeCounter> {
int _likes = 0;
@override
Widget build(BuildContext context) {
return Row(
children: [
Text('Likes: $_likes'),
const SizedBox(width: 12),
IconButton(
tooltip: 'Like',
icon: const Icon(Icons.favorite),
onPressed: () => setState(() => _likes++),
),
],
);
}
}
class ExpensiveHeader extends StatelessWidget {
const ExpensiveHeader({super.key});
@override
Widget build(BuildContext context) {
return const Text(
'Profile',
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
);
}
}
class ExpensiveFooter extends StatelessWidget {
const ExpensiveFooter({super.key});
@override
Widget build(BuildContext context) {
return const Text('Member since 2024');
}
}Here, tapping the button marks _LikeCounterState dirty. Flutter calls only LikeCounter's build method because the screen itself does not own the changing value.
This pattern works when the state has one clear owner. If several distant widgets need the same value, moving it too far down can create duplicated state or awkward callbacks. In that case, use a shared state solution, but still make each consumer listen only to the data it needs.
Decide whether a rebuild matters
Before extracting widgets, measure the behavior. A small setState scope is useful when the rebuilt subtree does meaningful work or updates often. It is usually unnecessary for a screen containing a few Text widgets.
When you find a costly update, move the stateful boundary downward, run the same interaction again, and compare the result. Keep the change only if it improves frame time, reduces unnecessary work, or makes ownership clearer.
Exercise
A screen has a search field and a static settings panel. Typing into the field calls setState on the screen and rebuilds both parts. Where should the search state usually live?
Show answer
Put the text and filtering state in a small StatefulWidget that owns the search field and its results. Keep the settings panel outside that widget so typing does not rebuild it.