Flutter StatelessWidget
A StatelessWidget is a widget that is fully described by the values passed to its constructor. It holds no data that changes over time — when its inputs change, the parent simply creates a new instance with new values. Most of the widgets you write will be stateless.
Anatomy of a StatelessWidget
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: Greeting(name: 'Ada')))));
class Greeting extends StatelessWidget {
// 1. A const constructor with a key and named parameters
const Greeting({super.key, required this.name});
// 2. Final fields: a stateless widget never changes them
final String name;
// 3. build() returns the widget subtree
@override
Widget build(BuildContext context) {
return Text(
'Hello, $name!',
style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
);
}
}The three parts:
- Constructor —
const Greeting({super.key, required this.name}).super.keyis a Dart 3 super parameter that forwards the optionalkeytoStatelessWidget. Marking itconstlets callers create constant instances. - Final fields — every field must be
final, because widgets are immutable. build()— called by the framework whenever the widget needs to be drawn. It receives aBuildContext(the widget's location in the tree) and returns other widgets.
Note: You never call
build()yourself. Flutter calls it when the widget is first inserted, when its parent rebuilds it with new arguments, or when an inherited value it depends on (like the theme) changes.
Passing data down
Parameters are how parents configure children. Use named, required parameters for mandatory values and give optional parameters a default:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ProfilePage()));
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Team')),
body: const Column(
children: [
MemberRow(name: 'Linh Tran', role: 'Designer'),
MemberRow(name: 'Omar Haddad', role: 'Developer', online: true),
MemberRow(name: 'Sofia Rossi', role: 'Product Manager'),
],
),
);
}
}
class MemberRow extends StatelessWidget {
const MemberRow({
super.key,
required this.name,
required this.role,
this.online = false,
});
final String name;
final String role;
final bool online;
@override
Widget build(BuildContext context) {
return ListTile(
leading: CircleAvatar(child: Text(name[0])),
title: Text(name),
subtitle: Text(role),
trailing: Icon(Icons.circle, size: 12, color: online ? Colors.green : Colors.grey),
);
}
}MemberRow is reusable: the screen describes what to show, the widget describes how.
Using the BuildContext
The context lets a widget look up things provided higher in the tree, such as the theme or the screen size:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
theme: ThemeData(colorSchemeSeed: Colors.teal),
home: const Scaffold(body: Center(child: ThemedBadge(text: 'NEW'))),
));
class ThemedBadge extends StatelessWidget {
const ThemedBadge({super.key, required this.text});
final String text;
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
final width = MediaQuery.sizeOf(context).width;
debugPrint('Screen width: $width');
return Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
decoration: BoxDecoration(
color: colors.primary,
borderRadius: BorderRadius.circular(20),
),
child: Text(text, style: TextStyle(color: colors.onPrimary)),
);
}
}Screen width: 360.0
Because ThemedBadge reads the theme through context, changing the app's seed colour restyles every badge automatically.
When to use StatelessWidget
| Use StatelessWidget when… | Use StatefulWidget when… |
|---|---|
| The UI depends only on constructor arguments and inherited data | The widget itself must remember something that changes |
| Examples: labels, icons with text, cards, list rows | Examples: counters, toggles, text input, animations |
Tip: Start every widget as stateless. Convert it to stateful only when it truly needs to own changing data — your editor has a "Convert to StatefulWidget" quick fix.
Test yourself
Why must the fields of a StatelessWidget be final?
- Dart forbids non-final fields in classes
- Widgets are immutable descriptions; changes come from creating a new widget
- Final fields render faster on the GPU
- So that setState() can modify them
What does super.key in const MyWidget({super.key}) do?
- Creates a unique random key
- Forwards the optional key parameter to the superclass constructor
- Makes the widget stateful
- Marks the widget as the root
Who calls a widget's build() method?
- Your own code, after creating the widget
- The Flutter framework
- The
main()function - The Dart garbage collector