Flutter 3.x+Live previewCopy-paste Dart

Flutter Star Shape

A star is a polygon with alternating outer and inner vertices. Choose the number of points, how deep the notches go (the inner radius ratio) and a rotation, and copy the code. Icons.star is fine for a fixed five-point icon; this is for stars you need to size, clip to, or vary.

Flutter ClipPath + CustomClipper Code
import 'dart:math' as math;
import 'package:flutter/material.dart';

// A 5-point star: outer and inner vertices alternate around the center.
Path starPath(Rect r) {
  const pointCount = 5;
  const innerRatio = 0.5;
  const rotation = 0 * math.pi / 180; // 0 puts a point at the top
  final center = r.center;
  final outer = math.min(r.width, r.height) / 2;
  final inner = outer * innerRatio;
  final points = <Offset>[
    for (var i = 0; i < pointCount * 2; i++)
      center +
          Offset.fromDirection(
            rotation + math.pi * i / pointCount - math.pi / 2,
            i.isEven ? outer : inner,
          ),
  ];
  return Path()..addPolygon(points, true);
}

class StarClipper extends CustomClipper<Path> {
  const StarClipper();

  @override
  Path getClip(Size size) => starPath(Offset.zero & size);

  // The shape never changes, so there is nothing to reclip.
  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) => false;
}

// Usage:
// ClipPath(
//   clipper: const StarClipper(),
//   child: Image.asset('assets/avatar.jpg', width: 160, height: 160, fit: BoxFit.cover),
// )

When to use ClipPath vs ShapeBorder for this shape

Use ClipPath to cut an image or gradient into a star.

Use the ShapeBorder for a badge, button or Card that should keep its shadow, border and ink ripple in the star shape.

Common mistakes

  • Setting the inner radius ratio close to 1. The star flattens into a polygon. Around 0.4 to 0.5 gives the classic five-point look.
  • Forgetting the quarter turn. Angles start at 3 o'clock, so the code subtracts math.pi / 2 to put a point at the top.
  • Drawing a pentagram (connecting every second vertex of a pentagon) and being surprised by the empty center. That outline crosses itself, so the fill rule decides whether the middle is filled. The star above alternates outer and inner vertices and has no such crossing; the fillType article below explains the rule.
  • Clipping a widget that has a shadow. Use the ShapeBorder version instead.

FAQ

How do I make a star shape in Flutter?

Compute 2 * points vertices around a center, alternating between an outer and an inner radius, and add them with Path.addPolygon. The code above does that and exposes the point count and inner ratio.

How do I change the number of points?

Change the pointCount constant. The points slider shows 3 to 12, and the code uses the same loop for any count.

Should I use Icons.star instead?

For a fixed, filled five-point icon the built-in icon is simpler. Use a custom path when you need a different point count, a rounded or thin star, or a shape to clip or give a border.

Related