Sub topic 1
How CustomPaint and CustomPainter fit together
Flutter widgets usually describe what appears on screen. CustomPaint is a widget that lets you draw directly with a canvas. A canvas is a two-dimensional drawing surface. You provide the drawing rules through a CustomPainter.
Assume you know Dart classes and inheritance, basic Flutter widget trees, layout constraints, and Size. You should also understand that Flutter lays out a widget before it paints it.
Prerequisites
You should be comfortable with:
- Creating
StatelessWidgetandStatefulWidgetclasses. - Passing named parameters to constructors.
- Reading a widget's
Size. - Understanding that a widget receives constraints, chooses a size, and then paints.
The three parts
These objects have separate jobs:
CustomPaintis the widget placed in the widget tree.CustomPaintercontains the drawing instructions.Canvasis the surface that receives those instructions.
CustomPaint creates or uses a canvas during Flutter's paint phase. It calls the painter's paint method and passes the canvas and the widget's final size.
A painter does not choose its own position in the widget tree. The surrounding layout determines its size and position. The painter then uses coordinates inside that rectangle. The top-left corner is (0, 0). The x coordinate increases to the right, and the y coordinate increases downward.
import 'package:flutter/material.dart';
class CirclePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.teal
..style = PaintingStyle.fill;
final center = size.center(Offset.zero);
final radius = size.shortestSide / 2;
canvas.drawCircle(center, radius, paint);
}
@override
bool shouldRepaint(covariant CirclePainter oldDelegate) {
return false;
}
}
class CircleExample extends StatelessWidget {
const CircleExample({super.key});
@override
Widget build(BuildContext context) {
return const CustomPaint(
size: Size(160, 160),
painter: CirclePainter(),
);
}
}paint receives a Canvas and a Size. The example creates a Paint object to describe the circle's color and fill style, then calls drawCircle. The painter has no changing fields, so shouldRepaint returns false.
The size property on CustomPaint supplies a preferred size when the parent does not impose a different one. In a real layout, the parent can still constrain that size. You can also place a child inside CustomPaint. The painter draws behind the child when assigned to painter, or in front of the child when assigned to foregroundPainter.
Painter lifecycle
Flutter may call paint many times. Do not assume it runs only once. Avoid expensive work inside paint, such as loading files or performing unrelated calculations. Create only the objects needed for the current drawing, and keep the method predictable.
When a new painter replaces the old painter, Flutter calls shouldRepaint. Later chapters will cover how to compare painter fields and how animations can request repainting without rebuilding the widget.
Exercise
Which object belongs in the widget tree, and which method receives the canvas?
Show answer
CustomPaint belongs in the widget tree. Its CustomPainter receives the canvas through the paint(Canvas canvas, Size size) method.