It could get confusing sometimes not knowing when to use the Flutter Stateless widget and when to use the Stateful widget. You’ll get a better understanding after going through this article to the end.
So far we’ve been going out of our way to create stateless widgets and stateful widgets. So you probably guessed that there are two widget types you can create. You were right. A
stateless widget is one that doesn’t maintain its own state. A
stateful widget does.
“State” in this context refers to data within the widget that can change during its lifetime. Let’s say we create a
Person widget (stateless). If it’s a widget that just displays the person’s information, it should be stateless. But if it is a person
maintenance widget where we allow the user to change the data by typing into a TextField, then we’d need a StatefulWidget. I hope you get that?
Now the question is “which one should I create?”
The short answer is create a stateless widget. Never use a stateful widget until you must. Assume all widgets you make will be stateless and start them out that way. Refactor them into stateful widgets when you’re sure you really do need state. But recognize that state can be avoided more often than developers think. Avoid it when you can to make widgets simpler and therefore easier to write, to maintain, and to extend. Your team members will thank you for it. 😉
Also Note That:
There is actually a third type of widget, the InheritedWidget. You set a value in your InheritedWidget and any descendent can reach back up through the tree and ask for that data directly. It is kind of an advanced topic. The video below should do some explanation
So now we know that Flutter apps are all about widgets. You’ll compose your own custom Stateless or Stateful widgets that have a build method which will render a tree of built-in Flutter widgets. So clearly we need to know about the builtin Flutter widgets.