Flutter Widgets: Understanding Stateless and Stateful Widgets

Ajiyemi Michael
3 min readMar 6, 2023

--

In Flutter, widgets are the building blocks of a UI. There are two types of widgets in Flutter: stateless and stateful. In this blog post, we’ll take a closer look at the differences between stateless and stateful widgets, and how to use them in your Flutter app.

Stateless Widgets

A StatelessWidget is a widget that does not have any mutable state. This means that the widget cannot change its appearance based on user interactions or other events. A StatelessWidget is essentially a "dumb" widget that displays static content.

Here’s an example of a StatelessWidget that displays a simple greeting:

class GreetingWidget extends StatelessWidget {
final String name;

GreetingWidget({required this.name});

@override
Widget build(BuildContext context) {
return Text('Hello, $name!');
}
}

In this example, GreetingWidget is a StatelessWidget that takes a name parameter in its constructor. The widget returns a Text widget that displays a greeting with the provided name.

Stateful Widgets

A StatefulWidget is a widget that has mutable state. This means that the widget can change its appearance based on user interactions or other events. A StatefulWidget is essentially a "smart" widget that can react to changes in its state.

Here’s an example of a StatefulWidget that displays a counter:

class CounterWidget extends StatefulWidget {
CounterWidget({Key? key}) : super(key: key);

@override
_CounterWidgetState createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pressed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment Counter'),
),
],
);
}
}

In this example, CounterWidget is a StatefulWidget that displays a counter. The widget has a _counter variable that is updated by the _incrementCounter function when the user presses the "Increment Counter" button. The _incrementCounter function calls setState to update the widget's state, which triggers a rebuild of the widget.

One of the most important aspects of the StatefulWidget is its ability to have access to the setState function.

The setState function is a method that is called on the State object associated with the StatefulWidget. When called, setState tells Flutter to re-render the widget and apply any changes to its appearance based on its new state.

For example, if a user interacts with a StatefulWidget by pressing a button, the setState function can be used to update the widget's state and trigger a rebuild of the widget. This allows the widget to display new content or update its appearance in response to user input.

Without the setState function, StatefulWidget widgets would not be able to respond to user interactions and other events. Therefore, it is a critical feature that makes StatefulWidget widgets so powerful and versatile in Flutter app development.

When to Use Stateless vs. Stateful Widgets

In general, you should use a StatelessWidget when your widget does not need to change its appearance based on user interactions or other events. StatelessWidgets are lightweight and performant, making them a good choice for displaying static content.

On the other hand, you should use a StatefulWidget when your widget needs to change its appearance based on user interactions or other events. StatefulWidgets are more powerful and flexible than StatelessWidgets, but they are also more complex and can be less performant.

Conclusion

In this blog post, we’ve explored the differences between stateless and stateful widgets in Flutter. We’ve looked at examples of each type of widget and discussed when to use each one. By understanding the differences between stateless and stateful widgets, you can build high-quality Flutter apps that are performant, scalable, and easy to maintain.

--

--

Ajiyemi Michael
Ajiyemi Michael

Written by Ajiyemi Michael

i AM a flutter developer, i write because it is my way of giving back to the community, to share my knowledge and experiences with others

Responses (2)