Flutter - Stateful vs Stateless Widgets
Last Updated :
14 Apr, 2025
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 directly into our main topic i.e. what are these stateful and stateless widgets and how do they differ from one another.
What is a State?
The State is the information that can be read synchronously when the widget is built and might change during the lifetime of the widget.
In other words, the state of the widget is the data of the objects that its properties (parameters) are sustaining 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 when a CheckBox widget is clicked a check appears on the box.
Flutter Stateless Widgets
- The widgets whose state can not be altered once they are built are called stateless widgets.
- These widgets are immutable once they are built i.e. any amount of change in the variables, icons, buttons, or retrieving data can not change the state of the app.
Below is the basic structure of a stateless widget. Stateless widget overrides the build() method and returns a widget. For example, we use Text or the Icon in our Flutter application where the state of the widget does not change in the runtime. It is used when the UI depends on the information within the object itself. Other examples can be Text, ElevatedButton, IconButtons.
Example of Stateless Widgets
main.dart:
Dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container();
}
}
So let us see what this small code snippet tells us. The name of the stateless widget is MyApp which is being called from the runApp() and extends a stateless widget. Inside this MyApp a build function is overridden and takes BuildContext as a parameter. This BuildContext is unique to each and every widget as it is used to locate the widget inside the widget tree.
Note: The widgets of a Flutter application are displayed in the form of a Widget Tree where we connect the parent and child widgets to show a relationship between them which then combines to form the state of your app.
The build function contains a container which is again a widget of Flutter inside which we will design the UI of the app. In the stateless widget, the build function is called only once which makes the UI of the screen.
Example: Stateless Widget
main.dart:
Dart
import 'package:flutter/material.dart';
// Main function to run the app
void main() => runApp(const MyApp());
// MyApp is a stateless widget
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// Build method to create the widget tree
@override
Widget build(BuildContext context) {
return MaterialApp(
// Disable debug banner
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
leading: const Icon(
Icons.menu,
// Icon color
color: Colors.white,
),
// Menu icon on the left
// Green background color for AppBar
backgroundColor: Colors.green,
title: const Text(
// Title text
"GeeksforGeeks",
// Text style
style: TextStyle(color: Colors.white),
),
), // AppBar
body: const Center(
child: Text(
// Center text
"Stateless Widget",
// Text style
style: TextStyle(color: Colors.black, fontSize: 30),
),
), // Center
), // Scaffold
); // MaterialApp
}
}
Output:
Flutter Stateful Widgets
- The widgets whose state can be altered once they are built are called stateful Widgets.
- These states are mutable and can be changed multiple times in their lifetime.
- This simply means the state of an app can change multiple times with different sets of variables, inputs, data.
Below is the basic structure of a stateful widget. Stateful widget overrides the createState() method and returns a State. It is used when the UI can change dynamically. Some examples can be CheckBox, RadioButton, Form, TextField.
Classes that inherit "Stateful Widget" are immutable. But the State is mutable which changes in the runtime when the user interacts with it.
Example:
main.dart:
Dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
// ignore: library_private_types_in_public_api
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Container();
}
}
So let us see what we have in this code snippet. The name of the Stateful Widget is MyApp which is called from the runApp() and extends a stateful widget. In the MyApp class, we override the create state function. This createState() function is used to create a mutable state for this widget at a given location in the tree. This method returns an instance for the respected state subclass. The other class which is _MyAppState extends the state, which manages all the changes in the widget. Inside this class, the build function is overridden which takes the BuildContext as the parameter. This build function returns a widget where we design the UI of the app. Since it is a stateful widget the build function is called many times which creates the entire UI once again with all the changes.
Example: Stateful Widget
This is a simple counter app , which can increase the number in the center dynamically, when the user taps on the FloatingActionButton.
main.dart:
Dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
// MyApp is the root widget of the application
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
// HomePage is the main screen of the app
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
// Variable to store the counter value
int _counter = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: const Icon(
Icons.menu,
color: Colors.white,
), // Set the leading icon
// Set the background color of the app bar
backgroundColor: Colors.green,
// Set the title of the app bar
title: const Text(
"GeeksforGeeks",
style: TextStyle(
// Set the text color
color: Colors.white,
),
),
),
// The main body of the scaffold
body: Center(
// Display a centered text widget
child: Text(
"$_counter",
// Apply text styling
style: const TextStyle(
// Set font size
fontSize: 24,
// Set font weight
fontWeight: FontWeight.bold,
),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.green,
onPressed: () {
// Increment the counter value by 1 using setState
setState(() {
_counter++;
});
},
child: const Icon(
Icons.add,
color: Colors.white,
),
),
);
}
}
Output:
Stateless widget is useful when the part of the user interface you are describing does not depend on anything other than the configuration information and the BuildContext whereas a Stateful widget is useful when the part of the user interface you are describing can change dynamically.
Similar Reads
Flutter Tutorial This Flutter Tutorial is specifically designed for beginners and experienced professionals. It covers both the basics and advanced concepts of the Flutter framework.Flutter is Googleâs mobile SDK that builds native Android and iOS apps from a single codebase. It was developed in December 2017. When
7 min read
Top 50 Flutter Interview Questions and Answers for 2025 Flutter is an open-source, cross-platform application development framework. It was developed by Google in 2017. It is used to build applications for Android, iOS, Linux, Mac, Windows, and the web. Flutter uses the Dart programming language. It provides a simple, powerful, efficient, and easy-to-und
15+ min read
What is Widgets in Flutter? Flutter is Google's UI toolkit for crafting beautiful, natively compiled iOS and Android apps from a single code base. To build any application we start with widgets - The building block of Flutter applications. Widgets describe what their view should look like given their current configuration and
5 min read
Flutter | An introduction to the open source SDK by Google 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 Widgets â the blocks with which the flutter apps are built. They are structural elements that ship with a bunch
5 min read
Android Studio Setup for Flutter Development This article will show how to set up Android Studio to run Flutter Applications. Android Studio is one of the popular IDE( integrated development environment  ) developed by Google itself to create cross-platform Android applications. First, you have to install Android Studio version 3.0 or later, a
3 min read
10 Best Flutter Projects with Source Code in 2025 Are you eager to begin your journey into Flutter app development but find yourself unsure of where to start? Look no further! This article serves as a comprehensive guide for aspiring developers, offering a wide range of innovative Flutter project ideas. Whether you're looking to refine your skills
7 min read
Flutter - Architecture Application Flutter architecture application mainly consists of: WidgetsGesturesConcept of StateLayersWidgetsWidgets are the primary component of any flutter application. It acts as a UI for the user to interact with the application. Any flutter application is itself a widget that is made up of a combination of
3 min read
Flutter - Changing App Icon Flutter SDK is an open-source software development kit for building beautiful UI which is natively compiled. When we create a Flutter Project, it comes with the default Flutter icon. In order to get the app published in stores like Google Play Store, Apple App Store, etc the default icon can be chan
3 min read
Flutter - AppBar Widget AppBar is usually the topmost component of the app (or sometimes the bottom-most), it contains the toolbar and some other common action buttons. As all the components in a Flutter application are a widget or a combination of widgets. So AppBar is also a built-in class or widget in Flutter which give
7 min read
Scaffold class in Flutter with Examples The Scaffold is a class in flutter that provides many widgets or we can say APIs. The Scaffold will expand or occupy the whole available space in device screen .The class Hierarchy is as follows:Object â³ Diagnosticable â³ Diagnosticable Tree â³ Widget â³ StateFul Widget â³ ScaffoldThe constructor of the
8 min read