如何在颤动中镜像路径?

How to mirror path in flutter?

所以我有一个在容器中绘制路径的代码。

 void paintWithPattern(
      Canvas canvas, double x, double y, double width, double height) {
    final maxDimension = max(width, height);
    final stripeW = maxDimension / featuresCount / 6;
    var step = stripeW * 9;
    final paint = Paint()
      ..style = PaintingStyle.fill
      ..color = bgColor;
    canvas.drawRect(Rect.fromLTWH(x, y, width, height), paint);
    final rectStripesCount =
        featuresCount * 2.5; // to make sure we cover the whole rectangle
    final allStripesPath = Path();
    for (var i = 1; i < rectStripesCount; i += 2) {
      final stripePath = Path();
      stripePath.moveTo(x - stripeW + step, y);
      stripePath.lineTo(x + step, y);
      stripePath.lineTo(x, y + step);
      stripePath.lineTo(x - stripeW, y + step);
      stripePath.close();
      allStripesPath.addPath(stripePath, Offset.zero);
      step += stripeW * 9;
    }
    paint
      ..style = PaintingStyle.fill
      ..color = fgColor;
    canvas.drawPath(allStripesPath, paint);
  }

容器上的代码绘制结果如下

怎么才能做到Y轴镜像? 如下图所示

您可以使用负缩放和平移(以校正位置)来做到这一点,就像 Y 轴这样:

canvas.scale(1, -1);
canvas.translate(0, -height);
canvas.drawPath(allStripesPath, paint);

对于 X 轴:

canvas.scale(-1, 1);
canvas.translate(-width, 0);
canvas.drawPath(allStripesPath, paint);

文档:

https://api.flutter.dev/flutter/dart-ui/Canvas/scale.html

https://api.flutter.dev/flutter/dart-ui/Canvas/translate.html