Stateless vs Stateful Widgets in Flutter With One Example

A StatelessWidget describes what to show using the values it receives and information available from its surroundings, such as the app’s theme. A StatefulWidget has a separate State object that can remember changing information between builds. Both can display a number that changes, respond to a tap, or show a different colour.

Imagine solving a logic puzzle on your phone. You tap square B2 to mark it as a place where a queen cannot go. A checkmark appears, and the screen now says “Ruled out: 1 of 3”. Then you press “Clear marks”. The checkmark disappears and the count returns to zero.

Which part should remember that B2 was marked? The square needs that fact to show its checkmark. The count needs it too. And the reset button needs a way to clear it. That one design decision explains why a Flutter screen often uses stateless and stateful widgets together.

What does “state” mean in a Flutter app?

Think of the pencil marks on a paper puzzle. The printed board tells you where B2 is. The pencil mark records a decision you made while solving it. You can erase the mark without changing the name of the square.

On the Flutter screen, B2 is a label. The set of squares the player has ruled out is state. The count is something else again: it is a value you can calculate from that set. If B2 and B3 are marked, the count is two. Storing a separate counter would give the app two records to keep in agreement whenever someone taps or resets.

A widget’s build() method describes the interface using the values available at that moment. Flutter can call it many times. Any choice the screen needs to remember between those calls needs an owner.

How can a stateless widget change on screen?

Give a square three things: its label, whether it is currently marked, and a function to call when the player taps it. With those inputs, the square has everything it needs to display the right appearance and report the next action. A function supplied for later use like this is called a callback.

Suppose B2 receives marked: false. It shows an empty choice. After the player taps it, the parent records B2 and supplies marked: true on the next build. The square now shows a checkmark. The earlier widget configuration stays immutable; Flutter uses the new configuration to update the interface.

A parent supplying a new configuration is one reason a stateless widget can rebuild. Another is a change in information the widget reads from higher up the tree. For example, a label that reads its colour from Theme.of(context) can rebuild when the app switches to a dark theme, even when the label’s text stays the same.

What does a stateful widget remember?

In this puzzle, the screen owns the set of marked squares. Its StatefulWidget describes the screen, and its State object holds the set. Flutter can keep that object while it updates the screen’s widget configuration. That is how B2 stays marked when the player goes on to tap B3.

Putting the set in the screen also gives “Clear marks” a simple job. The button clears one collection. Each square then receives its updated selection, and the count comes from the same collection. If each square kept its own private copy, the reset button would have to coordinate those copies to produce the same result.

One Flutter example using both widget types

The example represents three squares from a practice board. In a Flutter 3.50 project with material_ui available, the code below goes in lib/main.dart. Read PuzzleSquare first to see what one square receives, then look at _PracticeBoardState to find the marks the screen remembers.

import 'package:material_ui/material_ui.dart';

void main() { runApp(const MaterialApp(home: PracticeBoard())); }

class PuzzleSquare extends StatelessWidget { const PuzzleSquare({ super.key, required this.label, required this.marked, required this.onChanged, });

final String label; final bool marked; final ValueChanged<bool> onChanged;

@override Widget build(BuildContext context) { return FilterChip( label: Text(label), selected: marked, onSelected: onChanged, ); } }

class PracticeBoard extends StatefulWidget { const PracticeBoard({super.key});

@override State<PracticeBoard> createState() => _PracticeBoardState(); }

class _PracticeBoardState extends State<PracticeBoard> { static const squares = ['B2', 'B3', 'B4']; final markedSquares = <String>{};

void updateMark(String square, bool marked) { setState(() { if (marked) { markedSquares.add(square); } else { markedSquares.remove(square); } }); }

@override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: ListView( padding: const EdgeInsets.all(24), children: [ const Text('Tap a square to rule it out.'), Text('Ruled out: ${markedSquares.length} of ${squares.length}'), Wrap( spacing: 8, children: [ for (final square in squares) PuzzleSquare( label: square, marked: markedSquares.contains(square), onChanged: (marked) => updateMark(square, marked), ), ], ), TextButton( onPressed: markedSquares.isEmpty ? null : () => setState(markedSquares.clear), child: const Text('Clear marks'), ), ], ), ), ); } }

Try predicting the screen after three taps: B2, B3, then B2 again. Only B3 should remain marked, the count should read “1 of 3”, and “Clear marks” should still be available. Press it and all three choices should clear. The reset button should also become disabled because there is nothing left to clear.

One small Dart detail matters here. final markedSquares means the field keeps pointing to the same set. The program can still add and remove entries inside that set. By contrast, each PuzzleSquare receives a single final bool marked describing its selection for that configuration.

What actually happens when Flutter calls setState?

When the player taps B2, the control reports the requested selection through its callback. updateMark changes the set inside setState. Flutter runs that change immediately and schedules a build for the board’s State. At the next build, the board reads the updated set to describe the count, the squares and the reset button.

A rebuild means Flutter asks for a fresh description of part of the interface. Layout and painting are separate work. Seeing a call to build() in a debugger does not establish that Flutter repainted the entire screen. Likewise, setState is one reason a stateful component builds again. A new configuration from its parent or a change to an inherited dependency can also trigger a build.

If the board feels slow, first inspect what happens on each tap. Counting three entries is cheap. Solving the entire puzzle or loading a file inside build() would repeat substantially more work. Changing the widget’s class alone would leave that work in place.

How long does widget state last?

The marks survive ordinary rebuilds while Flutter retains the board’s State. If the board is permanently removed and the app later creates a fresh board, the new set starts empty. That may be exactly what a practice round needs. A player who expects to resume yesterday’s puzzle needs progress saved beyond the screen’s lifetime.

You will encounter three lifecycle methods as a screen takes on more responsibility:

  • initState() runs once for each new State object. A timed puzzle could use it to set up a timer. The small example above needs only a field initialiser.
  • didUpdateWidget() runs when Flutter retains the state and supplies a new widget configuration from the parent. This matters if a screen receives a different puzzle and needs to switch which data source it listens to.
  • dispose() is where the state releases resources it owns, such as cancelling that timer when the board is permanently removed.

For a “Resume puzzle” feature, decide what the promise covers. Resuming on the same phone may call for local storage. Resuming on a second phone calls for a shared account record and a way to synchronise progress. Both screens could use the same stateless square. The product requirement changes where the marks must live.

How do you choose between StatelessWidget and StatefulWidget?

  • Choose StatelessWidget when the component can do its job from its inputs and context. A puzzle square can show a mark, take a tap and report the player’s choice with the three inputs in the example.
  • Choose StatefulWidget when the component needs memory or a resource with its own lifetime. A hint panel that opens and closes independently can own its expanded state. A timer display may need to subscribe to a clock and clean up the subscription later.
  • Move state to a shared owner when several controls depend on the same fact. The marks belong to the practice board because the squares, count and reset button must agree. Start with their nearest common parent. A larger application may already have a puzzle model that is a better owner.

A practical test is to ask, “If a different control changes this value, what else must update?” Asking that about “Clear marks” identifies the right owner before you write the first widget. Flutter’s guide to adding interactivity shows how to divide responsibility between a parent and its children.

What this decision looks like in a real app

In Queens Master, players place queens under rules that apply across rows, columns and coloured regions. When I built it after Binero, the two games could share settings, themes and translations while keeping their puzzle rules separate. The three-square example above is a teaching exercise inspired by that distinction. A reusable control can display a player’s choice, while the game decides what that choice means.

If a screen in your app shows conflicting values or loses a user’s choice when they return, a useful starting point is the exact sequence of taps that produces the problem. I can help trace which component owns the value, how long it should survive and what needs to change. Explore my mobile app audit and development expertise.