Flutter Stateful and Stateless widgets

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Yesterday we had a high-level overview of how Flutter works, and learned it uses a widget approach.
Today we'll dive a little bit deeper into the two main types of widgets being:
The state, in general, is information about that widget that can be read once the widget is built. If this never changes, we consider it a stateless widget. If this information can change, it's a stateful widget.
Stateless widgets can never change. Some examples of stateless widgets are Icon, Text, and IconButton.
A stateful widget is dynamic. It might change appearance based on user interaction of when data changes.
Some examples might are TextField, Checkbox, and Radio.
Let's showcase a basic stateless widget that will show some text but not do anything.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Text(
'Hello World 👋',
textDirection: TextDirection.ltr,
),
);
}
}
As you can see above, this widget is going to render some text inside a center element. That's it. That fact won't change no matter what happens.
Stateful widgets are a different story. As mentioned, they might change properties based on data or user action.
Let's use the Flutter demo and create a basic application that will plus a counter when we click a button.
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
As you can see, we changed the MyApp to be a stateful widget now.
That means it must implement the createState function, which in our case calls the _MyAppState class.
Note: The underscore classes in Dart are considered private!
This new class will extend our basic app state, meaning it can handle change.
class _MyAppState extends State<MyApp> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
),
TextButton(
onPressed: _incrementCounter,
child: const Text('Add number'),
),
]),
),
);
}
}
In there, we define a basic counter variable and set it to zero. Then we create a function that can increment this number.
Be aware that setState is needed to make an actual change in a state.
Then we override the widget to create our widget as we did before, but in this one, we get to use a variable text and include a button that will handle an onPressed event.
So what happens:
And this keeps looping on every interaction.
I hope you got a good overview of the difference between stateless and stateful widgets and how they come together in Flutter.
You can find my implementation of the Stateful widget on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter