Flutter – Stateful Widget
Last Updated :
23 Apr, 2025
A Stateful Widget has states in it. To understand a Stateful Widget, you need to have a clear understanding of widgets and state management. A state can be defined as “an imperative changing of the user interface,” and a widget is “an immutable description of the part of the user interface”. To learn more about them, refer to the articles below.
Based on states, widgets are divided into 2 categories:
- Stateless Widget
- Stateful Widget
In this article, we will walk you through the Stateful Widgets.
A Stateful Widget has its mutable state that it needs to track. It is modified according to the user’s input. A Stateful Widget looks after two things primarily: the changed state based on its previous state and an updated view of the user interface. A track of the previous state value has to be looked at because there is a need to rebuild the widget to show the new changes made to your application. A Stateful Widget triggers a build method for creating its children widgets, and the subclass of the state holds the related data. It is often used in cases where redrawing of a widget is needed. A Stateful Widget can change when:
- There is a User Input included
- There are some User Interaction
- There are some Dynamic Changes
Given below is the basic structure of a Stateful Widget:
Dart
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Container();
}
}
The MyApp is a class that extends Stateful Widget. A generic method, createState(), rebuilds a mutable state for the widget at a particular location and returns another class that is extended by the previous state. The _MyAppState class extends another state.
There are various methods provided by the Stateful class to work with:
1. BuildContext: It provides information regarding which widget is to be built/rebuilt and where it will be located after rebuilding. Thus, BuildContext is the widget associated with the state.
Widget build(BuildContext context) {
return Container();
}
2. SetState(): A State object is used to modify the user interface. It executes the code for a particular callback and repaints the widgets that rely on that state for configuration.
setState(setState(() {
});
3. initState(): This method is the entry point of a widget. The initState() method initializes all the methods that the build method will depend upon. This is called only once in a widget’s lifetime and mostly overridden the rest of the time.
initState() {
//...
super.init();
}
4. didChangeDependencies(): It is used for loading the dependencies required for the execution of a state. The didChangeDependencies() is called immediately after the initState() is called for the first time and before the triggering of the build method.
void didChangeDependencies() {
}
5. dispose(): This method is used for removing an object permanently from the widget tree. It is used when we need to clear up the memory by invoking super.dispose().
void dispose(){
//...
super.dispose();
}
Given below is an example of the Stateful Widget.
Example
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
foregroundColor: Colors.white,
title: const Text('GeeksforGeeks'),
),
body: const FirstScreen(),
),
);
}
}
class FirstScreen extends StatelessWidget {
const FirstScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black,
foregroundColor: Colors.white,
),
onPressed:
() => Navigator.of(context).push(
MaterialPageRoute(builder: (context) => const NewScreen()),
),
child: const Text(
'Move to next screen',
style: TextStyle(color: Colors.white),
),
),
),
);
}
}
class NewScreen extends StatefulWidget {
const NewScreen({Key? key}) : super(key: key);
@override
State<NewScreen> createState() => _NewScreenState();
}
class _NewScreenState extends State<NewScreen> {
TextEditingController textEditingController = TextEditingController();
@override
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('GeeksforGeeks '),
backgroundColor: Colors.black,
foregroundColor: Colors.white,
),
body: Container(
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Container(child: Text('User Interface Changed!!')),
),
),
),
);
}
}
Output:
In the given example, the user interface changes, and the control shifts to the next screen as soon as the central button is clicked, i.e. State of the object is changed. This is exactly what a Stateful Widget is used for.
Similar Reads
Flutter - Stateless Widget
Stateless Widget is something that does not have a state. To understand a Stateless Widget, you need to clearly understand widgets and states. A state can be defined as "an imperative changing of the user interface," and a widget is "an immutable description of the part of the user interface". To le
4 min read
Flutter - Stepper Widget
In this article, we will learn about the Stepper widget in Flutter. A stepper widget displays progress through a sequence of steps. Stepper is generally used in filling forms online. For example, remember filling an online form for applying to any university or passport or driving license. We filled
8 min read
Flutter - TabView Widget
There are lots of apps where you often have come across tabs. Tabs are a common pattern in the apps. They are situated at top of the app below the App bar. So today we are going to create our own app with tabs. Table of Contents:Project SetupCodeConclusionProject Setup: You can either create a new p
4 min read
OffStage Widget in Flutter
Flutter provides an inbuilt widget called âOffstageâ, which is been used to hide or show any widget programmatically depending on user action/event. Offstage Widget is a widget that lays the child out as if it was in the tree, but without painting anything, without making the child available for hit
4 min read
Flutter - FlutterLogo Widget
FlutterLogo widget is as simple as it sounds, it is just the flutter logo in the form of an icon. This widget also comes built-in with Flutter SDK. This widget can found its use as a placeholder for an image or icon. Below we will see its implementation with all its properties and constructor. Const
3 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 - Transform Widget
The Transform widget in Flutter is used to apply various transformations to its child widget, such as rotation, translation (positioning), scaling, and skewing. In this article, we are going to implement all transformations such as rotation, translation (positioning), scaling, and skewing made by th
6 min read
Flutter - InkWell Widget
InkWell is the material widget in flutter. It responds to the touch action as performed by the user. Inkwell will respond when the user clicks the button. There are so many gestures like double-tap, long press, tap down, etc. Below are the so many properties of this widget. We can set the radius of
2 min read
RotatedBox Widget in Flutter
The RotatedBox widget is used to rotate its child by an integral number of quarter turns. It is used to orient its child widgets into either horizontal or vertical orientation. Furthermore, it is very lightweight and can be used for designing various UI as it gives flexibility to the user over the D
2 min read
Flutter - LayoutBuilder Widget
In Flutter, LayoutBuilder Widget is similar to the Builder widget except that the framework calls the builder function at layout time and provides the parent widget's constraints. This is useful when the parent constrains the child's size and doesn't depend on the child's intrinsic size. The LayoutB
3 min read