Sub topic 3
Canvas and Paint: the Two Core Objects
This chapter assumes you know Dart classes and inheritance, can build basic Flutter widget trees, understand Size, and have basic familiarity with AnimationController. You will use that knowledge to separate two responsibilities:
Canvassays where and what to draw.Paintsays how the drawing should look.
The working model
A CustomPainter receives a canvas and the size available to its CustomPaint widget. The canvas uses logical pixels. Its origin is the top-left corner. The x coordinate increases to the right, and the y coordinate increases downward.
Canvas provides drawing operations such as drawCircle, drawRect, drawLine, and drawPath. These methods do not store a shape for later editing. They immediately add pixels to the current canvas picture.
Paint is a configuration object. It contains properties such as color, fill or stroke style, stroke width, opacity, blend mode, and shader. The same Paint object can be reused when several shapes share one style.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: Scaffold(body: Center(child: Logo()))));
}
class Logo extends StatelessWidget {
const Logo({super.key});
@override
Widget build(BuildContext context) {
return const CustomPaint(
size: Size(180, 120),
painter: LogoPainter(),
);
}
}
class LogoPainter extends CustomPainter {
const LogoPainter();
@override
void paint(Canvas canvas, Size size) {
final background = Paint()
..color = Colors.indigo
..style = PaintingStyle.fill;
final outline = Paint()
..color = Colors.white
..style = PaintingStyle.stroke
..strokeWidth = 6;
final center = size.center(Offset.zero);
final radius = size.shortestSide * 0.35;
canvas.drawCircle(center, radius, background);
canvas.drawCircle(center, radius, outline);
canvas.drawLine(
Offset(center.dx - radius * 0.6, center.dy),
Offset(center.dx + radius * 0.6, center.dy),
outline,
);
}
@override
bool shouldRepaint(covariant LogoPainter oldDelegate) => false;
}The cascade operator .. changes several properties on the same Paint. PaintingStyle.fill fills the inside of a shape. PaintingStyle.stroke draws its boundary. The painter uses the supplied size instead of assuming a fixed canvas, so the logo remains centered when its size changes.
Canvas operations and Paint styles
| Canvas operation | Typical shape | Important Paint settings |
|---|---|---|
| `drawLine` | A straight segment | `color`, `strokeWidth`, `strokeCap` |
| `drawRect` | A rectangle | `style`, `color`, `strokeWidth` |
| `drawCircle` | A circle or disk | `style`, `color`, `strokeWidth` |
| `drawArc` | Part of an ellipse | `style`, `strokeWidth`, `strokeCap` |
A stroke is centered on the shape's boundary. Therefore, a stroked rectangle can extend beyond the rectangle's coordinates by half its stroke width. Account for this when aligning artwork with nearby widgets.
Paint is mutable. If you change its color before drawing the next shape, the next shape uses the new color. For unrelated styles, use separate objects or configure the object immediately before each drawing group.
How the objects work together
Canvas methods consume geometry, while Paint supplies rendering rules. This division lets one painter draw many shapes with different styles without changing the widget tree.
Exercise
Why does the example use two Paint objects for the circle instead of changing one object between calls?
Show answer
The two objects keep the fill and outline configurations separate, which makes the drawing order and style easier to understand. Reusing one object would also work if it were reconfigured before the second call.