How I Drew a Product Bottle in Flutter with CustomPainter (No Images)
Draw a realistic product bottle in Flutter with CustomPainter: a Path with cubic curves, gradient shaders for matte metal, clipPath details and a floating shadow. Step by step, with the full code from my water bottle shop UI.
By Yaqoob Developer · · 9 min read
- #Flutter
- #CustomPainter
- #Animation
- #Custom Shapes
- #UI Design
For my Water Bottle Shop Product Showcase UI, I didn't use a single image for the product. The matte black bottle, its steel cap, the faceted base and the shadow under it are all drawn in code with Flutter's CustomPainter.
I chose code over a PNG because it gives much more flexibility: you can change the colors and proportions in a few lines, the bottle stays sharp on every screen, and you can put your own brand text on it. When I posted the screen as a YouTube Short, people were interested in the animation and the custom bottle, and some asked how I made it in Flutter. This post is the answer, step by step, using the exact code from the project.
Try it first: open the live preview or watch the YouTube Short. The full project is on GitHub.
What we are building
A Bottle widget that you give a height, and it draws:
- A body with a narrow neck, curved shoulders and rounded bottom corners
- A gradient that makes the body look lit from the left
- A faint sheen near the top and four dark wedges at the base
- A brushed steel cap with a bright rim
- A soft shadow on the floor that reacts when the bottle floats
The only package in the project is google_fonts, for the text. Everything else is plain Flutter.
CustomPainter in one minute
A CustomPainter is a class with a paint(Canvas canvas, Size size) method. Flutter gives you a canvas and the size of the area, and you draw on it: rectangles, ovals, lines and, most usefully, paths.
You put it on screen with the CustomPaint widget:
CustomPaint(painter: MyPainter())
The one rule that makes painters easy: never use fixed pixel values for the shape. Work in fractions of size.width and size.height. Then the same painter draws a small bottle or a huge one without changes.
CustomPainter was a real learning experience for me in this project. It is what made custom shapes in Flutter click for me, and the bottle is a good first shape because it is symmetrical and made of only a few curves.
Step 1: The widget and its proportions
The bottle's width is about a third of its height, so the widget only needs a height:
class Bottle extends StatelessWidget {
const Bottle({super.key, required this.height, this.lift = 0});
final double height;
final double lift;
static const aspect = 0.34;
lift is used later for the floating animation. Ignore it for now.
Inside the painter, the important proportions are constants, as fractions of the bottle's box:
class _BottlePainter extends CustomPainter {
const _BottlePainter();
// Proportions of the bottle, as fractions of its box.
static const _capBottom = 0.15;
static const _capInset = 0.19;
static const _shoulderEnd = 0.43;
_capBottom: the cap takes the top 15% of the height._capInset: the neck starts 19% in from each side._shoulderEnd: the shoulders finish and the straight body starts at 43% of the height.
Changing these three numbers changes the whole character of the bottle, which is exactly the flexibility I wanted.
Step 2: Draw the body with a Path
This is the heart of the bottle. The path starts at the top-left of the neck, goes right, curves down the right shoulder, runs down the side, rounds the bottom corners and comes back up the left side:
Path _body(Size s) {
final w = s.width;
final h = s.height;
final neckL = w * _capInset;
final neckR = w - neckL;
final top = h * (_capBottom - 0.01);
final corner = w * 0.12;
return Path()
..moveTo(neckL, top)
..lineTo(neckR, top)
// Right shoulder: drops straight, then swells out to full width.
..cubicTo(neckR, h * 0.24, w, h * 0.30, w, h * _shoulderEnd)
..lineTo(w, h - corner)
..quadraticBezierTo(w, h, w - corner, h)
..lineTo(corner, h)
..quadraticBezierTo(0, h, 0, h - corner)
..lineTo(0, h * _shoulderEnd)
..cubicTo(0, h * 0.30, neckL, h * 0.24, neckL, top)
..close();
}
The shoulder is a cubicTo, which takes two control points and an end point:
- The first control point
(neckR, h * 0.24)is straight below the neck. It makes the curve start by going down, not sideways. - The second control point
(w, h * 0.30)is on the right edge, a bit above the end. It makes the curve arrive at the side vertically, so it blends into the straight body.
The bottom corners use quadraticBezierTo with the corner point as the control point. That is the simplest way to round a corner on a path.
From my experience: getting the shoulder curve right was a little challenging. I had to adjust the shape a few times before it looked smooth.
If your shoulder looks like a bump, check that both control points line up with the straight lines on either side: one directly below the neck, one on the side of the body.
Step 3: Make it look like matte metal with gradients
A flat black shape looks like a silhouette. A LinearGradient turned into a shader makes it look round and lit from the left:
final bodyRect = Offset.zero & size;
canvas.drawPath(
body,
Paint()
..shader = const LinearGradient(
colors: [
SwellColors.bodyEdge,
SwellColors.bodyLight,
SwellColors.bodyMid,
SwellColors.bodyDark,
],
stops: [0, 0.28, 0.6, 1],
).createShader(bodyRect),
);
The trick is in the order: it starts dark at the very edge, gets lightest at 28% (where the light hits), and then falls off into almost black on the right. Real round objects have a dark edge even on the lit side, and that is what sells the 3D look.
Before the gradient, one line adds depth around the whole shape:
canvas.drawShadow(body, Colors.black, 18, false);
Step 4: Sheen and faceted base with clipPath
The next details must stay inside the bottle. Instead of building more complicated paths, I clip the canvas to the body and then draw simple shapes:
canvas.save();
canvas.clipPath(body);
canvas.drawRect(
bodyRect,
Paint()
..shader = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.white.withValues(alpha: 0.06),
Colors.transparent,
Colors.transparent,
Colors.black.withValues(alpha: 0.25),
],
stops: const [0.15, 0.45, 0.85, 1],
).createShader(bodyRect),
);
_drawFacets(canvas, size);
canvas.restore();
save() and restore() matter: everything between them is clipped to the bottle, and everything after them (the cap) is not.
The vertical gradient gives a faint white sheen at the shoulders and darkens the bottom. Then the facets are four triangles along the base:
void _drawFacets(Canvas canvas, Size size) {
final w = size.width;
final h = size.height;
final top = h * 0.93;
final paint = Paint()..color = Colors.black.withValues(alpha: 0.18);
const peaks = [0.0, 0.25, 0.5, 0.75, 1.0];
for (var i = 0; i < peaks.length - 1; i++) {
final l = w * peaks[i];
final r = w * peaks[i + 1];
canvas.drawPath(
Path()
..moveTo(l, h)
..lineTo((l + r) / 2, top)
..lineTo(r, h)
..close(),
paint,
);
}
}
Because the canvas is clipped, the outer triangles are cut cleanly by the rounded corners. Without the clip they would stick out of the bottle.
Step 5: The brushed steel cap
The cap is a rounded rectangle with elliptical top corners, and a metal look comes from a gradient that goes dark, light, dark across it:
canvas.drawRRect(
cap,
Paint()
..shader = const LinearGradient(
colors: [
SwellColors.capDark,
SwellColors.capMid,
SwellColors.capLight,
SwellColors.capMid,
SwellColors.capDark,
],
stops: [0, 0.22, 0.48, 0.75, 1],
).createShader(rect),
);
Two small touches finish it: a dark 3-pixel band where the cap meets the bottle, and a thin white line along the top as a highlight on the rim:
canvas.drawLine(
Offset(rect.left + w * 0.1, 1.5),
Offset(rect.right - w * 0.1, 1.5),
Paint()
..color = Colors.white.withValues(alpha: 0.7)
..strokeWidth = 1.2
..strokeCap = StrokeCap.round,
);
Since this painter has no inputs, shouldRepaint returns false, so Flutter never repaints it for no reason:
@override
bool shouldRepaint(_BottlePainter oldDelegate) => false;
Step 6: Put your brand on the bottle
This is where drawing in code really pays off. The logo isn't painted at all. The Bottle widget is a Stack, and the logo is a normal widget positioned on top of the painted body:
Positioned(
top: height * 0.78 - lift,
child: SwellLogo(size: width * 0.17, color: SwellColors.bottleLogo),
),
The size is a fraction of the bottle's width and the position is a fraction of its height, so it scales with the bottle. Replace the logo widget with a Text of your own brand name, change its color, and the product is yours.
Step 7: A floating bottle with a shadow that stays on the floor
On the product screen, the bottle floats gently up and down. A repeating AnimationController and a cosine wave give a smooth value between 0 and 1:
late final AnimationController _float = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 3200),
)..repeat();
final wave = (1 - math.cos(_float.value * 2 * math.pi)) / 2;
final bob = wave * 10 * settle;
bob is passed to the bottle as lift (up to 10 pixels). settle is 0 until the bottle has finished its entrance animation, so it only starts floating once it has landed.
From my experience: without the shadow, the bottle looked like it was just moving in the air, not floating. I wanted the animation to feel more realistic, so I added a shadow that stays on the floor and changes as the bottle lifts. The changing shadow makes the animation feel natural.
Inside the Bottle widget, only the body and the logo move up by lift. The shadow painter stays where it is:
Positioned.fill(
child: CustomPaint(painter: _ShadowPainter(lift)),
),
Positioned.fill(
top: -lift,
bottom: lift,
child: const CustomPaint(painter: _BottlePainter()),
),
And the shadow gets narrower, flatter, lighter and blurrier as the bottle rises, just like a real shadow when an object moves away from the ground:
@override
void paint(Canvas canvas, Size size) {
final w = size.width;
final t = (lift / 24).clamp(0.0, 1.0);
canvas.drawOval(
Rect.fromCenter(
center: Offset(w / 2, size.height + 4),
width: w * (1.1 - 0.3 * t),
height: 18 - 6 * t,
),
Paint()
..color = Colors.black.withValues(alpha: 0.55 - 0.25 * t)
..maskFilter = MaskFilter.blur(BlurStyle.normal, 10 + 6 * t),
);
}
@override
bool shouldRepaint(_ShadowPainter oldDelegate) => oldDelegate.lift != lift;
Unlike the bottle painter, this one depends on lift, so shouldRepaint compares it and repaints only when it changes.
Common mistakes
- Using pixel values in
paint. The shape breaks on other screen sizes. Use fractions ofsize. - Forgetting
canvas.restore()afterclipPath. Everything you draw later is clipped too, and the cap disappears. - Returning
truefromshouldRepaintevery time. It works, but repaints on every frame. Compare only the values the painter uses. - Making the shoulder with
quadraticBezierTo. One control point can't make the curve start downward and end downward. A cubic curve can. - Moving the shadow together with the bottle. It looks like a sticker sliding around. Keep the shadow on the floor and change its size instead.
Summary
- Draw the bottle with a
CustomPainterand aPath, using fractions of the size. - Make smooth shoulders with
cubicTo, with control points lined up with the neck and the side. - Fake light and metal with
LinearGradientshaders: dark edge, light spot, dark fall-off. - Use
clipPathbetweensave()andrestore()for details that must stay inside the shape. - Put the logo on top as a normal widget, so your brand is a one-line change.
- For floating, move the bottle and keep the shadow on the floor, shrinking as it lifts.
See it in action in the live preview and get the full screen from the Water Bottle Shop Product Showcase UI project. If you want to practise shadows without writing code, try my Flutter BoxShadow Generator, and for more animation basics read Flutter animations for beginners.
Keep reading
More articles
· 7 min read
How I Update an Old Flutter Project's Packages Without Breaking It
A step-by-step way to upgrade packages in an old Flutter project: read flutter pub outdated, fix version solving failed, upgrade Firebase packages together and replace discontinued packages.
· 10 min read
Swipe to Log In Button in Flutter (No Package)
Build a swipe to log in button in Flutter from scratch: drag the knob, spring back under 80%, show a check on success. Step by step, with the full code from my crypto wallet login UI.
· 6 min read
How to Publish a Flutter App on Google Play: Step-by-Step Checklist
A complete checklist for publishing your Flutter app on Google Play: app signing, the release App Bundle, store listing images, Data safety, content rating, testing tracks and the 12-tester rule for new accounts.