fix transform on canvas children not working with scroll views
This commit is contained in:
@@ -28,6 +28,28 @@ class _SimpleExampleState extends State<SimpleExample> {
|
||||
),
|
||||
);
|
||||
}
|
||||
controller.addChild(
|
||||
const Offset(0, 200),
|
||||
Container(
|
||||
width: 300,
|
||||
height: 150,
|
||||
color: Colors.grey[200],
|
||||
child: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
'This is a very long text inside a scrollable container. '
|
||||
'You can scroll to see more content. '
|
||||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
|
||||
'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. '
|
||||
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. '
|
||||
'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
+14
-6
@@ -246,14 +246,22 @@ class _CanvasRenderBox extends RenderBox
|
||||
while (child != null) {
|
||||
final _CanvasWidgetParentData childParentData = child.parentData! as _CanvasWidgetParentData;
|
||||
|
||||
context.canvas.save();
|
||||
// relying purely on canvas manips here
|
||||
final drawAt = canvasStartOffset + childParentData.offset;
|
||||
context.canvas.translate(drawAt.dx, drawAt.dy);
|
||||
context.canvas.scale(childParentData.scale, childParentData.scale);
|
||||
context.paintChild(child, Offset.zero); // paint at 0 as already translated
|
||||
|
||||
context.canvas.restore();
|
||||
// Apply transformation using pushTransform for proper coordinate handling
|
||||
final transform = Matrix4.identity()
|
||||
..translate(drawAt.dx, drawAt.dy)
|
||||
..scale(childParentData.scale, childParentData.scale);
|
||||
|
||||
// Note: initially I was using context.canvas.translate and context.canvas.scale
|
||||
// but that doesn't work with say using a SingleChildScrollView inside a child
|
||||
// so using pushTransform is the way to go here
|
||||
|
||||
// 0 offset since transform will take care of it
|
||||
context.pushTransform(needsCompositing, Offset.zero, transform, (context, offset) {
|
||||
context.paintChild(child!, Offset.zero);
|
||||
});
|
||||
|
||||
child = childParentData.nextSibling;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user