Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a line with a pointed triangle in Flutter?

I am looking at implementing the following design.

enter image description here

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.

like image 838
Panduranga Rao Sadhu Avatar asked Jan 25 '23 15:01

Panduranga Rao Sadhu


1 Answers

Its easy, just you need to understand how to use clippers.

Here is how :

u need to use 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.

like image 120
Sahdeep Singh Avatar answered Feb 11 '23 23:02

Sahdeep Singh