Sub topic 3
Which Widgets Draw and Which Only Compose
Assume you know basic Dart and can build apps with StatelessWidget, StatefulWidget, Row, Column, Padding, and Text. This chapter distinguishes widgets that create render objects from widgets that only describe or organize other widgets.
Four important widget kinds
A widget is an immutable configuration object. It tells Flutter what the UI should contain. The widget itself usually does not calculate sizes or paint pixels.
| Widget kind | Main responsibility | Creates a render object? |
|---|---|---|
| `StatelessWidget` | Builds a subtree from its inputs | No, usually |
| `StatefulWidget` | Owns a `State` object that builds a subtree | No, usually |
| `InheritedWidget` | Shares data with descendants | No |
| `RenderObjectWidget` | Creates and updates a render object | Yes |
“Usually” matters because a StatelessWidget or StatefulWidget can return a render-object widget such as Padding or Text. The outer widget composes the subtree; the returned widget creates the render object.
Composition widgets
StatelessWidget is useful when its output depends only on constructor values and information from BuildContext. Its build method returns another widget.
StatefulWidget is also a composition widget. Its State object stores changing data and returns a new widget configuration when that data changes. Neither class directly performs layout or painting.
import 'package:flutter/widgets.dart';
class Greeting extends StatelessWidget {
const Greeting({super.key, required this.name});
final String name;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: Text('Hello, $name'),
);
}
}Here, Greeting composes Padding and Text. Padding creates a RenderPadding, and Text creates a render object that lays out and paints text. Greeting creates an element but no render object of its own.
InheritedWidget also composes no visual object. It stores a value in the element tree and lets descendants find it through BuildContext. A descendant that calls a dependency method can rebuild when the inherited value changes.
Widgets that create render objects
A RenderObjectWidget connects the widget tree to the render object tree. Its methods create a render object and update that object when widget properties change.
Flutter provides specialized subclasses:
LeafRenderObjectWidgethas no render-object children.SingleChildRenderObjectWidgethas one child.MultiChildRenderObjectWidgethas multiple children.
Examples include Padding, Align, SizedBox, Row, and CustomPaint. They determine how their render objects participate in layout, painting, and hit testing.
A useful rule
When reading a widget tree, ask: “Does this widget return another widget, or does it create a render object?”
Composition widgets organize behavior and configuration. Render-object widgets perform the low-level visual work. This separation lets Flutter rebuild inexpensive descriptions while reusing the render objects that can remain unchanged.
Exercise
In the Greeting example, which widgets create render objects?
Show answer
Padding creates a RenderPadding, and Text creates a render object for text layout and painting. Greeting creates no render object.