如何在Flutter中绘制带有尖头三角形的线?

时间:2019-11-14 09:26:40

标签: flutter flutter-layout

我正在考虑实施以下设计。

enter image description here

如何实现上图中的三角形凸起?我是新手,对如何开始对此一无所知。

2 个答案:

答案 0 :(得分:1)

这很简单,只需要您了解如何使用推子即可。

方法如下:

您需要使用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,
            ),
          )
        ],
      )),
    );
  }

并添加您的自定义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;
}

就这样,您将获得相同的结果。

答案 1 :(得分:1)

就这样

   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;
}