Flutter Dashed Border
Flutter's Border only draws solid lines, so a dashed outline has to be drawn by hand. The usual way is to measure the outline with Path.computeMetrics() and stroke short pieces of it with extractPath. Set the dash, gap, stroke width and corner radius below and copy the code that matches.
The editor has no dashed strokes yet, so the preset opens the rounded outline the dashes follow.
import 'dart:math' as math;
import 'package:flutter/material.dart';
class DashedBorderPainter extends CustomPainter {
const DashedBorderPainter({this.color = const Color(0xFF6366F1)});
final Color color;
static const double dashLength = 8.0;
static const double gapLength = 6.0;
static const double strokeWidth = 2.0;
static const double radius = 16.0;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = color
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth;
// Inset by half the stroke so the outer edge is not clipped by the widget bounds.
final outline = Path()
..addRRect(
RRect.fromRectAndRadius(
(Offset.zero & size).deflate(strokeWidth / 2),
const Radius.circular(radius),
),
);
for (final metric in outline.computeMetrics()) {
var distance = 0.0;
while (distance < metric.length) {
final end = math.min(distance + dashLength, metric.length);
canvas.drawPath(metric.extractPath(distance, end), paint);
distance += dashLength + gapLength;
}
}
}
@override
bool shouldRepaint(DashedBorderPainter oldDelegate) =>
oldDelegate.color != color;
}
// Usage:
// CustomPaint(
// painter: const DashedBorderPainter(),
// child: const Padding(
// padding: EdgeInsets.all(24),
// child: Text('Drop files here'),
// ),
// )When to use CustomPainter vs ShapeBorder for this shape
A dashed border is a stroke, not a shape to clip to, so there is no ClipPath version here. Pick between the other two.
Use the CustomPainter when you wrap a single widget in CustomPaint and the border is purely decoration, for example a "drop files here" zone.
Use the ShapeBorder when the dashed outline belongs to a surface that already takes a shape: Container with ShapeDecoration, Card, Material or InkWell(customBorder: ...). The border then lives in one place and the ink splash can follow it.
Common mistakes
- Stroking the full rect. A stroke is centered on the path, so half of it falls outside the widget bounds and is cut off. Both versions above deflate the rect by half the stroke width.
- Not clamping the last dash.
extractPath(distance, end)withendbeyondmetric.lengthis harmless in practice, butmath.minkeeps the intent clear. - Returning
falsefromshouldRepaintwhen the color is a field. Compare every field the painter reads. - Re-measuring the path on every frame of an animation. If you animate the dashes (marching ants), build the
Pathonce and only change the dash offset.
FAQ
Does Flutter have a dashed border?
Not in the framework. BorderStyle only has solid and none, so a dashed border is either drawn with Path.computeMetrics() and PathMetric.extractPath, as in the code above, or taken from a package.
How do I make a dashed border with rounded corners?
Build a rounded rectangle path with RRect.fromRectAndRadius, deflate it by half the stroke width, then step along each PathMetric and draw a short extracted segment per dash. Use the corner radius slider to see the result.
How do I animate a dashed border?
Keep the outline Path and its metrics, and start the dash loop at an offset that grows with an animation value, so the dashes appear to travel. The PathMetric tutorial linked below covers marching-ants dashes.