Skip to content
Guest
Flutter · Course Syllabus Show lessons
Flutter UI · 9 min read · Beginner

The Widget Tree: Stateless vs StatefulWidget

Understand Flutter's declarative UI model ('UI = f(state)'), StatelessWidget, and StatefulWidget with setState.

What you'll learn

  • Build reusable StatelessWidgets for pure presentation
  • Manage local interactive state with StatefulWidget and setState()
  • Keep build() methods pure and fast (never call APIs inside build)

Overview

Understand Flutter’s declarative UI model (‘UI = f(state)’), StatelessWidget, and StatefulWidget with setState.

Code Example

class CounterCard extends StatefulWidget {
  const CounterCard({super.key});
  @override
  State<CounterCard> createState() => _CounterCardState();
}

class _CounterCardState extends State<CounterCard> {
  int count = 0;
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () => setState(() => count++),
      child: Text('Clicked $count times'),
    );
  }
}

Key Takeaways

  • Build reusable StatelessWidgets for pure presentation
  • Manage local interactive state with StatefulWidget and setState()
  • Keep build() methods pure and fast (never call APIs inside build)

Quick Check

Check Your Understanding

Score: 0 / 1

1. What happens when you mutate a state variable inside a `StatefulWidget` WITHOUT calling `setState()`?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind