Mastering Smooth Corners in Flutter: Replicating iOS Superellipse Shapes
The Flutter news you actually need
No spam, ever. Unsubscribe in one click.
Beyond BorderRadius: Crafting iOS-Style Smooth Corners in Flutter
If you’ve ever admired the sleek, subtly rounded corners on iOS app icons, buttons, or cards, you’ve seen a superellipse (often called a “squircle”) in action. These aren’t your standard circular arcs; they are a specific mathematical shape that provides a smoother, more visually pleasing transition from edge to curve.
In Flutter, the go-to solution for rounded corners is the BorderRadius class. While powerful, it creates simple circular arcs. Try using a large BorderRadius.circular() value on a rectangle, and you’ll see the classic “bubble” look, not the refined iOS aesthetic. This leaves many developers wondering how to bridge that gap.
Let’s explore how to master these smooth corners in your own Flutter apps.
The Problem with BorderRadius.circular()
First, let’s visualize the difference. Here’s a standard Flutter container with a large circular radius:
Container(
width: 200,
height: 100,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(30.0),
),
)
This creates a shape where the curvature is constant. The corner arc is a perfect quarter-circle. For smaller radii, this is fine. But as the radius increases relative to the shape’s size, it can look inflated and less integrated with the straight edges. The iOS superellipse curve changes its tightness, blending more seamlessly.
Solution 1: Approximating with BorderRadius.elliptical()
Flutter’s BorderRadius.elliptical() allows you to define independent radii for the horizontal and vertical axes of the corner ellipse. By making these elliptical radii large, we can approximate a superellipse effect.
Container(
width: 200,
height: 60,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.elliptical(50, 30),
),
child: Center(child: Text('Smooth Container')),
)
Experimenting with the two values lets you control the “smoothness” and achieve a look closer to the desired shape.
Solution 2: The Custom Superellipse Clipper
For maximum control and a true superellipse, we can implement a custom CustomClipper<Path>. The superellipse formula uses a n value for “corner smoothness”.
Here’s a practical clipper:
import 'dart:math';
class SuperellipseClipper extends CustomClipper<Path> {
final double n; // Smoothness parameter. 4.0 is a common start.
SuperellipseClipper({this.n = 4.0});
@override
Path getClip(Size size) {
final path = Path();
final width = size.width;
final height = size.height;
final halfWidth = width / 2;
final halfHeight = height / 2;
// Move to the top-middle of the shape
path.moveTo(halfWidth, 0);
// Top-right corner
for (var i = 0; i <= halfWidth; i++) {
final x = halfWidth + i;
final y = halfHeight * pow(1 - pow(i / halfWidth, n), 1 / n);
path.lineTo(x, y);
}
// Bottom-right corner
for (var i = 0; i <= halfHeight; i++) {
final y = halfHeight + i;
final x = halfWidth * pow(1 - pow(i / halfHeight, n), 1 / n);
path.lineTo(x, y);
}
// Bottom-left corner
for (var i = 0; i <= halfWidth; i++) {
final x = halfWidth - i;
final y = halfHeight * pow(1 - pow(i / halfWidth, n), 1 / n);
path.lineTo(x, height - y);
}
// Top-left corner
for (var i = 0; i <= halfHeight; i++) {
final y = halfHeight - i;
final x = halfWidth * pow(1 - pow(i / halfHeight, n), 1 / n);
path.lineTo(width - x, y);
}
path.close();
return path;
}
@override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) => false;
}
Use it with a ClipPath widget:
ClipPath(
clipper: SuperellipseClipper(n: 4.0),
child: Container(
width: 300,
height: 150,
color: Colors.green,
child: Center(child: Text('True Superellipse')),
),
)
Note: The n value dramatically affects the shape. Start with n = 4.0 and adjust.
Solution 3: Leveraging a Package
A reliable package can save time. The superellipse_shape package provides a SuperellipseShape that works with Material widgets.
First, add it to your pubspec.yaml:
dependencies:
superellipse_shape: ^2.0.0
Then use it:
import 'package:superellipse_shape/superellipse_shape.dart';
Material(
shape: SuperellipseShape(
borderRadius: BorderRadius.circular(40.0),
),
child: Container(
width: 200,
height: 80,
padding: EdgeInsets.all(16),
child: Center(child: Text('Package Power')),
),
)
Choosing Your Approach
- For quick approximations: Use
BorderRadius.elliptical(). - For full control: Implement a custom
SuperellipseClipper. - For production convenience: Use the
superellipse_shapepackage.
Implementing these smooth corner shapes can elevate the polish of your Flutter app.
This blog is produced with the assistance of AI by a human editor. Learn more
Related Posts
Fixing Flutter TextField Issues: Preventing Input Lag, Lost Keystrokes, and Keyboard Glitches
Many Flutter developers struggle with common TextField issues like typing lag, lost keystrokes, or erratic keyboard behavior. This post will diagnose the root causes of these problems, including state management, TextEditingController misuse, and custom decorations, offering practical solutions for a smooth and reliable user input experience.
Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data
Developers frequently struggle with persistent storage in Flutter, especially when dealing with OS-level backups and app reinstalls. This post will clarify the nuances of different storage solutions like `SharedPreferences`, `path_provider`, and `flutter_secure_storage`, focusing on how to correctly manage data lifetimes (per-install vs. per-user) and avoid issues like device IDs migrating to new phones or data being unexpectedly backed up.
Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring
Flutter's `ImageFiltered` can cause significant lag when blurring large or frequently changing content like text. This post will explain the performance bottlenecks, provide solutions like caching blurred outputs with `RepaintBoundary` or `SnapshotWidget`, and offer practical tips to ensure smooth UI effects without sacrificing frame rates.