boxy icon indicating copy to clipboard operation
boxy copied to clipboard

`layers.clipPath()` does not apply `paintOffset` to `path` correctly when used in `paint()`.

Open PurplePolyhedron opened this issue 1 year ago • 2 comments

layers.clipPath() does not apply paintOffset to path correctly when used in paint(). It works as expected in paintChildren()

import 'package:boxy/boxy.dart';
import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(MaterialApp(
    theme: ThemeData.from(colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.purple)),
    home: Scaffold(
      appBar: AppBar(
        title: Text("Demo"),
      ),
      body: MyWidget(),
    ),
  ));
}

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.black, width: 2.0),
      ),
      child: CustomBoxy(delegate: Delegate()),
    );
  }
}

class Delegate extends BoxyDelegate {
  @override
  Size layout() {
    return Size(300.0, 300.0);
  }

  @override
  void paint() {
    layers.clipPath(
      path: Path()..addRect(Rect.fromLTWH(100.0, 100.0, 100.0, 100.0)),
      paint: () {
        canvas.drawRect(Rect.fromLTWH(0.0, 0.0, 300.0, 300.0), Paint()..color = Colors.blue);
      },
    );
  }
}

PurplePolyhedron avatar May 11 '24 06:05 PurplePolyhedron

This is expected, the paint method has an offset canvas, but it should be restored before pushing new layers since those layers might not actually be drawn to the same canvas as the rest of the widget.

pingbird avatar May 11 '24 16:05 pingbird

Just to confirm this is what expected, I might have phrased it badly at first. In BoxyDelegate.paint(), The path argument provided to layers.clipPath doesn't automatically get global offset added.(it's difficult to get the global offset in paint() since BoxyDelegate.paintOffset is being masked.)

However in BoxyDelegate.paintChildren() layers.clipPath does apply the global offset to the path argument.

PurplePolyhedron avatar May 12 '24 07:05 PurplePolyhedron