Created
June 20, 2024 13:59
-
-
Save PlugFox/d79b64c8f223b8653d0e8312b0741d86 to your computer and use it in GitHub Desktop.
Clickable painter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:math'; | |
void main() => runApp(SegmentWidget()); | |
class _SegmentPainter extends CustomPainter { | |
static const offset = -pi / 2; | |
double start; | |
double end; | |
double innerRadius; | |
double outerRadius; | |
Color color; | |
late Path path; | |
_SegmentPainter( | |
this.start, this.end, this.innerRadius, this.outerRadius, this.color) { | |
path = new Path() | |
..arcTo( | |
Rect.fromCircle(center: new Offset(0.0, 0.0), radius: outerRadius), | |
offset + start, | |
end - start, | |
true) | |
..relativeLineTo(-cos(offset + end) * (outerRadius - innerRadius), | |
-sin(offset + end) * (outerRadius - innerRadius)) | |
..arcTo( | |
Rect.fromCircle(center: new Offset(0.0, 0.0), radius: innerRadius), | |
offset + end, | |
start - end, | |
false) | |
..close(); | |
} | |
@override | |
bool shouldRepaint(_SegmentPainter oldDelegate) { | |
return oldDelegate.start != start || | |
oldDelegate.end != end || | |
oldDelegate.innerRadius != innerRadius || | |
oldDelegate.outerRadius != outerRadius || | |
oldDelegate.color != color; | |
} | |
@override | |
bool shouldRebuildSemantics(_SegmentPainter oldDelegate) => true; | |
@override | |
void paint(Canvas canvas, Size size) { | |
canvas.drawPath( | |
path, | |
new Paint() | |
..color = color | |
..style = PaintingStyle.fill); | |
} | |
@override | |
bool hitTest(Offset position) { | |
return path.contains(position); | |
} | |
} | |
class SegmentWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new GestureDetector( | |
onTap: () => print('tap'), | |
child: new SizedBox( | |
width: 250.0, | |
height: 250.0, | |
child: new CustomPaint( | |
painter: new _SegmentPainter(0.0, 2.8, 150.0, 200.0, Colors.orange), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment