【问题标题】:How to set the start point of clippath如何设置剪辑路径的起点
【发布时间】:2020-04-30 10:48:04
【问题描述】:

我即将使用ClipPath() 制作一个形状。但是当我绘制它时,它似乎是从其父级的(0.0)坐标开始的。但我希望它从另一个点开始(size.width * 0.25, size.height)。我想实现这个:

代码如下:

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

    class TheHill extends CustomClipper<Path> {
      @override
      Path getClip(Size size) {
        var path = Path();

        path.moveTo(size.width * 0.15, size.height);

        path.quadraticBezierTo(size.width * 0.25, size.height * 0.99999,
            size.width * 0.40, size.height * 0.92);

        path.quadraticBezierTo(size.width * 0.5, size.height * 0.87,
            size.width * 0.60, size.height * 0.92);

        path.quadraticBezierTo(size.width * 0.75, size.height * 0.99999,
            size.width * 0.85, size.height);

        path.lineTo(size.width, size.height);
        path.lineTo(0, size.height);

        path.lineTo(size.width, 0.0);
        path.close();

        return path;
      }

      @override
      bool shouldReclip(CustomClipper<Path> oldClipper) => false;
    }

这是输出(红色括号是额外的 IDK 来自哪里):

【问题讨论】:

  • path.lineTo(0, size.height); path.lineTo(size.width, 0.0);删除这两行并尝试。
  • @VirenVVarasadiya 到处都是,除了我想去的地方
  • 你想要什么形状?
  • 上面的山形除了我标记的三角形。
  • 我已经编辑了帖子。请再看看。

标签: flutter flutter-widget


【解决方案1】:

您可以使用以下方式。

  ClipPath(
          clipper: TheHill(),
          child: Container(
            width: 250,
            height: 250,
            color: Colors.amber,
            child: Align(
              alignment: Alignment.bottomCenter,
              child: Icon(Icons.play_arrow),
            ),
          ),
        ),

自定义剪辑器:

class TheHill extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();

    path.moveTo(size.width * 0.15, size.height);

    path.quadraticBezierTo(size.width * 0.25, size.height * 0.99999,
        size.width * 0.40, size.height * 0.92);

    path.quadraticBezierTo(size.width * 0.5, size.height * 0.87,
        size.width * 0.60, size.height * 0.92);

    path.quadraticBezierTo(size.width * 0.75, size.height * 0.99999,
        size.width * 0.85, size.height);

    path.lineTo(size.width, size.height);
    path.close();

    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

【讨论】:

  • 感谢您抽出宝贵时间回答这个问题。但是代码具有我想要的完全相反的结果。我希望填充的部分清空并填充空的部分。像一座小山。
  • 好吧,我稍后会尝试制作一些动画......当使用自定义画家时,我无法做到......
  • 而且它不能分配给任何小部件,所以我只能将它用作背景
  • 我不知道如何感谢您的宝贵时间。但是我们可以坚持使用剪贴路径吗?因为这就是问题的关键。或者如果有任何问题,我会感谢你解释。
  • 我的答案仅在 clipPath 上。这行得通吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
相关资源
最近更新 更多