简体   繁体   English

如何在 Flutter 中画一条带尖三角形的线?

[英]How to draw a line with a pointed triangle in Flutter?

I am looking at implementing the following design.我正在考虑实施以下设计。

在此处输入图像描述

How do I achieve the triangular bump on the line as in the image above?如上图所示,如何实现线上的三角形凹凸? I am new to flutter and am clueless on how to get started on this.我是 flutter 的新手,对如何开始这一点一无所知。

Its easy, just you need to understand how to use clippers.很简单,只需要了解如何使用快船。

Here is how:方法如下:

u need to use ClipPath你需要使用ClipPath


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.lightGreen,
      appBar: AppBar(
        title: Text("test"),
        backgroundColor: Colors.deepOrange,
      ),
      body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Container(
            width: double.infinity,
            height: 200,
            color: Colors.red,
            child: Center(
              child: Text("Download"),
            ),
          ),
          ClipPath(
            clipper: TriangleClipper(),
            child: Container(
              color: Colors.red,
              height: 10,
              width: 20,
            ),
          )
        ],
      )),
    );
  }

And add your custom clipper:并添加您的自定义剪辑器:

class TriangleClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    final path = Path();
    path.lineTo(size.width, 0.0);
    path.lineTo(size.width / 2, size.height);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(TriangleClipper oldClipper) => false;
}

Thats it you will get the same result.就是这样,您将得到相同的结果。

That's it而已

   ClipPath(
                clipper: ClipperStack(),
                child: Container(
                  height: 100,
                  color: Colors.pink,
                  child: Center(child: Text("DOWNLOAD")),
                ),
              ),

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

    path.moveTo(0.0, 0.0);
    path.lineTo(0.0, size.height-10);
    path.lineTo((size.width / 2 )-5, size.height-10);


    path.lineTo(size.width / 2, size.height);


    path.lineTo((size.width / 2)+5, size.height-10);
    path.lineTo(size.width, size.height-10);


    path.lineTo(size.width, 0.0);

    return path;
  }

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

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

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