windmill.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. /// Description: 小风车组件
  4. /// Time : 07/08/2024 Monday
  5. /// Author : liuyuqi.gov@msn.cn
  6. class Windmill extends CustomPainter {
  7. final double radians;
  8. Windmill({required this.radians}) : super();
  9. final double windBlade = 100;
  10. final Paint _paint = Paint()
  11. ..color = Colors.black
  12. ..strokeWidth = 2.0;
  13. @override
  14. void paint(canvas, Size size) {
  15. drawStick(canvas, size);
  16. drawWindmill(canvas, size);
  17. }
  18. @override
  19. bool shouldRepaint(covariant CustomPainter oldDelegate) {
  20. return true;
  21. }
  22. void drawStick(Canvas canvas, Size size) {
  23. _paint.color = Colors.brown;
  24. double height = size.height * 0.6;
  25. double width = size.width * 0.1;
  26. canvas.drawRect(
  27. Rect.fromCenter(
  28. center: Offset((size.width - width) / 2, size.height - height),
  29. width: width,
  30. height: height),
  31. _paint);
  32. }
  33. final List<Color> colors = [
  34. Colors.greenAccent.shade100,
  35. Colors.yellowAccent.shade100,
  36. Colors.blueAccent.shade100,
  37. Colors.pinkAccent.shade100,
  38. ];
  39. void drawWindmill(Canvas canvas, Size size) {
  40. double height = size.height * 0.2;
  41. double width = size.width * 0.3;
  42. double stickWidth = size.width * 0.1;
  43. Path path = Path();
  44. path.moveTo(0.0, 0.0);
  45. path.lineTo(width * 2 / 3, height);
  46. path.lineTo(width, 0);
  47. path.close();
  48. canvas.save();
  49. canvas.translate(size.width / 2 - stickWidth / 2, height / 2);
  50. double radian = radians;
  51. for (int i = 0; i < 4; i++) {
  52. canvas.save();
  53. _paint.color = colors[i];
  54. canvas.rotate(radian);
  55. canvas.drawPath(path, _paint);
  56. canvas.restore();
  57. radian += pi / 2;
  58. }
  59. canvas.restore();
  60. }
  61. }