Sub topic 5
Extract widget classes instead of helper methods
This chapter assumes you know basic Dart classes, StatelessWidget, StatefulWidget, setState, and the build method. The goal is to make rebuild boundaries visible and predictable when a screen grows.
Why extraction changes rebuild behavior
A helper method that returns a widget is still called by its parent’s build method. When the parent rebuilds, the helper runs again and creates its widget subtree again.
An extracted widget class gives Flutter a separate element in the widget tree. The parent can rebuild while the extracted child keeps its existing element and state. Flutter still compares the new widget configuration with the previous one, but the child has a clearer boundary for state, dependencies, and future optimization.
The difference is easiest to see when only one part of a screen changes:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ProfileScreen()));
}
class ProfileScreen extends StatefulWidget {
const ProfileScreen({super.key});
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
int _likes = 0;
Widget buildHeader() {
return const Text(
'Profile',
style: TextStyle(fontSize: 28),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Example')),
body: Column(
children: [
buildHeader(),
Text('Likes: $_likes'),
ElevatedButton(
onPressed: () => setState(() => _likes++),
child: const Text('Like'),
),
],
),
);
}
}Every tap calls the parent’s setState. The parent runs its entire build method again, including buildHeader. The header is small, so this may be perfectly acceptable. Extraction is not automatically a performance fix.
Now make the header a widget class:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ProfileScreen()));
}
class ProfileScreen extends StatefulWidget {
const ProfileScreen({super.key});
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
int _likes = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Example')),
body: Column(
children: [
const ProfileHeader(),
Text('Likes: $_likes'),
ElevatedButton(
onPressed: () => setState(() => _likes++),
child: const Text('Like'),
),
],
),
);
}
}
class ProfileHeader extends StatelessWidget {
const ProfileHeader({super.key});
@override
Widget build(BuildContext context) {
return const Text(
'Profile',
style: TextStyle(fontSize: 28),
);
}
}The ProfileHeader class has its own element. Because it has a const constructor and no changing inputs, the same widget instance can be reused. The parent still rebuilds, but Flutter does not need to rebuild the unchanged header subtree.
When to choose a widget class
Use an extracted widget class when at least one of these is true:
- The part has its own state or should own state.
- The part has a clear name and responsibility.
- The part is large enough to make the parent difficult to read.
- The part has dependencies that should be limited to its own subtree.
- The part is reused in more than one place.
- You want a const constructor or a stable rebuild boundary.
A helper method can be useful for a tiny, local fragment that has no state and no expensive work. Do not split every line into a class. The useful boundary is usually a visual component or a unit of behavior.
A practical extraction pattern
When extracting a widget, pass changing values through constructor parameters. Keep the child’s inputs explicit.
class LikeSummary extends StatelessWidget {
const LikeSummary({
required this.likes,
super.key,
});
final int likes;
@override
Widget build(BuildContext context) {
return Text('Likes: $likes');
}
}The parent owns the changing count, while LikeSummary only displays it. This separation makes it easier to move the count into a smaller StatefulWidget later if the button and count should update independently of the rest of the screen.
Exercise
When is a helper method acceptable instead of an extracted widget class?
Show answer
It is acceptable for a small, local, stateless fragment when the parent rebuild cost is already cheap and no separate state or dependency boundary is needed.