Sub topic 6
Writing a StatefulWidget and its State
Assume you know basic Dart classes, constructors, named parameters, and null safety. You should also be able to run a basic Flutter app.
A StatefulWidget is a widget whose configuration is immutable but whose visible behavior can change over time. The changing data lives in a separate State object. Flutter keeps that State object between rebuilds, so values such as counters, selected options, and loading flags are not lost each time build runs.
Separating configuration from changing data
The StatefulWidget class describes the widget's configuration. Its fields should normally be final. The associated State class owns mutable fields and produces the current UI.
The connection works like this:
createState creates the state object once when Flutter inserts the widget into the widget tree. The build method reads the state and returns widgets for the current screen.
A complete StatefulWidget
This app displays a counter. Pressing the button changes _count, then calls setState to tell Flutter that the UI must be built again.
import 'package:flutter/material.dart';
void main() {
runApp(const CounterApp());
}
class CounterApp extends StatelessWidget {
const CounterApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: CounterPage(),
);
}
}
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int _count = 0;
void _increment() {
setState(() {
_count++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Counter'),
),
body: Center(
child: Text(
'Count: $_count',
style: Theme.of(context).textTheme.headlineMedium,
),
),
floatingActionButton: FloatingActionButton(
onPressed: _increment,
child: const Icon(Icons.add),
),
);
}
}CounterPage has no mutable fields. Flutter can replace or recreate its widget configuration. _CounterPageState, however, keeps _count while this page remains in the tree.
The callback passed to onPressed calls _increment. The assignment _count++ changes the data, but it does not by itself request a new frame. The setState call marks this state object as needing a rebuild. Flutter then calls build, and the interpolated value in Text('Count: $_count') displays the new count.
State object lifecycle
A State object can also manage resources that should exist only while the widget is active. Override initState to initialize a controller or subscription once. Override dispose to release it.
Keep build focused on describing the current UI. Put event handlers and state transitions in methods such as _increment. This makes it easier to test the transition separately and prevents build from accidentally performing work repeatedly.
Exercise
Why does the counter use setState instead of only changing _count?
Show answer
Changing _count updates the State object, but setState tells Flutter to call build again so the displayed Text reflects the new value.