magnifier_painter.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import 'package:flutter/material.dart';
  2. class MagnifierPainter extends CustomPainter {
  3. const MagnifierPainter({required this.color, this.strokeWidth = 5});
  4. final double strokeWidth;
  5. final Color color;
  6. @override
  7. void paint(Canvas canvas, Size size) {
  8. _drawCircle(canvas, size);
  9. _drawCrosshair(canvas, size);
  10. }
  11. void _drawCircle(Canvas canvas, Size size) {
  12. Paint paintObject = Paint()
  13. ..style = PaintingStyle.stroke
  14. ..strokeWidth = strokeWidth
  15. ..color = color;
  16. canvas.drawCircle(
  17. size.center(const Offset(0, 0)), size.longestSide / 2, paintObject);
  18. }
  19. void _drawCrosshair(Canvas canvas, Size size) {
  20. Paint crossPaint = Paint()
  21. ..strokeWidth = strokeWidth / 2
  22. ..color = color;
  23. double crossSize = size.longestSide * 0.04;
  24. canvas.drawLine(size.center(Offset(-crossSize, -crossSize)),
  25. size.center(Offset(crossSize, crossSize)), crossPaint);
  26. canvas.drawLine(size.center(Offset(crossSize, -crossSize)),
  27. size.center(Offset(-crossSize, crossSize)), crossPaint);
  28. }
  29. @override
  30. bool shouldRepaint(CustomPainter oldDelegate) {
  31. return true;
  32. }
  33. }