paint method

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset thumbCenter,
  3. Offset offset,
  4. TextPainter textPainter, {
  5. required RenderBox parentBox,
  6. required SfSliderThemeData sliderThemeData,
  7. required Paint paint,
  8. required Animation<double> animation,
  9. required Rect trackRect,
})
override

Draws the tooltip based on the values passed in the arguments.

Implementation

@override
void paint(
  PaintingContext context,
  Offset thumbCenter,
  Offset offset,
  TextPainter textPainter, {
  required RenderBox parentBox,
  required SfSliderThemeData sliderThemeData,
  required Paint paint,
  required Animation<double> animation,
  required Rect trackRect,
}) {
  final double leftPadding = tooltipTextPadding.dx / 2;
  final double minLeftX = trackRect.left;
  // ignore: avoid_as
  final Path path =
      (_isVertical(parentBox as RenderBaseSlider))
          ? _updateRectangularTooltipWidth(
            textPainter.size + tooltipTextPadding,
            offset.dy,
            trackRect,
            thumbCenter.dy,
            isVertical: _isVertical(parentBox),
            isLeftTooltip: _isLeftTooltip(parentBox),
          )
          : _updateRectangularTooltipWidth(
            textPainter.size + tooltipTextPadding,
            offset.dy,
            trackRect,
            thumbCenter.dx,
            isVertical: _isVertical(parentBox),
          );

  context.canvas.save();
  context.canvas.translate(thumbCenter.dx, thumbCenter.dy);
  context.canvas.scale(animation.value);
  final Paint strokePaint = Paint();
  if (_hasTooltipOverlapStroke(parentBox) &&
      sliderThemeData.tooltipBackgroundColor != Colors.transparent) {
    if (sliderThemeData is SfRangeSliderThemeData) {
      strokePaint.color = sliderThemeData.overlappingTooltipStrokeColor!;
      strokePaint.style = PaintingStyle.stroke;
      strokePaint.strokeWidth = 1.0;
    } else if (sliderThemeData is SfRangeSelectorThemeData) {
      strokePaint.color = sliderThemeData.overlappingTooltipStrokeColor!;
      strokePaint.style = PaintingStyle.stroke;
      strokePaint.strokeWidth = 1.0;
    }
  }
  // This loop is used to avoid the improper rendering of tooltips in
  // web html rendering.
  else {
    strokePaint
      ..color = Colors.transparent
      ..style = PaintingStyle.stroke;
  }
  context.canvas.drawPath(path, strokePaint);
  context.canvas.drawPath(path, paint);

  final Rect pathRect = path.getBounds();
  final double halfPathWidth = pathRect.width / 2;
  final double halfTextPainterWidth = textPainter.width / 2;
  final double rectLeftPosition = thumbCenter.dx - halfPathWidth;
  if (_isVertical(parentBox)) {
    final double halfPathHeight = pathRect.height / 2;
    final double halfTextPainterHeight = textPainter.height / 2;
    final double rectTopPosition = thumbCenter.dy - halfPathHeight;
    if (_isLeftTooltip(parentBox)) {
      final double dx =
          -offset.dy -
          tooltipTriangleHeight -
          (pathRect.size.width - tooltipTriangleHeight) / 2 -
          textPainter.width / 2;
      final double dy =
          rectTopPosition >= trackRect.top
              ? thumbCenter.dy + halfPathHeight >= trackRect.bottom
                  ? -halfTextPainterHeight -
                      halfPathHeight -
                      thumbCenter.dy +
                      trackRect.bottom
                  : -halfTextPainterHeight
              : -halfTextPainterHeight +
                  halfPathHeight -
                  thumbCenter.dy +
                  trackRect.top;
      textPainter.paint(context.canvas, Offset(dx, dy));
    } else {
      final double dx =
          offset.dy +
          tooltipTriangleHeight +
          (pathRect.size.width - tooltipTriangleHeight) / 2 -
          textPainter.width / 2;
      final double dy =
          rectTopPosition >= trackRect.top
              ? thumbCenter.dy + halfPathHeight >= trackRect.bottom
                  ? -halfTextPainterHeight -
                      halfPathHeight -
                      thumbCenter.dy +
                      trackRect.bottom
                  : -halfTextPainterHeight
              : -halfTextPainterHeight +
                  halfPathHeight -
                  thumbCenter.dy +
                  trackRect.top;
      textPainter.paint(context.canvas, Offset(dx, dy));
    }
  } else {
    final double dx =
        rectLeftPosition >= minLeftX
            ? thumbCenter.dx + halfTextPainterWidth + leftPadding >
                    trackRect.right
                ? -halfTextPainterWidth -
                    halfPathWidth +
                    trackRect.right -
                    thumbCenter.dx
                : -halfTextPainterWidth
            : -halfTextPainterWidth +
                halfPathWidth +
                trackRect.left -
                thumbCenter.dx;
    final double dy =
        offset.dy +
        tooltipTriangleHeight +
        (pathRect.size.height - tooltipTriangleHeight) / 2 +
        textPainter.height / 2;
    textPainter.paint(context.canvas, Offset(dx, -dy));
  }
  context.canvas.restore();
}