Search code examples
flutterdartflutter-layout

How to paint arc or moon shape over another circle in flutter


I really need your help to create or paint this kind of container in a flutter,

What I Want is below pic as a result

enter image description here

this is to show user profiles or different users inside this container like this, please help me out with how to do this I would really like you to appreciate your help, thanks in advance

What I did so far:

 Container(
                              decoration: BoxDecoration(
                                  gradient: LinearGradient(
                                    begin: Alignment.topRight,
                                    end: Alignment.bottomLeft,
                                    colors: [
                                      Color(0XFF8134AF),
                                      Color(0XFFDD2A7B),
                                      Color(0XFFFEDA77),
                                      Color(0XFFF58529),
                                    ],
                                  ),
                                  shape: BoxShape.circle),
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  shape: BoxShape.circle,
                                  image: DecorationImage(
                                    image: AssetImage(
                                      IconAssets.user_icon,
                                    ),
                                    fit: BoxFit.cover,
                                  ),
                                ),
                                margin: EdgeInsets.all(AppMargin.m2),
                                padding: const EdgeInsets.all(AppPadding.p3),
                              ),
                            ),

Result:

enter image description here

please help me out how to create like above picture , thank you


Solution

  • I am using PathOperation.difference to paint the moon.

    class MoonPainter extends CustomPainter {
      @override
      void paint(Canvas canvas, Size size) {
        final center = Offset(size.width / 2, size.height / 2);
        final paint = Paint()
          ..shader = const LinearGradient(
            begin: Alignment.topRight,
            end: Alignment.bottomLeft,
            colors: [
              Color(0XFF8134AF),
              Color(0XFFDD2A7B),
              Color(0XFFFEDA77),
              Color(0XFFF58529),
            ],
          ).createShader(Rect.fromLTRB(0, 0, size.width, size.height));
    
        Path path1 = Path()
          ..addOval(Rect.fromCenter(
              center: center, width: size.width, height: size.height));
    
        Path path2 = Path()
          ..addOval(
            Rect.fromCenter(
              center: Offset(
                center.dx - 10,
                center.dy - 10,
              ),
              width: size.width - 10,
              height: size.height - 10,
            ),
          );
        canvas.drawPath(
          Path.combine(PathOperation.difference, path1, path2),
          paint,
        );
      }
    
      @override
      bool shouldRepaint(covariant CustomPainter oldDelegate) {
        return false;
      }
    }
    

    enter image description here

    SizedBox(
      width: 300,
      height: 300,
      child: CustomPaint(
        painter: MoonPainter(),
      ),
    ),
    

    You can include another oval inside paint. Change the color and decorate the way you like