DEV Community

Cover image for Quick Guide to Custom Painting in Flutter
yatendra2001
yatendra2001

Posted on

Quick Guide to Custom Painting in Flutter

Flutter's custom painting capability is akin to handing Picasso a blank canvas. It's a powerful tool, enabling developers to craft intricate UI designs.

Ready to deep dive?

Let's decode the art of custom painting in Flutter.


1. The Basics of Custom Painting

At its core, custom painting in Flutter revolves around the CustomPainter class. This class provides a canvas on which you can draw your graphics, shapes, and paths.

Key Components:

  1. CustomPaint Widget: The canvas where your masterpiece will reside.
  2. CustomPainter Class: The artist's toolkit, where the drawing logic is defined.

2. Drawing Shapes and Paths

Drawing basic shapes is straightforward. Here's a breakdown:

  1. Lines: Use canvas.drawLine().
  2. Rectangles: Use canvas.drawRect().
  3. Circles: Use canvas.drawCircle().
  4. Ovals: Use canvas.drawOval().
  5. Paths: The Path class lets you create custom shapes.

Combine it with canvas.drawPath() to render it.
For instance, to draw a triangle:

Path path = Path()
  ..moveTo(size.width / 2, 0)
  ..lineTo(0, size.height)
  ..lineTo(size.width, size.height)
  ..close();

canvas.drawPath(path, paint);
Enter fullscreen mode Exit fullscreen mode

3. Gradients, Shadows, and Styles

Gradients: Flutter supports linear and radial gradients. Use ShaderMask with LinearGradient or RadialGradient.

final gradient = LinearGradient(
  colors: [Colors.red, Colors.blue],
).createShader(Rect.fromLTWH(0, 0, size.width, size.height));

paint.shader = gradient;
Enter fullscreen mode Exit fullscreen mode

Shadows: Add depth using the Shadow class. It can be applied to text or shapes.

paint.color = Colors.blue;
canvas.drawShadow(path, Colors.grey, 3.0, false);
canvas.drawPath(path, paint);
Enter fullscreen mode Exit fullscreen mode

Styles: The PaintingStyle enum offers .fill and .stroke to either fill shapes or draw their outlines.


4. Advanced Techniques

Clipping: ClipPath and ClipOval widgets allow you to clip your custom-painted widget into desired shapes.

Transformations: Use canvas.translate(), canvas.rotate(), and canvas.scale() to move, rotate, or scale your drawings.

Blend Modes: The BlendMode class lets you define how one layer should blend with layers below it.


5. Performance and Optimization

  1. Repaint Boundaries: Wrap your CustomPaint widget in a RepaintBoundary to isolate its rendering from the rest of the widget tree.
  2. Caching: For static graphics, consider using the PictureRecorder class to cache your painting.
  3. Offscreen Rendering: Use OffscreenCanvas to perform painting operations off the main thread.

6. Debugging and Tools

Flutter's Skia Shader Language (SkSL) Warm-Up: Precompile shaders to prevent jank during the first-time animation.

DevTools: Use Flutter DevTools to inspect rendering layers and identify performance bottlenecks.


Conclusion

Flutter's custom painting is a vast ocean of possibilities. With the right techniques and optimizations, you can craft UI masterpieces that stand out.

As you embark on your custom painting journey, remember: it's not just about drawing shapes, but about creating art.


Before We Go...

Hey, thanks for sticking around! If this post was your jam, imagine what’s coming up next.

I’m launching a YouTube channel, and trust me, you don't want to miss out. Give it a look and maybe even hit that subscribe button?

Tap to subscribe.

Until we meet again, code on and stay curious!

Got any doubt or wanna chat? React out to me on twitter or linkedin.

Top comments (0)