Sub topic 5
Writing a StatelessWidget
This chapter assumes basic Dart syntax, null safety, and a working Flutter installation. You should know how to create and run a simple Flutter app.
A StatelessWidget is a widget whose configuration does not change after it is created. It receives values through its constructor and uses those values to describe its UI. It does not store changing data such as a counter or a selected item.
A stateless widget can still appear in an app that changes. When its parent rebuilds with different constructor values, Flutter creates or updates the widget configuration and calls build again.
The structure of a StatelessWidget
A StatelessWidget usually has three parts:
| Part | Purpose |
|---|---|
| Constructor and final fields | Receive the widget's configuration |
| `build` method | Return the widgets to display |
| `BuildContext` parameter | Identify the widget's location in the tree |
Here is a complete example:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Greeting')),
body: const Center(
child: Greeting(name: 'Mina'),
),
),
);
}
}
class Greeting extends StatelessWidget {
const Greeting({super.key, required this.name});
final String name;
@override
Widget build(BuildContext context) {
return Text('Hello, $name');
}
}Greeting receives a name value through its required named parameter. The field is final, so the value cannot be replaced after the object is created. The build method reads that value and returns a Text widget.
The context parameter is a BuildContext. It represents this widget's position in the widget tree. You can use it to find inherited information, such as the current theme or screen size. The example does not need that information, but the parameter is required by the build method.
Why use a StatelessWidget?
Use a stateless widget when the widget's appearance depends only on its inputs and other stable information. Common examples include:
- A product title that receives a
String. - A profile header that receives a user object.
- A reusable button that receives a label and a callback.
- A layout that arranges other widgets without owning changing data.
Splitting UI into small stateless widgets makes each part easier to read and test. It also gives each widget a clear responsibility.
Building a widget tree
The return value of build is the root of a smaller widget tree. That root can contain many descendants:
When Flutter needs to display Greeting, it calls build. The method should describe what the UI should look like for the current inputs. Avoid performing one-time setup, network requests, or other side effects directly in build, because Flutter may call it many times.
If the widget needs to remember changing data, use a StatefulWidget instead, or let a parent own the data and pass the current value into this widget.
Exercise
Should a widget that displays userName and receives it through a required final constructor field be stateless or stateful? Why?
Show answer
It should be stateless if it only displays that value. Its UI depends on constructor input, and it does not need to remember changing data itself.