Before beginning development with Flutter, one should familiar with Widgets like Stateless and Stateful. Today we will talk on Stateless Widget.

Stateless Widget

As the name suggests, Stateless widgets are immutable.

Stateless widgets remain the same even if the user interacts with them. This kind of widget has no state, so they can’t change according to an internal state. They can only react to higher widget changes.

Stateless widgets cannot change their state during the runtime of the app, which means the widgets cannot be redrawn while the app is in action.

Outline of Stateless Widget

To build a stateless widget, we will extend the StatelessWidget abstract class. The structure of a Stateless widget looks like this:

Here, MyHome class is extending StatelessWidget. Every Stateless widget class have to implement an overridden method called build(BuildContext context). Build method returns another Widget.

The build(…) function of the StateLessWidget is called only ONCE. To redraw the StatelessWidget, we need to create a new instance of the Widget.

Example of Stateless Widget

To keep things simple, I’m creating a new Flutter project.  The directory formation looks like :

Project Directory
Project Directory

It contains a Scaffold widget, an AppBar widget, and a couple of Text widgets that display text as “Hello World”.

Stateless Widget

That’s it for now in Stateless Widget, Keep Learning and Sharing. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *