Flutter - Stateless Widget
Last Updated :
24 Apr, 2025
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 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 understand what Stateless Widgets are.
A Stateless Widget does not have any mutable state that it needs to track. The only area of focus of a stateless widget is the information displayed and the user interface. They deal with situations that are independent of the user's input. A Stateless Widget does not tell the framework when to remove it or rebuild it, it gets a command from the framework itself. They create user interfaces that do not change dynamically upon update in the nearby values. We use a stateless widget when we create an application that does not require redrawing a widget again and again.
Given below is the basic structure of a stateless widget known as GreenFrog.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container();
}
}
The MyApp class is a Stateless Widget, and the Widget build is a method with BuildContext as a parameter that returns widgets. Every widget, while entering the Widget build(Build Context context), has to override it to enter the widget tree. Some Examples of a stateless widget are Text, IconButton, AppBar, etc.
The Build Method is called inside a stateless widget in only three cases:
- Initially in the start when it is built for the very first time, while starting the Application.
- If the parent widget is changed.
- If the inherited widget is changed.
Consider a scenario where you want to call your friend using a mobile phone. The UI of your mobile will remain the same, i.e., no changes will be made until and unless you do not run a command for calling. The moment you click on the screen for recent call logs, the UI will rebuild itself and open call logs for you, i.e., it changes its state.
Now, look at the code below:
Dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
void know() {
// ignore: avoid_print
print("Button Pressed");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('GeeksforGeeks'),
backgroundColor: Colors.green,
),
body: Center(
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green)),
onPressed: know,
child: const Text(
' Stateless Widget Tutorial ',
style: TextStyle(color: Colors.white),
),
),
// RaisedButton class is deprecated and should not be used
// Use ElevatedButton class instead
// child: RaisedButton(
// padding: EdgeInsets.all(5),
// color: Colors.green,
// onPressed: know,
// child: const Text(
// ' Stateless Widget Tutorial ',
// style: TextStyle(color: Colors.white),
// ),
// ),
),
),
);
}
}
The control will enter the runApp() through the main. From there, it will be redirected to the stateless widget of the MyApp class. The build method will be called the very first time MyApp's stateless widget is built. Every time a widget wants to enter the widget tree, it has to override the Widget build (BuildContext context)method. After that, the control goes to the MaterialApp widget, and the rest of the code will be executed. Remember, every time a new widget enters the widget tree, it has to override the build method.
Output:
The point to be noted in the output is that the raised button is pressed 5 times, and every time we get a print statement on the console. What does it mean? It means that the stateless widget is built only once in the beginning, but the widget is built 5 times each time the method know().
Similar Reads
Flutter - Stateful Widget
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 lear
4 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 isT
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 - 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
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
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
Flutter - ShaderMask Widget
ShaderMask is a widget in Flutter that applies a shader to its child. It allows you to create various visual effects by manipulating the colors and gradients of the child widget. In this article, we are going to implement the ShaderMask widget and explore some properties of it. Basic Syntax of Shade
3 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 - RadioListTile Widget
RadioListTile is a widget that combines a radio button with a list tile. It is often used in scenarios where you need to present a list of mutually exclusive options, and the user can select one option from the list. Each RadioListTile represents a single option in the list and consists of a title,
6 min read