Skip to content

Instantly share code, notes, and snippets.

@imaNNeo
Created October 29, 2020 08:51
Show Gist options
  • Save imaNNeo/fb9ac2325792785fe075d8a862af0671 to your computer and use it in GitHub Desktop.
Save imaNNeo/fb9ac2325792785fe075d8a862af0671 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'dart:math';
main() => runApp(MaterialApp(home: MyHomePage()));
const int _DefaultDashes = 20;
const Color _DefaultColor = Color(0);
const double _DefaultGapSize = 3.0;
const double _DefaultStrokeWidth = 2.5;
class DashedCircle extends StatelessWidget {
final int dashes;
final Color color;
final double gapSize;
final double strokeWidth;
final Widget child;
DashedCircle(
{this.child,
this.dashes = _DefaultDashes,
this.color = _DefaultColor,
this.gapSize = _DefaultGapSize,
this.strokeWidth = _DefaultStrokeWidth});
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: DashedCirclePainter(
dashes: dashes,
color: color,
gapSize: gapSize,
strokeWidth: strokeWidth),
child: child,
);
}
}
class DashedCirclePainter extends CustomPainter {
final int dashes;
final Color color;
final double gapSize;
final double strokeWidth;
DashedCirclePainter(
{this.dashes = _DefaultDashes,
this.color = _DefaultColor,
this.gapSize = _DefaultGapSize,
this.strokeWidth = _DefaultStrokeWidth});
@override
void paint(Canvas canvas, Size size) {
final double gap = pi / 180 * gapSize;
final double singleAngle = (pi * 2) / dashes;
for (int i = 0; i < dashes; i++) {
final Paint paint = Paint()
..color = color
..strokeWidth = _DefaultStrokeWidth
..style = PaintingStyle.stroke;
canvas.drawArc(Offset.zero & size, gap + singleAngle * i,
singleAngle - gap * 2, false, paint);
}
}
@override
bool shouldRepaint(DashedCirclePainter oldDelegate) {
return dashes != oldDelegate.dashes || color != oldDelegate.color;
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter 4 Fun'),
),
body: Center(
child: DashedCircle(
child: Padding(
padding: EdgeInsets.all(5.0),
child: CircleAvatar(
radius: 60.0,
backgroundColor: Colors.grey,
backgroundImage: NetworkImage("https://img.techpowerup.org/201029/user2.png"),
),
),
dashes: 20,
gapSize: 4,
color: Colors.green,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment