Sub topic 6
Building Shapes with `Path`
Prerequisites: you should understand Dart classes, Flutter widget trees, CustomPainter, Canvas, Paint, and the Flutter Size coordinate system. This chapter focuses on combining points and curves into shapes that are more complex than circles and rectangles.
A Path is a list of drawing commands. It can contain straight lines, curves, and arcs. The path itself has no color. You pass it to Canvas.drawPath with a Paint object.
Creating a path
A path usually starts with moveTo. This places the current point without drawing. Commands such as lineTo then draw from the current point to a new point.
Use close to connect the current point back to the starting point. Closing is important when you fill a shape. Without it, the fill algorithm still closes the region internally, but the visible stroke will not include the final connecting line.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: Scaffold(body: ShapeDemo())));
}
class ShapeDemo extends StatelessWidget {
const ShapeDemo({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: CustomPaint(
size: Size(320, 220),
painter: BadgePainter(),
),
);
}
}
class BadgePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final path = Path()
..moveTo(size.width / 2, 10)
..lineTo(size.width - 20, 70)
..cubicTo(
size.width - 5,
120,
size.width - 55,
190,
size.width / 2,
size.height - 10,
)
..cubicTo(
55,
190,
5,
120,
20,
70,
)
..close();
final paint = Paint()
..color = Colors.indigo
..style = PaintingStyle.fill;
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(covariant BadgePainter oldDelegate) => false;
}quadraticBezierTo creates a curve with one control point. cubicTo creates a curve with two control points. A control point pulls the curve toward itself. The final point becomes the new current point.
Filling and stroking paths
A filled path paints its interior. A stroked path paints only its outline. You can draw the same path twice with different Paint objects to create a filled shape with a border.
The direction in which you build subpaths matters when a path contains holes. Flutter uses a fill rule to decide which regions belong to the interior. For predictable results, build each boundary consistently and test complex shapes visually.
Curves, arcs, and subpaths
Use arcTo or addArc when part of a shape follows a circle or ellipse. Use moveTo to start a separate subpath without connecting it to the previous one. This is useful for drawing multiple independent regions with one drawPath call.
Common errors
- Coordinates are local to the painter. They are not screen coordinates.
- A path does not automatically scale when its widget changes size. Use
Sizevalues when calculating points. - Forgetting
closecan leave an open outline. - Creating a path outside
paintcan accidentally preserve geometry between frames. - Using a fill paint when you intended an outline hides the shape's boundary details.
Exercise
Which command starts a new path segment without drawing a line from the previous point, and which command closes the current shape?
Show answer
moveTo starts a new subpath without drawing. close connects the current point to the subpath's starting point.