Difference Between Stateless and Stateful Widget in Flutter
Last Updated :
05 Aug, 2021
As we all know the flutter app consists of widgets only, these are broadly classified into two types:
- Stateless Widget
- Stateful Widget
State:
Before knowing the difference between the stateless and stateful widget we should take a look at the definition of State of a widget.
The State is information that can read synchronously when the widget is build and might change during the lifetime of the widget.
In simpler words, the state of the widget is the information of the objects that its properties (parameters) are holding at the time of its creation (when the widget is painted on the screen). The state can also change when it is used for example the color of RaisedButton widget might change when pressed.
Stateless Widget:
Stateless widgets are the widgets that don't change i.e. they are immutable. Its appearance and properties remain unchanged throughout the lifetime of the widget. In simple words, 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.
Examples: Icon, IconButton, and Text are examples of stateless widgets.
To create a Stateless widget, we have to override the build() method as implemented in the code below.
Dart
import 'package:flutter/material.dart'
void main() = > runApp(GeeksforGeeks())
class GeeksforGeeks extends StatelessWidget {
@override Widget build(BuildContext context)
{return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(backgroundColor: Colors.green,
title: Text("GeeksforGeeks"), ),
body: Container(child: Center(child: Text("Stateless Widget"),
),
),
),
)
}}
Output :

Stateful Widget:
Stateful Widgets are the ones that change its properties during run-time. They are dynamic i.e., they are mutable and can be drawn multiple times within its lifetime. It can change its appearance in response to events triggered by user interactions or when it receives data.
Examples : Checkbox, Radio Button, Slider, InkWell, Form, and TextField are examples of Stateful widgets.
To create a Stateful widget, we have to override the createState() method, which returns the state of the widget.
Dart
import 'package:flutter/material.dart'
void main() = > runApp(MyApp())
class MyApp extends StatelessWidget {
@override Widget build(BuildContext context)
{return MaterialApp(theme: ThemeData(
primarySwatch: Colors.green, ),
home: MyHomePage(title: 'GeeksforGeeks'),
)}}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}): super(key: key)
Output :
Differences Between Stateless and Stateful Widget:
Stateless Widget:
- Stateless Widgets are static widgets.
- They do not depend on any data change or any behavior change.
- Stateless Widgets do not have a state, they will be rendered once and will not update themselves, but will only be updated when external data changes.
- For Example: Text, Icon, RaisedButton are Stateless Widgets.
Stateful Widget:
- Stateful Widgets are dynamic widgets.
- They can be updated during runtime based on user action or data change.
- Stateful Widgets have an internal state and can re-render if the input data changes or if Widget's state changes.
- For Example: Checkbox, Radio Button, Slider are Stateful Widgets
Similar Reads
Flutter - Difference Between ListView and List Flutter is Googleâs Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), and Web apps from a single codebase. When building applications with Flutter everything is towards Widgets â the blocks with which the flutter apps are built. They are structural elements that ship with
4 min read
Difference Between Isolates and Compute in Flutter When developing mobile applications with Flutter, it's important to consider performance and responsiveness to ensure a smooth user experience. Two tools that can be used to achieve this are Flutter Isolates and Flutter Compute. Both tools allow for parallel processing, but they have some difference
4 min read
Flutter - Difference Between setState and Provider in State Management In this article, we will look into how we can use state management or provider package for implementing state management in our flutter app. We are creating a simple counter app in flutter, which will help us in understanding state management in flutter application and deciding which is a better wa
5 min read
Flutter - Stateful vs Stateless Widgets The state of an app can very simply be defined as anything that exists in the memory of the app while the app is running. This includes all the widgets that maintain the UI of the app including the buttons, text fonts, icons, animations, etc. So now that we know what are these states let's dive dire
6 min read
Flutter - Widget Tree and Element Tree As we know, the user interface of an application when it is designed in Flutter contains a set of widgets. So that means the whole application is set to be made out of widgets. A widget describes what the view of the app would look like in the given state. Now, as the user interface contains severa
4 min read