Sub topic 5
Fill, stroke, caps, and joins
Assume you understand Dart classes, Flutter widget trees, Size, and the basic CustomPaint and CustomPainter APIs. This chapter focuses on how a Paint object controls the appearance of geometry after a Canvas drawing method creates it.
Choose between fill and stroke
Paint.style determines whether Flutter colors the inside of a shape, outlines its boundary, or does both.
PaintingStyle.fillcolors the area inside a shape.PaintingStyle.strokedraws only the outline.PaintingStylehas no combined fill-and-stroke value. Draw the shape twice when you need both.
The strokeWidth property controls the thickness of an outline. It is centered on the path boundary. For example, a 10-pixel stroke extends about 5 pixels inside and 5 pixels outside the path.
import 'package:flutter/material.dart';
class BadgePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final rect = Offset.zero & size;
final roundedRect = RRect.fromRectAndRadius(
rect.deflate(8),
const Radius.circular(20),
);
final fill = Paint()
..color = Colors.teal
..style = PaintingStyle.fill;
final outline = Paint()
..color = Colors.white
..style = PaintingStyle.stroke
..strokeWidth = 6;
canvas.drawRRect(roundedRect, fill);
canvas.drawRRect(roundedRect, outline);
}
@override
bool shouldRepaint(covariant BadgePainter oldDelegate) => false;
}
class BadgeExample extends StatelessWidget {
const BadgeExample({super.key});
@override
Widget build(BuildContext context) {
return const CustomPaint(
size: Size(240, 120),
painter: BadgePainter(),
);
}
}Drawing the fill first and the outline second keeps the border visible. If you reverse the order, the fill can cover part of the border.
Control line ends with caps
A cap is the shape at the end of an open stroked line or path.
| Cap | Appearance | Typical use |
|---|---|---|
| `StrokeCap.butt` | Ends exactly at the endpoint | Precise borders |
| `StrokeCap.round` | Adds a semicircle | Progress indicators and hand-drawn lines |
| `StrokeCap.square` | Extends by half the stroke width | Strong, block-like lines |
The default cap is butt. Caps affect open paths and line segments. They do not change the boundary of a filled shape.
Control corners with joins
A join is the way two stroked path segments meet at a corner.
StrokeJoin.mitercreates a sharp point. Very acute angles can produce long spikes.strokeMiterLimitlimits that behavior.StrokeJoin.bevelcuts the corner off with a straight edge.StrokeJoin.roundreplaces the corner with a circular arc.
The following painter shows both settings on one path:
import 'package:flutter/material.dart';
class CornerPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final path = Path()
..moveTo(20, size.height - 20)
..lineTo(size.width / 2, 20)
..lineTo(size.width - 20, size.height - 20);
final paint = Paint()
..color = Colors.indigo
..style = PaintingStyle.stroke
..strokeWidth = 18
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round;
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(covariant CornerPainter oldDelegate) => false;
}Select a style deliberately
Use fills for solid icons, panels, and surfaces. Use strokes for borders, diagrams, and paths where the interior must remain transparent. Use round caps and joins when a line should look soft. Use bevel joins when sharp corners could create unwanted spikes.
Exercise
Which settings draw a thick, soft-ended outline around a path without filling its interior?
Show answer
Set style to PaintingStyle.stroke, choose a positive strokeWidth, and use StrokeCap.round. Add StrokeJoin.round if the path has corners that should also look soft.