StackLearn

Welcome to the blog of Sarvesh Bhatnagar

Stateless Widget vs Stateful Widget

Flutter: Stateless Widget vs Stateful Widget

Everything in flutter is about widgets. There are two types of widgets in flutter i.e. Stateless widget and Stateful widget. August 4, 2020

In a line, if your widget change appearance use stateful widget and if it doesn't use stateless widget that's all. We know that flutter is all about widgets and in flutter there are 2 types of widgets.

  1. Stateless Widget
  2. Stateful Widget

Stateless Widget

A stateless widget never changes. For example Icon, IconButton, Text and so on. Once they are built they remain the same during the lifetime of the application. There are various places where you might want to use Stateless widget such as your app Title, Images, and so on.

Stateful Widget

A stateful widget is something which has a state so basically that widget needs to be rebuilt whenever some event occurs. A good example would be a toggle button. Whenever you tap on the toggle button its state changes to either on or off. The appearance of the button changes and this happens because the stateful widget asks the build method to rebuild the widget and change its state.

Shortcuts:

  • Shortcut to create a stateless widget : type stless and let the autocomplete complete itself.
  • Shortcut to create a stateful widget : type stful and let the autocomplete complete itself.


Sample Stateless Widget Code:

Sample Stateful Widget Code:


- Sarvesh Bhatnagar

About Me

I am Sarvesh Bhatnagar, I love to code and I enjoy teaching. This website is an effort to help people learn basics of programming. I am starting with teaching App Development using flutter. If you want to contribute to this website and become an author in this website, mail to: contact@sarveshbhatnagar.com