|
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'config.dart'; |
| 4 | + |
| 5 | +class PiPainter extends CustomPainter { |
| 6 | + final Config config; |
| 7 | + |
| 8 | + PiPainter(this.config) : super(repaint: config); |
| 9 | + |
| 10 | + @override |
| 11 | + void paint(Canvas canvas, Size size) { |
| 12 | + print(size); |
| 13 | + Paint paint = Paint(); |
| 14 | + paint.style = PaintingStyle.stroke; |
| 15 | + final double span = config.gap; |
| 16 | + drawHelpText( |
| 17 | + '$config', |
| 18 | + canvas, |
| 19 | + Offset.zero, |
| 20 | + ); |
| 21 | + canvas.translate(0, size.height*0.3); |
| 22 | + |
| 23 | + canvas.save(); |
| 24 | + for (int i = 0; i <= config.lineCount; i++) { |
| 25 | + canvas.drawLine(Offset.zero, Offset(size.width, 0), paint); |
| 26 | + print(span*i); |
| 27 | + canvas.translate(0, span); |
| 28 | + } |
| 29 | + canvas.restore(); |
| 30 | + |
| 31 | + Paint needlePaint = Paint(); |
| 32 | + needlePaint.style = PaintingStyle.stroke; |
| 33 | + needlePaint.strokeWidth = 1; |
| 34 | + |
| 35 | + for (Line line in config.lines) { |
| 36 | + if (line.isActive(config.gap)) { |
| 37 | + needlePaint.color = Colors.red; |
| 38 | + } else { |
| 39 | + needlePaint.color = Colors.green; |
| 40 | + } |
| 41 | + canvas.drawLine(line.p0, line.p1, needlePaint); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + bool shouldRepaint(covariant PiPainter oldDelegate) { |
| 49 | + return config != oldDelegate.config; |
| 50 | + } |
| 51 | + |
| 52 | + final TextPainter textPainter = TextPainter( |
| 53 | + textAlign: TextAlign.start, |
| 54 | + textDirection: TextDirection.ltr, |
| 55 | + ); |
| 56 | + |
| 57 | + void drawHelpText( |
| 58 | + String text, |
| 59 | + Canvas canvas, |
| 60 | + Offset offset, { |
| 61 | + Color color = Colors.lightBlue, |
| 62 | + }) { |
| 63 | + textPainter.text = TextSpan( |
| 64 | + text: text, |
| 65 | + style: TextStyle(fontSize: 14, color: color), |
| 66 | + ); |
| 67 | + textPainter.layout(maxWidth: 250); |
| 68 | + textPainter.paint(canvas, offset); |
| 69 | + } |
| 70 | +} |
0 commit comments