paintOnWidget method

void paintOnWidget(
  1. Canvas canvas,
  2. Size size, {
  3. PatternScaleBehavior patternScaleBehavior = PatternScaleBehavior.container,
  4. Rect? customRect,
})

Paint the Pattern on a widget.

The widget must be wrapped inside a CustomPaint first.

Rectangular widgets, like Containers, Rows, Columns, IconButtons, etc. can be painted directly with the above method. For widgets that have a custom shape, like a BottomAppbar, you need to provide a different clipBehavior property, e.g. Clip.antiAlias, to make sure the pattern is clipped to the special shape. Check the second screen in the example app for more.

If PatternScaleBehavior.customRect is specified, you must also provide a customRect to scale the Pattern to.

Implementation

void paintOnWidget(Canvas canvas, Size size,
    {PatternScaleBehavior patternScaleBehavior =
        PatternScaleBehavior.container,
    Rect? customRect}) {
  canvas.save();
  final rect = Rect.fromLTWH(0.0, 0.0, size.width, size.height);
  canvas.clipRect(rect);
  switch (patternScaleBehavior) {
    case PatternScaleBehavior.container:
      paintWithPattern(canvas, 0.0, 0.0, size.width, size.height);
      break;
    case PatternScaleBehavior.canvas:
      Size screenSize = WidgetsBinding.instance!.window.physicalSize /
          WidgetsBinding.instance!.window.devicePixelRatio;
      paintOnCanvas(canvas, screenSize);
      break;
    case PatternScaleBehavior.customRect:
      (customRect != null)
          ? paintWithPattern(canvas, customRect.left, customRect.top,
              customRect.width, customRect.height)
          : paintOnCanvas(canvas, size);
      break;
  }
  canvas.restore();
}