Skip to content

Instantly share code, notes, and snippets.

@miguelpruivo
Created December 13, 2018 12:19
Show Gist options
  • Save miguelpruivo/9576a88db02f424ba15b2e8fed4abb1a to your computer and use it in GitHub Desktop.
Save miguelpruivo/9576a88db02f424ba15b2e8fed4abb1a to your computer and use it in GitHub Desktop.
Polygon Widget
class ClipPolygon extends StatelessWidget {
final Widget child;
final int sides;
ClipPolygon({
@required this.child,
@required this.sides,
});
@override
Widget build(BuildContext context) {
return new AspectRatio(
aspectRatio: 1.0,
child: new ClipPath(
clipper: new Polygon(sides: sides),
child: child,
));
}
}
class Polygon extends CustomClipper<Path> {
final int sides;
Polygon({@required this.sides});
Path draw(double width) {
final anglePerSide = 360 / sides;
final radius = width / 2;
Path path = new Path();
for (var i = 0; i <= sides; i++) {
double currentAngle = anglePerSide * i;
bool isFirst = i == 0;
_drawLine(path, currentAngle, radius, isFirst);
}
return path;
}
double _angleToRadian(double angle) {
return angle * (pi / 180);
}
Offset _getOffset(double angle, double radius) {
//final rotationAwareAngle = angle - 90;
final radian = _angleToRadian(angle);
print(angle);
final x = cos(radian) * radius + radius;
final y = sin(radian) * radius + radius;
print(x);
print(y);
return new Offset(x, y);
}
_drawLine(Path path, double currentAngle, double radius, bool move) {
Offset current = _getOffset(currentAngle, radius);
if (move)
path.moveTo(current.dx, current.dy);
else
path.lineTo(current.dx, current.dy);
}
@override
Path getClip(Size size) {
return draw(size.width);
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment