Flutter is a popular open-source framework for building mobile applications. It offers a variety of widgets that can be used to create beautiful and functional user interfaces. Among these widgets are stateful and stateless widgets, which are the building blocks of most Flutter apps. In this blog post, we’ll take a closer look at the basics of stateful and stateless widgets in Flutter.
1. Stateless Widgets
Stateless widgets are widgets that do not change their state during the lifetime of the widget. They are immutable and cannot be updated once they are created. This means that their properties are set when they are created and do not change until the widget is destroyed.
Stateless widgets are used for creating UI components that do not require any dynamic behavior or data changes. For example, a text label or an icon can be implemented using a stateless widget.
The following is an example of a stateless widget that displays a text label:
class TextLabel extends StatelessWidget {
final String text;
TextLabel({required this.text});
@override
Widget build(BuildContext context) {
return Text(text);
}
}
As you can see, the TextLabel
widget takes a text
parameter in its constructor and returns a Text
widget in its build
method. Since the TextLabel
widget does not have any state, it can be used in multiple places in the app without causing any issues.
2. Stateful Widgets
Stateful widgets, on the other hand, are widgets that can change their state during the lifetime of the widget. They are mutable and can be updated when required. This means that their properties can change during runtime, and the widget can be updated accordingly.
Stateful widgets are used for creating UI components that require dynamic behavior or data changes. For example, a user interface element that changes its state based on user input can be implemented using a stateful widget.
The following is an example of a stateful widget that displays a counter and increments it when a button is pressed:
class CounterWidget extends StatefulWidget {
@override
_CounterWidgetState createState() => _CounterWidgetState();
}
class _CounterWidgetState extends State<CounterWidget> {
int counter = 0;
void _incrementCounter() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Text('Counter: $counter'),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment'),
),
],
);
}
}
As you can see, the CounterWidget
widget extends the StatefulWidget
class and returns a Column
widget in its build
method. The widget also has a private _CounterWidgetState
class, which is where the widget’s state is stored.
The _incrementCounter
method updates the state of the widget by calling the setState
method, which triggers a rebuild of the widget. This causes the CounterWidget
to display the updated value of the counter.
Conclusion
In conclusion, stateful and stateless widgets are the building blocks of most Flutter apps. Stateless widgets are immutable and do not change their state during the lifetime of the widget, while stateful widgets are mutable and can change their state during runtime. Understanding the basics of stateful and stateless widgets is essential for creating dynamic and responsive user interfaces in Flutter.