Sub topic 15
Blend modes and saveLayer
Assume you know Dart classes, Flutter widget trees, layout constraints, Canvas, and Paint. You should also know how a CustomPainter draws inside its paint method.
Blend modes control how newly drawn pixels, called the source, combine with pixels already on the canvas, called the destination. The default mode, BlendMode.srcOver, places the source over the destination while respecting opacity.
Why saveLayer matters
Some effects need to combine several drawings as one group. Canvas.saveLayer creates an offscreen buffer, or temporary layer. Drawing after saveLayer goes into that buffer. restore composites the finished buffer back onto the previous canvas.
This matters when a blend mode should affect only the drawings in the group. Without a layer, a mode such as dstOut can affect content that was already painted outside your intended group.
A layer has a memory and rendering cost. Create one only around the area and operations that need it. The bounds passed to saveLayer should usually be as small as practical.
Using a blend mode to cut a hole
This painter draws an orange panel and removes a circular hole from it:
import 'package:flutter/material.dart';
class HolePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final bounds = Offset.zero & size;
canvas.saveLayer(bounds, Paint());
canvas.drawRect(
bounds,
Paint()..color = Colors.orange,
);
canvas.drawCircle(
size.center(Offset.zero),
40,
Paint()..blendMode = BlendMode.dstOut,
);
canvas.restore();
}
@override
bool shouldRepaint(covariant HolePainter oldDelegate) => false;
}
class HoleDemo extends StatelessWidget {
const HoleDemo({super.key});
@override
Widget build(BuildContext context) {
return const CustomPaint(
size: Size(240, 160),
painter: HolePainter(),
);
}
}BlendMode.dstOut keeps the destination where the source is transparent and removes the destination where the source is opaque. Because the orange panel is inside the saved layer, the circle removes pixels from that panel. The canvas underneath can show through the resulting transparent area.
Choosing a mode
| Mode | Typical result | Common use |
|---|---|---|
| `srcOver` | Source appears over destination | Normal drawing |
| `multiply` | Colors become darker | Shading and texture |
| `screen` | Colors become lighter | Glow and highlights |
| `dstOut` | Destination is erased by source | Masks and holes |
| `clear` | Both source and destination become transparent | Clearing a region |
The exact result depends on alpha values and draw order. Test blend modes against the actual background, especially when the background is transparent.
Practical decision rule
Use a blend mode without a layer when it should combine with the existing canvas content. Use saveLayer when the effect must be isolated to a group of drawings. Pair every saveLayer with restore, and keep the calls balanced even when the painter later gains conditional branches.
Exercise
Why does the hole painter use saveLayer before drawing with BlendMode.dstOut?
Show answer
The layer isolates the orange panel and the erasing circle, so dstOut removes pixels from that group instead of unexpectedly changing unrelated canvas content.