drawDefaultLifeBar method

void drawDefaultLifeBar(
  1. Canvas canvas, {
  2. Offset align = Offset.zero,
  3. bool drawInBottom = false,
  4. double margin = 4,
  5. double height = 4,
  6. double? width,
  7. List<Color>? colorsLife,
  8. Color backgroundColor = const Color(0xFF000000),
  9. BorderRadius borderRadius = BorderRadius.zero,
  10. double borderWidth = 0,
  11. Color borderColor = const Color(0xFFFFFFFF),
})

Draw simple life bar

Implementation

void drawDefaultLifeBar(
  Canvas canvas, {
  Offset align = Offset.zero,
  bool drawInBottom = false,
  double margin = 4,
  double height = 4,
  double? width,
  List<Color>? colorsLife,
  Color backgroundColor = const Color(0xFF000000),
  BorderRadius borderRadius = BorderRadius.zero,
  double borderWidth = 0,
  Color borderColor = const Color(0xFFFFFFFF),
}) {
  double yPosition = (position.top - height) - margin;

  double xPosition = position.left + align.dx;

  if (drawInBottom) {
    yPosition = position.bottom + margin;
  }

  yPosition = yPosition - align.dy;

  final w = width ?? position.width;

  double currentBarLife = (life * w) / maxLife;

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

    canvas.drawRRect(
      borderRect,
      _barLiveBorderPaint
        ..color = borderColor
        ..strokeWidth = borderWidth
        ..style = PaintingStyle.stroke,
    );
  }

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

  canvas.drawRRect(
    bgRect,
    _barLiveBgPaint
      ..color = backgroundColor
      ..style = PaintingStyle.fill,
  );

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

  canvas.drawRRect(
    lifeRect,
    _barLivePaint
      ..color = _getColorLife(
        currentBarLife,
        w,
        colorsLife ??
            [Color(0xFFF44336), Color(0xFFFFEB3B), Color(0xFF4CAF50)],
      )
      ..style = PaintingStyle.fill,
  );
}