Flutter 3.x+Live previewCopy-paste Dart

Flutter Heart Shape

A heart is six cubic Bezier curves. The code below draws one inside whatever rect it gets, and the size slider shrinks it inside that rect. Use the CustomPainter for a filled or outlined heart, ClipPath to cut an image, or the ShapeBorder for a button or card.

Style
Flutter CustomPainter Code
import 'package:flutter/material.dart';

// A heart drawn with six cubic curves. [scale] shrinks it inside the rect.
Path heartPath(Rect r) {
  const scale = 1.0;
  final box = Rect.fromCenter(
    center: r.center,
    width: r.width * scale,
    height: r.height * scale,
  );
  double x(double f) => box.left + box.width * f;
  double y(double f) => box.top + box.height * f;

  return Path()
    ..moveTo(x(0.5), y(0.3))
    ..cubicTo(x(0.5), y(0.14), x(0.36), y(0.04), x(0.24), y(0.04))
    ..cubicTo(x(0.08), y(0.04), x(0.0), y(0.2), x(0.0), y(0.34))
    ..cubicTo(x(0.0), y(0.62), x(0.36), y(0.78), x(0.5), y(0.94))
    ..cubicTo(x(0.64), y(0.78), x(1.0), y(0.62), x(1.0), y(0.34))
    ..cubicTo(x(1.0), y(0.2), x(0.92), y(0.04), x(0.76), y(0.04))
    ..cubicTo(x(0.64), y(0.04), x(0.5), y(0.14), x(0.5), y(0.3))
    ..close();
}

class HeartPainter extends CustomPainter {
  const HeartPainter({this.color = const Color(0xFFEF4444)});

  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;
    final rect = Offset.zero & size;
    canvas.drawPath(heartPath(rect), paint);
  }

  @override
  bool shouldRepaint(HeartPainter oldDelegate) => oldDelegate.color != color;
}

// Usage:
// const CustomPaint(
//   size: Size(120, 110),
//   painter: HeartPainter(),
// )

When to use ClipPath vs ShapeBorder for this shape

Use the CustomPainter for a standalone heart: a like button, a rating icon or a decoration. It supports fill or outline directly.

Use ClipPath to cut content into a heart, and the ShapeBorder when the heart is the surface of a Card or Material and needs a shadow or ripple.

Common mistakes

  • Stroking the outline without deflating the rect. Half the stroke falls outside the bounds and is cut off. The outline version deflates by half the stroke width.
  • Stretching it with the wrong aspect ratio. The heart fills the rect it is given, so a tall or very wide box distorts it. The preview uses a box slightly wider than tall (12:11).
  • Using Paint() with the default style and expecting an outline. The default is fill. Set PaintingStyle.stroke and a strokeWidth.
  • Hard-coding pixel sizes. The curve points are fractions of the rect, so the heart scales with the widget.

FAQ

How do I draw a heart in Flutter?

Use Path.cubicTo for the two lobes and the point at the bottom, then close(). Draw it with a CustomPainter and canvas.drawPath, or return it from a CustomClipper<Path>.

How do I make an outlined heart?

Set PaintingStyle.stroke and a strokeWidth on the Paint, and deflate the rect by half the stroke width so the line is not cut off. Switch the style to Outline above to see that version of the code.

Why not use Icons.favorite?

Icons.favorite is the simplest option for an icon. A custom path is better when you need to clip an image, add a border, or draw the heart at a size and style an icon font cannot give you.

Related