render method

  1. @override
void render(
  1. Canvas canvas
)
override

Implementation

@override
void render(Canvas canvas) {
  double yPosition = (y - height);
  double xPosition = (target.size.x - width) / 2;
  switch (drawPosition) {
    case BarLifeDrawPosition.top:
      break;
    case BarLifeDrawPosition.bottom:
      yPosition = target.size.y + y;
      break;
    case BarLifeDrawPosition.left:
      xPosition = -width + x;
      yPosition = (target.size.y / 2 - height / 2) + y;
      break;
    case BarLifeDrawPosition.right:
      xPosition = width + x;
      yPosition = (target.size.y / 2 - height / 2) + y;
      break;
  }

  double currentBarLife = (_life * width) / _maxLife;

  if (borderWidth > 0) {
    final RRect borderRect = borderRadius.toRRect(
      Rect.fromLTWH(
        xPosition,
        yPosition,
        width,
        height,
      ),
    );

    canvas.drawRRect(
      borderRect,
      _barLifeBorderPaint,
    );
  }

  final RRect bgRect = borderRadius.toRRect(Rect.fromLTWH(
    xPosition,
    yPosition,
    width,
    height,
  ));

  canvas.drawRRect(
    bgRect,
    _barLifeBgPaint,
  );

  final RRect lifeRect = borderRadius.toRRect(Rect.fromLTWH(
    xPosition,
    yPosition,
    currentBarLife,
    height,
  ));

  canvas.drawRRect(
    lifeRect,
    _barLifePaint
      ..color = _getColorLife(
        currentBarLife,
        width,
        colors ??
            [
              const Color(0xFFF44336),
              const Color(0xFFFFEB3B),
              const Color(0xFF4CAF50),
            ],
      ),
  );

  if (showLifeText) {
    double xText = _textOffset.x + xPosition + (width - _textSize.x) / 2;
    double yText = _textOffset.y + yPosition + (height - _textSize.y) / 2;
    _textConfig.render(
      canvas,
      _getLifeText(),
      Vector2(xText, yText),
    );
  }

  super.render(canvas);
}