kline_toolbar_icon.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'package:flutter/material.dart';
  2. /// AppBar / 工具栏用的 K 线蜡烛图图标(合约、现货等交易页共用)
  3. class KlineToolbarIcon extends StatelessWidget {
  4. const KlineToolbarIcon({super.key, required this.color});
  5. final Color color;
  6. @override
  7. Widget build(BuildContext context) {
  8. return SizedBox(
  9. width: 20,
  10. height: 20,
  11. child: CustomPaint(painter: _KlineToolbarPainter(color: color)),
  12. );
  13. }
  14. }
  15. class _KlineToolbarPainter extends CustomPainter {
  16. const _KlineToolbarPainter({required this.color});
  17. final Color color;
  18. @override
  19. void paint(Canvas canvas, Size size) {
  20. final stroke = Paint()
  21. ..color = color
  22. ..strokeWidth = 1.2
  23. ..style = PaintingStyle.stroke;
  24. final fill = Paint()
  25. ..color = color
  26. ..style = PaintingStyle.fill;
  27. // 4根蜡烛:(x, wickTop, bodyTop, bodyBottom, wickBottom)
  28. const bars = [
  29. (2.5, 1.0, 4.0, 11.0, 14.0),
  30. (7.0, 2.0, 3.5, 9.0, 13.0),
  31. (11.5, 4.0, 6.5, 13.0, 16.0),
  32. (16.0, 2.5, 4.5, 10.0, 15.0),
  33. ];
  34. const barW = 3.0;
  35. for (final (x, wT, bT, bB, wBtm) in bars) {
  36. final cx = x + barW / 2;
  37. canvas.drawLine(Offset(cx, wT), Offset(cx, wBtm), stroke);
  38. canvas.drawRect(Rect.fromLTWH(x, bT, barW, bB - bT), fill);
  39. }
  40. }
  41. @override
  42. bool shouldRepaint(covariant _KlineToolbarPainter oldDelegate) =>
  43. oldDelegate.color != color;
  44. }