Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Widgets  ›  Lesson

Stateless Widgets

Stateless Widgets 12 minA stateless widget is an immutable description of part of the screen whose build method depends only on its constructor arguments and inherited context.

Stateless Widgets

Sprout's home screen is a list of plants, each row showing a name and "water in 2 days". Every row looks the same apart from its data. In Flutter, that row is a widget: a small, reusable description of part of the screen.

The rule

A stateless widget turns its inputs into a picture, nothing more.

class PlantTile extends StatelessWidget {
  const PlantTile({super.key, required this.name, required this.daysLeft});

  final String name;
  final int daysLeft;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: const Icon(Icons.local_florist),
      title: Text(name),
      subtitle: Text('Water in $daysLeft days'),
    );
  }
}
  • The fields are final. A widget is an immutable description; it never edits itself.
  • The const constructor lets Flutter reuse identical instances, such as const Icon(...), instead of rebuilding them.
  • build returns more widgets. ListTile, Icon and Text are widgets too, so the screen is a tree of widgets inside widgets.
  • super.key passes an optional key to the parent class. Keys help Flutter tell apart items of the same type, for example when a list is reordered.

To show a different plant, the parent simply creates another PlantTile with other arguments: PlantTile(name: 'Cactus', daysLeft: 12).

The shortcut

Ask "Does this widget ever need to change a value by itself?" If the only way its content changes is the parent handing it new arguments, it is stateless.

Is it stateless?Step 1 of 4
  1. The plant row

    Shows a name and a number given by the parent. It never changes them itself. Stateless.

The trap is adding a non-final field to a stateless widget and changing it. The screen will not update, and the analyzer warns you, because nothing tells Flutter to rebuild.

Drills

Answer fast — trust the rule.

quick drill

Which method must a StatelessWidget override?

quick drill

Why are a stateless widget's fields declared final?

quick drill

How do you make a PlantTile show "Cactus" instead of "Fern"?

quick drill

What does the const in const Icon(Icons.local_florist) allow?

Remember

  • A stateless widget turns its inputs into a picture, nothing more.
  • Shortcut: if only the parent changes its content, it is stateless.
  • Trap: mutable fields in a stateless widget never refresh the screen.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWrite PlantTile

Write a stateless widget PlantTile that shows a plant's name and how many days until it needs water. Explain why it can be stateless, and what makes it show different content for different plants.