draw method
void
draw()
override
Implementation
@override
void draw(Canvas canvas, Size size, double scaleX, double scrollX,
double Function(double) getX, double Function(double) getY) {
debugPrint(
'HorizontalLineTool.draw: yPosition=$yPosition, priceLevel=$priceLevel');
if (yPosition == null) {
debugPrint('HorizontalLineTool.draw: yPosition为null,跳过绘制');
return;
}
final paint = Paint()
..color = color.withOpacity(state == DrawingToolState.drawing ? 0.6 : 1.0)
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke;
// 如果是预览状态,绘制虚线效果
if (state == DrawingToolState.drawing) {
_drawDashedLine(
canvas, Offset(0, yPosition!), Offset(size.width, yPosition!), paint);
} else {
// 绘制横跨整个图表的水平线
debugPrint('绘制水平线: y=$yPosition, 宽度=${size.width}');
canvas.drawLine(
Offset(0, yPosition!),
Offset(size.width, yPosition!),
paint,
);
}
// 绘制价格标签(仅在非预览状态)
if (priceLevel != null && state != DrawingToolState.drawing) {
final textPainter = TextPainter(
text: TextSpan(
text: priceLevel!.toStringAsFixed(2),
style: TextStyle(color: color, fontSize: 12),
),
textDirection: TextDirection.ltr,
);
textPainter.layout();
textPainter.paint(
canvas,
Offset(size.width - textPainter.width - 5,
yPosition! - textPainter.height / 2));
}
}