drawTitle method

void drawTitle(
  1. Canvas canvas,
  2. Size size
)

Implementation

void drawTitle(Canvas canvas, Size size) {
  final titlePainter = TextPainter(
    textAlign: TextAlign.center,
    text: TextSpan(
        text: title.data,
        style: title.style ?? const TextStyle(color: Colors.black)),
    textDirection: TextDirection.ltr,
  )..layout();

  Offset titlePositionOffset;
  switch (gaugeOrientation) {
    case GaugeOrientation.horizontal:
      if (titlePosition == TitlePosition.top) {
        titlePositionOffset = Offset((size.width / 6), size.height / 2);
      } else {
        titlePositionOffset = Offset((size.width / 1.8), size.height / 2);
      }
      final pivot = titlePainter.size.center(titlePositionOffset);
      canvas.save();
      canvas.translate(pivot.dx, pivot.dy);
      canvas.rotate(RadialHelper.degreesToRadians(-90));
      canvas.translate(-pivot.dx, -pivot.dy);
      titlePainter.paint(canvas, titlePositionOffset);
      canvas.restore();

      break;
    case GaugeOrientation.vertical:
      final halfTextWidth = titlePainter.width / 2;
      double titleHeight;
      if (titlePosition.name == 'top') {
        titleHeight = getScaleUpperLimit(size) - 50;

        /// Subtract this value from half width of canvas to center title.
        final titlePositionOffset =
            Offset(((size.width / 2) - halfTextWidth), titleHeight);
        titlePainter.paint(canvas, titlePositionOffset);
      } else if (titlePosition.name == 'bottom') {
        titleHeight = getScaleLowerLimit(size) + 50;
        final titlePositionOffset =
            Offset(((size.width / 2) - halfTextWidth), titleHeight);
        titlePainter.paint(canvas, titlePositionOffset);
      }
      break;
  }
}