Sub topic 1
Why Flutter rebuilds UI from configuration
Prerequisites
This chapter assumes you know Dart classes, generics, and closures. You should have written a StatelessWidget and a StatefulWidget, and used layout widgets such as Row, Column, Padding, and Text.
Widgets are cheap, immutable descriptions
A widget is an immutable object that describes part of the UI. It holds configuration such as text, padding, or color. It does not draw anything. Flutter calls build to get a fresh set of widgets, and because widgets are small and discarded often, creating them is cheap.
import 'package:flutter/widgets.dart';
void main() => runApp(const GreetingApp());
class GreetingApp extends StatelessWidget {
const GreetingApp({super.key});
@override
Widget build(BuildContext context) {
return const Directionality(
textDirection: TextDirection.ltr,
child: Padding(
padding: EdgeInsets.all(16),
child: Text('Hello'),
),
);
}
}Which widgets draw and which only compose
Some widgets only combine other widgets. GreetingApp and Directionality fall into this group. Others create a render object, the part that lays out and paints. Padding creates a RenderPadding, and Text builds a RichText that creates a RenderParagraph. Composing widgets have no drawing code of their own.
Elements: the living tree behind your widgets
An element is the long-lived object that Flutter keeps for each position in the tree. Widgets come and go on every build, but elements persist. An element holds a reference to its current widget, its child elements, and, for a StatefulWidget, its State object.
How an element is mounted, updated, and unmounted
An element moves through four stages:
- Mount: Flutter creates the element and attaches it to the tree.
- Update: a new parent build supplies a new widget, and the element keeps running with that widget.
- Deactivate: the element is removed from the tree and may be reinserted in the same frame.
- Unmount: the element is gone for good, and its
Stateis disposed.
When a parent rebuilds, each child element checks whether it can take the new widget. The rule is that the runtime type and the key must match:
// Paraphrase of the rule in Flutter's Widget class.
static bool canUpdate(Widget oldWidget, Widget newWidget) {
return oldWidget.runtimeType == newWidget.runtimeType &&
oldWidget.key == newWidget.key;
}If the check passes, the element is reused and its State survives. If not, the old element unmounts and a new one mounts.
BuildContext is an Element
The context parameter in build is the element for that widget. This is why context can look up ancestors such as Theme or Directionality. Use it during build and event handlers. Do not keep it after the element unmounts.
> Note: an element that unmounts can no longer be used for lookups. Reading from a stale context throws an error in debug mode.
Exercise
Exercise
A parent rebuilds and replaces a Text('A') with a Text('B') in the same slot. Does the element get reused?
Show answer
Yes. Both widgets have the same runtime type (Text) and no key, so canUpdate returns true. The element is updated with the new widget and keeps its position in the tree.