drawTooltip method

void drawTooltip(
  1. Canvas canvas,
  2. Size canvasSize,
  3. Offset anchor,
  4. List<String> lines,
)

Draw a rounded-rect tooltip bubble.

Implementation

void drawTooltip(
  Canvas canvas,
  Size canvasSize,
  Offset anchor,
  List<String> lines,
) {
  const padding = 8.0;
  const radius = 6.0;
  const lineSpacing = 4.0;

  final style = theme.typography.tooltipStyle.copyWith(
    color: theme.tooltipTextColor,
  );

  final painters = lines
      .map((l) => textPainterCache.get(l, style, maxWidth: 200))
      .toList();
  final maxW = painters.fold<double>(0, (m, p) => p.width > m ? p.width : m);
  final totalH =
      painters.fold<double>(0, (s, p) => s + p.height) +
      lineSpacing * (painters.length - 1);

  double x = anchor.dx + 12;
  double y = anchor.dy - totalH / 2 - padding;
  x = x.clamp(0, canvasSize.width - maxW - padding * 2 - 12);
  y = y.clamp(0, canvasSize.height - totalH - padding * 2);

  final rect = RRect.fromRectAndRadius(
    Rect.fromLTWH(x, y, maxW + padding * 2, totalH + padding * 2),
    const Radius.circular(radius),
  );

  canvas.drawRRect(rect, fillPaint(theme.tooltipBackgroundColor));

  double textY = y + padding;
  for (final tp in painters) {
    tp.paint(canvas, Offset(x + padding, textY));
    textY += tp.height + lineSpacing;
  }
}