paint method

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset offset
)
override

Called each time the AnimatedBackground needs to repaint.

The canvas provided in the context is already offset by the amount specified in offset, however the parameter is provided to make the signature of the methods uniform.

Implementation

@override
void paint(PaintingContext context, Offset offset) {
  Canvas canvas = context.canvas;
  Paint paint = Paint()..strokeCap = StrokeCap.round;
  final bool axisHorizontal =
      (direction == LineDirection.Ltr || direction == LineDirection.Rtl);
  final int sign =
      (direction == LineDirection.Ltr || direction == LineDirection.Ttb)
          ? 1
          : -1;
  for (var line in lines!) {
    final tailDirection = axisHorizontal
        ? Offset(sign * line.speed / 2.0, 0.0)
        : Offset(0.0, sign * line.speed / 2.0);
    final headDelta =
        axisHorizontal ? Offset(20.0 * sign, 0.0) : Offset(0.0, 20.0 * sign);
    final target = line.position! + tailDirection;
    paint
      ..shader = ui.Gradient.linear(line.position!, target - headDelta,
          <Color>[line.color!.withAlpha(0), line.color!])
      ..strokeWidth = line.thickness.toDouble();
    canvas.drawLine(line.position!, target, paint);
  }
}