Stateless vs Stateful Widgets in Flutter
Stateless vs Stateful Widgets in Flutter: Understanding the Difference
Flutter is a powerful framework for building cross-platform mobile applications, and one of its core concepts revolves around widgets. If you're new to Flutter, you may have encountered the terms StatelessWidget and StatefulWidget. But what exactly do they mean, and when should you use each? Let's explore these fundamental building blocks of Flutter UI development.
What is a StatelessWidget?
A StatelessWidget is an immutable widget, meaning its properties cannot change after it is built. It only depends on the data passed to it at the time of creation. If the widget needs to update, a new instance must be created.
Example of a StatelessWidget:
import 'package:flutter/material.dart';
class MyStatelessWidget extends StatelessWidget {
final String title;
MyStatelessWidget({required this.title});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(
child: Text('This is a StatelessWidget'),
),
);
}
}
When to Use StatelessWidget:
When the widget does not require dynamic updates.
For UI elements like text, icons, images, and static layouts.
When the UI is only dependent on external parameters (like API responses or user input) without internal state changes.
What is a StatefulWidget?
A StatefulWidget can change its internal state during its lifetime. It consists of two classes:
A StatefulWidget class, which is immutable.
A State class, which holds mutable state and manages UI updates.
Example of a StatefulWidget:
import 'package:flutter/material.dart';
class MyStatefulWidget extends StatefulWidget {
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int counter = 0;
void incrementCounter() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Stateful Widget Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Counter: \$counter', style: TextStyle(fontSize: 24)),
ElevatedButton(
onPressed: incrementCounter,
child: Text('Increase Counter'),
),
],
),
),
);
}
}
When to Use StatefulWidget:
When the widget needs to update dynamically (e.g., user interactions, animations, timers, etc.).
For elements like forms, counters, checkboxes, sliders, and real-time UI updates.
When handling network calls or local state changes (e.g., toggling dark mode).
Key Differences Between Stateless and Stateful Widgets
Feature | StatelessWidget | StatefulWidget |
Mutability | Immutable | Mutable |
State Management | No internal state | Manages its state |
Performance | More efficient | Slightly heavier |
Rebuilds | Rebuilds only when parent changes | Can rebuild independently |
Example Use Cases | Static UI, icons, text, images | Buttons, forms, animations, real-time updates |
Choosing the Right Widget
If your UI does not change once built → StatelessWidget ✅
If your UI needs updates based on user interaction or internal logic → StatefulWidget ✅
Conclusion
Understanding StatelessWidget vs StatefulWidget is crucial in Flutter development. If performance and efficiency are your goals, always prefer StatelessWidget unless state management is necessary. StatefulWidget is great for dynamic UIs that respond to user input and real-time changes.