Sub topic 4
Drawing Lines, Rectangles, Circles, and Arcs
This chapter assumes that you understand Dart classes, basic Flutter widget trees, and how Flutter constraints produce a Size. It also assumes that you know the CustomPaint and CustomPainter roles from the previous chapter.
A Canvas is the drawing surface. It provides methods such as drawLine and drawCircle. A Paint object describes how a shape looks, including its color, fill style, stroke width, and line endings.
Understanding the canvas coordinates
The canvas uses local coordinates. The origin, (0, 0), is at the painter's top-left corner. The x-coordinate increases to the right, and the y-coordinate increases downward.
The Size passed to paint tells you the available width and height. Use it instead of hard-coding positions when the painter should adapt to different screen sizes.
Common drawing methods
| Method | Main arguments | Result |
|---|---|---|
| `drawLine` | Two `Offset` points and a `Paint` | A straight line |
| `drawRect` | A `Rect` and a `Paint` | An axis-aligned rectangle |
| `drawCircle` | Center, radius, and `Paint` | A circle |
| `drawArc` | Bounding rectangle, start angle, sweep angle, and `Paint` | Part of an ellipse or circle |
Angles use radians. An angle of zero points to the right. Positive angles move clockwise because the canvas y-axis points downward. The useCenter argument in drawArc controls whether Flutter connects the arc's endpoints to the center.
A complete painter example
This painter draws a line, a filled rectangle, an outlined circle, and a semicircular arc.
import 'dart:math' as math;
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: PrimitiveExample()));
}
class PrimitiveExample extends StatelessWidget {
const PrimitiveExample({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: CustomPaint(
size: Size(320, 220),
painter: PrimitivePainter(),
),
),
);
}
}
class PrimitivePainter extends CustomPainter {
const PrimitivePainter();
@override
void paint(Canvas canvas, Size size) {
final linePaint = Paint()
..color = Colors.indigo
..strokeWidth = 5
..strokeCap = StrokeCap.round;
final fillPaint = Paint()
..color = Colors.orange
..style = PaintingStyle.fill;
final outlinePaint = Paint()
..color = Colors.teal
..style = PaintingStyle.stroke
..strokeWidth = 5;
final arcPaint = Paint()
..color = Colors.purple
..style = PaintingStyle.stroke
..strokeWidth = 5;
canvas.drawLine(
const Offset(20, 20),
Offset(size.width - 20, 20),
linePaint,
);
canvas.drawRect(
const Rect.fromLTWH(30, 50, 100, 70),
fillPaint,
);
canvas.drawCircle(
Offset(size.width - 80, 85),
40,
outlinePaint,
);
final arcBounds = Rect.fromCircle(
center: Offset(size.width / 2, 175),
radius: 35,
);
canvas.drawArc(
arcBounds,
-math.pi / 2,
math.pi,
false,
arcPaint,
);
}
@override
bool shouldRepaint(covariant PrimitivePainter oldDelegate) => false;
}Rect.fromLTWH creates a rectangle from its left, top, width, and height. Rect.fromCircle creates the bounding rectangle needed by an arc. The circle's radius is half its diameter, while a rectangle's dimensions are independent.
Choosing the right primitive
Use drawLine for independent straight segments. Use drawRect for axis-aligned boxes. Use drawCircle when the shape must remain circular. Use drawArc for progress indicators, gauges, and curved outlines. For connected segments or irregular outlines, use Path, which the next chapter covers.
A primitive does not store visual state by itself. The Paint supplied to the method determines whether it is filled or outlined and how its edges appear. Reuse compatible Paint objects inside paint when several shapes share the same style.
Exercise
What drawArc arguments create a half-circle that starts at the top and sweeps clockwise?
Show answer
Use a bounding rectangle, -math.pi / 2 as the start angle, math.pi as the sweep angle, false for useCenter, and an outline Paint.