paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Offset offset,
  3. ImageConfiguration configuration
)
override

Paints the Decoration for which this object was created on the given canvas using the given configuration.

The ImageConfiguration object passed as the third argument must, at a minimum, have a non-null Size.

If this object caches resources for painting (e.g. Paint objects), the cache may be flushed when paint is called with a new configuration. For this reason, it may be more efficient to call Decoration.createBoxPainter for each different rectangle that is being painted in a particular frame.

For example, if a decoration's owner wants to paint a particular decoration once for its whole size, and once just in the bottom right, it might get two BoxPainter instances, one for each. However, when its size changes, it could continue using those same instances, since the previous resources would no longer be relevant and thus losing them would not be an issue.

Implementations should paint their decorations on the canvas in a rectangle whose top left corner is at the given offset and whose size is given by configuration.size.

When a Decoration is painted in a Container or DecoratedBox (which is what Container uses), the ImageConfiguration.textDirection property will be populated based on the ambient Directionality.

Implementation

@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
  if(configuration.size==null){
    return;
  }

  Radius radius = _decoration.radius ?? Radius.zero;
  canvas.save();
  final Paint paint = Paint()
    ..style = PaintingStyle.stroke
    ..color = Colors.orangeAccent
    ..strokeWidth = _decoration.strokeWidth;
  final Path path = Path();

  canvas.translate(
    offset.dx + configuration.size!.width / 2,
    offset.dy + configuration.size!.height / 2,
  );

  final Rect zone = Rect.fromCenter(
    center: Offset.zero,
    width: configuration.size!.width,
    height: configuration.size!.height,
  );

  if (_decoration.color != null) {
    final Paint rectPaint = Paint()..color = _decoration.color!;
    canvas.drawRRect(
        RRect.fromRectAndRadius(zone, radius), rectPaint);
  }

  path.addRRect(RRect.fromRectAndRadius(
    zone,
    radius,
  ));

  if (_decoration.gradient != null) {
    paint.shader = _decoration.gradient!.createShader(zone);
  }

  DashPainter(
    span: _decoration.span,
    step: _decoration.step,
    pointCount: _decoration.pointCount,
    pointWidth: _decoration.pointWidth,
  ).paint(canvas, path, paint);
  canvas.restore();
}