I am building a list of boxes layouts in my app using flutter. I want dotted border around the box. I have used card
widget to create the boxes. But, how can I get dotted border around the boxes?

- 4,704
- 13
- 34
- 52

- 2,217
- 6
- 16
- 22
-
Have a look at the solution in this [comment](https://github.com/flutter/flutter/issues/4858#issuecomment-472716207). Also the issue is about the missing of a solution for a circular border. – NiklasPor Mar 27 '19 at 10:07
-
Flutter release plugin for dotted border, show full example https://stackoverflow.com/a/63252124/11827756 – Kalpesh Dabhi Aug 04 '20 at 17:29
5 Answers
EDIT
I have added this as a package in pub.
Now, all you need to do is
DottedBorder(
color: Colors.black,
gap: 3,
strokeWidth: 1,
child: FlutterLogo(size: 148),
)
Working Solution [Outdated]
Like tomerpacific said in this answer, Flutter does not have a default implementation for dashed border at the moment.
I worked for some time yesterday and was able to come up with a solution using CustomPainter
. Hope this helps someone.
Add the DashedRect
to your container, like so
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
height: 400,
width: 300,
color: Colors.black12,
child: DashedRect(color: Colors.red, strokeWidth: 2.0, gap: 3.0,),
),
),
);
}
}
DashedRect.dart
import 'package:flutter/material.dart';
import 'dart:math' as math;
class DashedRect extends StatelessWidget {
final Color color;
final double strokeWidth;
final double gap;
DashedRect(
{this.color = Colors.black, this.strokeWidth = 1.0, this.gap = 5.0});
@override
Widget build(BuildContext context) {
return Container(
child: Padding(
padding: EdgeInsets.all(strokeWidth / 2),
child: CustomPaint(
painter:
DashRectPainter(color: color, strokeWidth: strokeWidth, gap: gap),
),
),
);
}
}
class DashRectPainter extends CustomPainter {
double strokeWidth;
Color color;
double gap;
DashRectPainter(
{this.strokeWidth = 5.0, this.color = Colors.red, this.gap = 5.0});
@override
void paint(Canvas canvas, Size size) {
Paint dashedPaint = Paint()
..color = color
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke;
double x = size.width;
double y = size.height;
Path _topPath = getDashedPath(
a: math.Point(0, 0),
b: math.Point(x, 0),
gap: gap,
);
Path _rightPath = getDashedPath(
a: math.Point(x, 0),
b: math.Point(x, y),
gap: gap,
);
Path _bottomPath = getDashedPath(
a: math.Point(0, y),
b: math.Point(x, y),
gap: gap,
);
Path _leftPath = getDashedPath(
a: math.Point(0, 0),
b: math.Point(0.001, y),
gap: gap,
);
canvas.drawPath(_topPath, dashedPaint);
canvas.drawPath(_rightPath, dashedPaint);
canvas.drawPath(_bottomPath, dashedPaint);
canvas.drawPath(_leftPath, dashedPaint);
}
Path getDashedPath({
required math.Point<double> a,
required math.Point<double> b,
required gap,
}) {
Size size = Size(b.x - a.x, b.y - a.y);
Path path = Path();
path.moveTo(a.x, a.y);
bool shouldDraw = true;
math.Point currentPoint = math.Point(a.x, a.y);
num radians = math.atan(size.height / size.width);
num dx = math.cos(radians) * gap < 0
? math.cos(radians) * gap * -1
: math.cos(radians) * gap;
num dy = math.sin(radians) * gap < 0
? math.sin(radians) * gap * -1
: math.sin(radians) * gap;
while (currentPoint.x <= b.x && currentPoint.y <= b.y) {
shouldDraw
? path.lineTo(currentPoint.x, currentPoint.y)
: path.moveTo(currentPoint.x, currentPoint.y);
shouldDraw = !shouldDraw;
currentPoint = math.Point(
currentPoint.x + dx,
currentPoint.y + dy,
);
}
return path;
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
I do not expect this to fit in with all use cases and there is a lot of room for customization and improvement. Comment if you find any bugs.

- 185
- 1
- 11

- 6,562
- 5
- 40
- 72
-
2
-
i'd like to add that the flutter team decided against implementing dashed lines (and i think this includes dashed borders as well), because of performance. See this issue: https://github.com/flutter/flutter/issues/4858 – Jens Apr 03 '20 at 11:09
-
1@Jens And not provide/ try to implement internal alternative. In other words, "lol" – Nick Jan 19 '21 at 17:13
-
Can we flag this as the answer? Because I also thought about CustomPaint here for the only solution. – Rebar May 20 '21 at 00:30
-
Thank you !!! you have saved my day :) but can you please share code for circular radius instead of just Rectangle? – Shree Softech Oct 17 '22 at 05:19
-
@ShreeSoftech you could just wrap the widget in a ClipRRect – public static void Main Nov 23 '22 at 08:21
You can use dotted_border Flutter package
return DottedBorder(
borderType: BorderType.RRect,
radius: Radius.circular(20),
dashPattern: [10, 10],
color: Colors.grey,
strokeWidth: 2,
child: Card(
color: Colors.amber,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Center(child: Text("hi")),
)

- 193
- 1
- 10
There is an one plugin for draw dotted border around widgets
https://pub.dev/packages/dotted_border
Using this plugin you can draw dotted or dashed border
//1. Install the plugin by add dependencies in pubspace.yaml
dotted_border: ^1.0.6
Add below code for show border
DottedBorder(
color: Colors.black,
strokeWidth: 1,
child: FlutterLogo(size: 148),
)

- 760
- 8
- 18
-
1Just pointing out that I am the author of this package and the package is not endorsed/affiliated by Flutter in any way. – Ajil O. Feb 13 '22 at 08:55
-
CustomPaint(
painter: DottedBorderPainter(),
child:Container(
child: Text('dashed border',style: TextStyle(
fontSize: 13.5,color: Colors.black))))
class DottedBorderPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.black
..strokeWidth = 2
..style = PaintingStyle.stroke;
final double dashWidth = 5; // Width of each dash
final double dashSpace = 5; // Space between each dash
double startX = 0;
while (startX < size.width) {
canvas.drawLine(Offset(startX, 0), Offset(startX + dashWidth, 0), paint);
startX += dashWidth + dashSpace;
}
// Draw right border
double startY = 0;
while (startY < size.height) {
canvas.drawLine(
Offset(size.width, startY),
Offset(size.width, startY + dashWidth),
paint,
);
startY += dashWidth + dashSpace;
}
// Draw bottom border
startX = 0;
while (startX < size.width) {
canvas.drawLine(
Offset(startX, size.height),
Offset(startX + dashWidth, size.height),
paint,
);
startX += dashWidth + dashSpace;
}
// Draw left border
startY = 0;
while (startY < size.height) {
canvas.drawLine(Offset(0, startY), Offset(0, startY + dashWidth), paint);
startY += dashWidth + dashSpace;
}
}

- 71
- 9
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 05 '23 at 20:55
-
BorderStyle.none can be useful if you wanna apply some animation or remove\add border function onTap(like a lighting border) event or similar.

- 108
- 4