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?
How to create a dotted border around a box in flutter?
104k Views Asked by Juthi Sarker Aka At
5
There are 5 best solutions below
0
On
BorderStyle.none can be useful if you wanna apply some animation or remove\add border function onTap(like a lighting border) event or similar.
2
On
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),
)
0
On
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")),
)
2
On
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;
}
}

EDIT
I have added this as a package in pub.
Now, all you need to do is
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
DashedRectto your container, like soDashedRect.dart
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.