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 - Stack Widget
The Stack widget in Flutter is a powerful tool that allows for the layering of multiple widgets on top of each other. While layouts like Row and Column arrange widgets horizontally and vertically, respectively, Stack provides a solution when you need to overlay widgets. For instance, if you want to
6 min read
Flutter - ListTile Widget
The ListTile widget is used to populate a ListView in Flutter. It contains a title as well as leading or trailing icons. Let's understand this with the help of an example. The Constructor of the ListTile classListTile({ Key key, Widget leading, Widget title, Widget subtitle, Widget trailing, bool is
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 - StreamBuilder Widget
A StreamBuilder in Flutter is used to listen to a stream of data and rebuild its widget subtree whenever new data is emitted by the stream. It's commonly used for real-time updates, such as when working with streams of data from network requests, databases, or other asynchronous sources. In this art
5 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
Table Widget in Flutter
Table widget is used to display items in a table layout. There is no need to use Rows and Columns to create a table. If we have multiple rows with the same width of columns then Table widget is the right approach. SliverList or Column will be most suitable if we only want to have a single column. Th
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