r/learnprogramming • u/ConsistentMood2020 • 2h ago
Tutorial Flutter tip: split giant build() methods into small widgets — it cuts rebuilds more than you'd expect
Something that helped me a lot when my Flutter screens started feeling sluggish:
When you call setState, Flutter rebuilds everything inside that widget's build() method. If your build() is one giant method with dozens of widgets, all of them rebuild — even the parts that never change.
Two habits that fixed most of it for me:
- Split the UI into small widget classes (not helper methods that return widgets — separate StatelessWidget classes). Flutter can then skip rebuilding the ones whose inputs didn't change.
- Mark widgets const wherever possible (const Text(...), const Icon(...)). A const widget never rebuilds.
Small example:
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
appBar: AppBar(title: Text('My App')),
body: MyExpensiveList(),
);
}
}
On one screen I moved a static header into its own const widget and the frame drops on scroll went away. Keep build() methods short, extract widgets early, and use const by default.
1
Upvotes