Sub topic 2
The Coordinate System and Sizing a CustomPainter
This chapter assumes that you know Dart classes, basic Flutter widget trees, and layout constraints. You should also understand that a Size contains a width and height, and that Flutter lays out widgets before it paints them.
Flutter's local coordinate system
A Canvas uses local coordinates measured in logical pixels. The origin, written as (0, 0), is at the top-left corner of the area being painted.
- The
xcoordinate increases from left to right. - The
ycoordinate increases from top to bottom. size.widthis the available horizontal space.size.heightis the available vertical space.
A point such as (40, 20) means 40 logical pixels from the left and 20 logical pixels from the top. These coordinates are local to the painter. They do not automatically refer to the whole screen.
How CustomPaint gets a size
CustomPaint is a widget, so it follows Flutter's normal layout rules. Its parent gives it constraints. The widget then chooses a size that satisfies those constraints.
When CustomPaint has a child, its size usually follows the child. When it has no child, the size property provides its preferred size. A parent with tight constraints can still override that preferred size.
| Situation | Typical source of the final size |
|---|---|
| `CustomPaint` has a child | The child's layout size |
| No child and finite constraints | The `size` property, adjusted to fit |
| Parent gives tight constraints | The parent's required width and height |
The painter does not choose its size inside paint. Flutter has already completed layout before calling paint. The Size size argument tells you the area that the painter received.
Draw relative to Size
Hard-coded coordinates can work for one fixed size, but they often break when the widget is resized or displayed on another device. Use values derived from size instead.
This example places a circle at the center and scales its radius to fit the shorter dimension:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: CircleScreen()));
}
class CircleScreen extends StatelessWidget {
const CircleScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CustomPaint(
size: const Size(240, 160),
painter: CirclePainter(),
),
),
);
}
}
class CirclePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final center = size.center(Offset.zero);
final radius = size.shortestSide * 0.4;
final paint = Paint()..color = Colors.teal;
canvas.drawCircle(center, radius, paint);
}
@override
bool shouldRepaint(covariant CirclePainter oldDelegate) => false;
}size.center(Offset.zero) calculates the center of the painter's rectangle. The radius uses shortestSide, so the circle remains inside both dimensions.
Common sizing errors
A painter can appear missing even when its drawing code is correct.
- A
CustomPaintwith no child and no useful size may receive a zero-sized area. - Drawing outside
Offset.zero & sizecan clip the result or place it outside the visible area. - Assuming a fixed screen size makes responsive layouts fail.
- Using
MediaQueryinside the painter is usually the wrong abstraction. The painter should use theSizeFlutter gives it.
Exercise
A painter receives a size of 300 by 120. What is the center point, and which dimension should you use to keep a circle inside the area?
Show answer
The center is (150, 60). Use the height, 120, because it is the shorter dimension.