Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to give specific type of border to my container's corner in flutter. I don't want it to be rounded

Reference Image.

I currently have this code where I am trying to apply a specific type of corner to a container.

class TestCode extends StatelessWidget {
  const TestCode({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 358,
          height: 177,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(topLeft: Radius.circular(30)),
            color: Color(0x6b000000),
            shape: BoxShape.rectangle,
          ),
        ),
      ),
    );
  }
}

What I want to do is not to have rounded corners but cut off corners in a container.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use CustomPainter, first crate this class:

class CustomDraw extends CustomPainter {
  late Paint painter;

  CustomDraw(BuildContext buildContext, Color color) {
    painter = Paint()
      ..color = color
      ..style = PaintingStyle.fill;
  }

  @override
  void paint(Canvas canvas, Size size) {
    var path = Path();

    path.moveTo(0, size.height * 0.2);
    path.lineTo(size.height * 0.2, 0);
    path.lineTo(size.width, 0);
    path.lineTo(size.width, size.height * 0.8);
    path.lineTo(size.width - size.height * 0.2, size.height);
    path.lineTo(0, size.height);

    path.close();

    canvas.drawPath(path, painter);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}

then use it like this:

Container(
   clipBehavior: Clip.antiAlias,
   decoration: BoxDecoration(),
   child: CustomPaint(
      size: Size(300, 100),
      painter: CustomDraw(context, Colors.red),
   ),
),

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading