Flutter 3.x+Live previewCopy-paste Dart

Flutter Half Circle (Semicircle) Shape

A semicircle is half of an oval: Path.arcTo draws it in one call. Pick which half you want below. The same code works for domes, bowls, tabs and gauge backgrounds, because it fills whatever rect it is given.

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

// The top half (a dome; the flat edge is at the bottom).
// The oval is twice the rect on one axis, so the arc ends exactly on the rect edges.
Path halfCirclePath(Rect r) {
  return Path()
    ..arcTo(
      Rect.fromLTWH(r.left, r.top, r.width, r.height * 2),
      math.pi,
      math.pi,
      true,
    )
    ..close();
}

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

  @override
  Path getClip(Size size) => halfCirclePath(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 HalfCircleClipper(),
//   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 content, for example an image banner that ends in a dome.

Use the ShapeBorder for a surface with its own shadow or ripple, such as a Material tab or a Card. For a plain filled half circle with no clipping, BorderRadius can also do it: a box twice as wide as it is tall with BorderRadius.vertical(top: Radius.circular(height)) is a dome.

Common mistakes

  • Giving the arc a rect of the same size as the widget. For a half circle the oval must be twice the rect on one axis, otherwise you draw a quarter or a bulge. The code above builds the oval for you.
  • Mixing up the start angle. Flutter angles run clockwise from 3 o'clock, so math.pi is 9 o'clock and 0 is 3 o'clock. A sweep of math.pi then covers exactly half the oval.
  • Forgetting forceMoveTo: true and getting a stray line from the previous point. With a fresh Path() the first arcTo should start with a move.
  • Expecting a perfect circle from a non-square box. The curve is an ellipse that fills the rect, so use a 2:1 box (or 1:2 for left and right) for a true semicircle.

FAQ

How do I make a semicircle in Flutter?

Use Path.arcTo with an oval rect that is twice as tall (or wide) as the shape, a start angle, and a sweep of math.pi. The code above picks the right oval and start angle for the half you choose.

Can I make a half circle with BorderRadius?

Yes for a filled box: a container 200 wide and 100 tall with BorderRadius.vertical(top: Radius.circular(100)) is a dome. Use a path when you need to clip content or give a custom ShapeBorder its own shadow.

How do I flip the half circle?

Change which half you draw. The selector above switches between top, bottom, left and right, and the code changes the oval rect and the start angle to match.

Related