Sub topic 17
Relayout and Repaint Boundaries
Assume you know basic Dart and can compose Flutter widgets such as Row, Column, and Container. This chapter focuses on what happens after Flutter rebuilds widgets: deciding which objects must lay out again and which pixels must be painted again.
Relayout boundaries
A relayout boundary limits how far a layout change travels up or down the render object tree. When a render object needs layout, Flutter normally asks its parent to lay out again because the child's size might affect the parent. At a boundary, Flutter can stop this propagation when the parent does not depend on the child's size.
Relayout boundaries are usually created automatically by layout relationships. A child with tight, fixed constraints is often easier to isolate because its size cannot change. Flutter does not provide a general RelayoutBoundary widget that you add everywhere.
Layout still follows this rule:
- Constraints go down from parent to child.
- Sizes go up from child to parent.
- The parent sets the child's position.
A boundary is useful only when those dependencies allow Flutter to avoid more layout work. It does not prevent a widget from rebuilding.
Repaint boundaries
A repaint boundary is a separate painting and compositing region. RepaintBoundary is the widget commonly used to request one.
When a render object inside the boundary changes its appearance, Flutter can repaint that region without repainting unaffected content outside it. The boundary stores the result in a separate composited layer.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: BoundaryDemo()));
class BoundaryDemo extends StatefulWidget {
const BoundaryDemo({super.key});
@override
State<BoundaryDemo> createState() => _BoundaryDemoState();
}
class _BoundaryDemoState extends State<BoundaryDemo> {
int _tapCount = 0;
@override
Widget build(BuildContext context) {
final color = Colors.blue[((_tapCount % 8) + 1) * 100] ?? Colors.blue;
return Scaffold(
appBar: AppBar(title: const Text('Boundaries')),
body: Column(
children: [
ElevatedButton(
onPressed: () => setState(() => _tapCount++),
child: Text('Change square: $_tapCount'),
),
RepaintBoundary(
child: Container(
width: 160,
height: 160,
color: color,
alignment: Alignment.center,
child: const Text('Changing'),
),
),
Container(
width: 160,
height: 160,
color: Colors.orange,
alignment: Alignment.center,
child: const Text('Unchanged'),
),
],
),
);
}
}The setState call rebuilds _BoundaryDemoState. The changing square also needs repainting. The repaint boundary lets Flutter reuse the unchanged orange region instead of repainting it as part of the same large region.
A boundary has a cost. It can use extra memory for layers and may add compositing work. Use it for expensive, frequently changing regions, not for every small widget.
Practical consequences for performance
| Situation | Likely useful action |
|---|---|
| A large static region sits beside animation | Add a carefully chosen `RepaintBoundary` |
| A small state change rebuilds a large subtree | Split the subtree into smaller widgets |
| A value never changes | Use `const` widgets |
| A child keeps changing size | Avoid assuming a relayout boundary will isolate it |
| Performance is uncertain | Measure with DevTools before changing the tree |
Widget splitting limits the scope of rebuilds only when the changing state belongs to the smaller widget. const reduces widget allocation and lets Flutter reuse immutable configuration, but it does not automatically create a repaint boundary.
Use Flutter DevTools’ performance timeline and layer tree to verify whether a boundary reduces work. A boundary that improves one animation can hurt memory or compositing elsewhere.
Exercise
Why might adding a RepaintBoundary to every widget make an app slower?
Show answer
Each boundary can create extra layers, memory use, and compositing work. Boundaries should isolate expensive or frequently changing regions, and their effect should be measured.