简体   繁体   English

绘制自定义形状 Flutter

[英]Draw Custom shape Flutter

I'm trying to draw the shape in this image The shape that I want to draw Put I can't get the same result by my code:我正在尝试在此图像中绘制形状我想要绘制的形状放置我的代码无法获得相同的结果:

CustomShapeClipper extends CustomClipper<Path> {
@override

Path getClip(Size size) {

final Path path = Path()

..moveTo(0, size.height * 0.6)

..quadraticBezierTo(

size.width * 0.7 , size.height - (size.height * 0.1) ,

size.width, size.height * 0.8

)

..lineTo(size.width, 0)

..lineTo(0, 0)

..close();


return path;

}


@override

bool shouldReclip(CustomClipper oldClipper) => true;


}

I get this result My Result我得到这个结果我的结果

Kindly guide me for this.请指导我。

Thanks in advance提前致谢

Try this and let me know if it works for you.试试这个,让我知道它是否适合你。

In your widget body you can have a build method looking similar to this在您的小部件正文中,您可以有一个类似于此的构建方法

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          ClipPath(
            clipper: BottomEndClipper(),
            child: Container(
              height: MediaQuery.of(context).size.height * .5,
              decoration: BoxDecoration(
               //Your gradient or own color 
                color: Colors.purple,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

And your custom clipper looking like this你的自定义剪裁器看起来像这样

class BottomEndClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();
    path.lineTo(0, size.height - 80);
    path.lineTo(size.width * .7, size.height - 10);
    path.quadraticBezierTo(
        size.width * .8, size.height, size.width * .95, size.height * .90);

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


    path.close();
    return path;
  }
  //Should return false if you don't wish to redraw every time
  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

I managed to draw something similar using cubicTo instead of quadraticBezierTo .我设法使用cubicTo而不是quadraticBezierTo绘制了类似的东西。 A simple example for what you need:您需要的一个简单示例:

final Path path = Path()
      ..moveTo(0, size.height * 0.6)
      ..lineTo(size.width * 0.7 - (size.width * 0.05),
          size.height - 2 * (size.height * 0.1))
      ..cubicTo(
          size.width * 0.7 - (size.width * 0.01),
          size.height - 1.88 * (size.height * 0.1),
          size.width * 0.7 + (size.width * 0.01),
          size.height - 1.88 *  (size.height * 0.1),
          size.width * 0.7 + (size.width * 0.05),
          size.height - 2 * (size.height * 0.1))
      ..lineTo(size.width, size.height * 0.7)
      ..lineTo(size.width, 0)
      ..lineTo(0, 0)
      ..close();

I know that there are a lot of numbers, but you can extract some points as separate variables for a better readability.我知道有很多数字,但是您可以提取一些点作为单独的变量以提高可读性。 Practically instead of drawing a Quadric Bezier, we draw 2 lines and the curve between them.实际上,我们不是绘制二次贝塞尔曲线,而是绘制 2 条线和它们之间的曲线。

Also you can add clipBehavior: Clip.antiAliasWithSaveLayer to your ClipPath for a smooth drawing您还可以将clipBehavior: Clip.antiAliasWithSaveLayer添加到您的ClipPath以实现平滑绘图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM