paint method

  1. @override
void paint({
  1. required Canvas canvas,
  2. required Size size,
  3. required double scale,
  4. required double blur,
  5. required Color color,
})
override

Perform to take a paint task corresponding to the current state, referencing the given arguments.

Implementation

@override
void paint({
  required Canvas canvas,
  required Size size,
  required double scale,
  required double blur,
  required Color color,
}) {
  // Returns how far the given offset is from the centre of the canvas size,
  // defined as a percentage (0~1), relative to the canvas size.
  Offset centerToRatioOf(Offset offset) {
    final sizeOffset = sizeToOffset(size);
    final dx = (offset.dx / sizeOffset.dx) - 0.5;
    final dy = (offset.dy / sizeOffset.dy) - 0.5;

    return Offset(dx.abs(), dy.abs());
  }

  // The offset that serves as the reference point for the spread of the effect.
  final baseOffset = eventedOffset;

  // If a touch event occurs at the exact center,
  // it is the size at which the touch ripple effect fills completely.
  final centerDistance = sizeToOffset(size / 2).distance;

  // However, since touch events don't actually occur at the exact center but at various offsets,
  // it is necessary to compensate for this.
  //
  // If the touch event moves away from the center,
  // the touch ripple effect should expand in size accordingly.
  //
  // This defines the additional scale that needs to be expanded.
  final centerToRatio = centerToRatioOf(baseOffset);

  // This defines the additional touch ripple size.
  final distance = Offset(
    sizeToOffset(size).dx * centerToRatio.dx,
    sizeToOffset(size).dy * centerToRatio.dy,
  ).distance + (blur * 2);

  final paintSize = (centerDistance + distance) * spreadPercent;
  final paintColor = color.withAlpha(((color.alpha) * fadePercent).toInt());
  final paint = Paint()
    ..color = paintColor
    ..style = PaintingStyle.fill;

  if (blur != 0) {
    paint.maskFilter = MaskFilter.blur(BlurStyle.normal, blur);
  }

  canvas.drawCircle(baseOffset, paintSize * scale, paint);
}