Search code examples
flutterdartflutter-dependencies

How can I make such clip circles in flutter?


I want to do the same thing.Firstly I did it with stack widget.However the result is not desired.I want to make these circles transparent.How can I do it?

enter image description here

I used custom_clippers 2.0.0 package.However I could not find the same pattern inside of this package.


Solution

  • I think the ClipPath is the thing you need.

    Here is an exemple that should gives you similar result using:

    import 'package:flutter/cupertino.dart';
    
    
    
    class MyClipper extends CustomClipper<Path> {
      MyClipper({this.position, this.holeRadius = 16});
    
      double? position;
      final double holeRadius;
    
      @override
      Path getClip(Size size) {
        final path = Path()
          ..moveTo(0, 0)
          ..lineTo(size.width, 0.0)
          ..lineTo(size.width, position! - holeRadius)
          ..arcToPoint(
            Offset(size.width, position!),
            clockwise: false,
            radius: const Radius.circular(1),
          )
          ..lineTo(size.width, size.height)
          ..lineTo(0, size.height)
          ..lineTo(0, position!)
          ..arcToPoint(
            Offset(0, position! - holeRadius),
            clockwise: false,
            radius: const Radius.circular(1),
          );
    
        path.lineTo(0.0, size.height);
    
        path.close();
        return path;
      }
    
      @override
      bool shouldReclip(CustomClipper oldClipper) => oldClipper != this;
    }
    

    And you can use it like following:

    ClipPath(
        clipper: MyClipper(position: 50,holeRadius: 20),
        child: Container(
        height: 200,
        color: Colors.green,
        ),
       )
    

    Result:

    enter image description here

    It is a very minimal exemple, you can modify it to achieve your result.