Skip to content
tezvyn:

CustomPaint: Drawing Your Own Widgets in Flutter

Source: api.flutter.devMediumHow cards are made

CustomPaint is your blank canvas widget; CustomPainter is the artist that draws on it. Use them for custom charts or unique UI. The footgun: shared canvases can cause blend modes to erase other widgets, so use saveLayer sparingly.

Why it exists

Flutter's widget library is extensive, but sometimes you need a visual element that doesn't exist. Instead of composing many widgets, CustomPaint provides a low-level, high-performance escape hatch to draw exactly what you need, from simple shapes to complex data visualizations.

The mental model

Think of CustomPaint as a widget that carves out a rectangular space on the screen and gives you a blank Canvas. CustomPainter is a separate class where you define the drawing logic. You create your painter, pass it to the CustomPaint widget, and Flutter handles calling your painter's paint method when it's time to draw. The widget is the frame; the painter is the artwork.

How it works

You subclass CustomPainter and must implement two key methods. First, paint(Canvas canvas, Size size), which is where all drawing logic lives. You use the provided canvas object to call drawing commands like drawRect, drawCircle, or drawPath. Second, shouldRepaint(CustomPainter oldDelegate), a crucial performance method. It should return true only if the new painter has different properties than the old one, warranting a redraw. For animations, pass a Listenable to your painter's constructor to trigger repaints efficiently without rebuilding the widget tree.

When to use it

Use CustomPaint for performance-sensitive custom graphics. It's perfect for static decorations, dynamic data visualizations like bar charts or pie charts, or interactive elements like a color wheel where you need pixel-level control over the appearance. It's your tool when composing existing widgets is too slow or too complex.

When not to use it

Don't use CustomPaint for tasks that can be achieved by composing existing widgets. It's more complex and less declarative than the standard widget approach. Avoid it for simple layouts or standard UI elements like buttons and text fields; use Container, Row, and Column for those.

One canonical example

A common use is creating a background effect. To draw a sky with a sun, you'd create a Sky class extending CustomPainter. In its paint method, you'd define a RadialGradient for the sun and draw a Rect filled with that gradient onto the canvas. Since the sky is static, its shouldRepaint method would simply return false to prevent unnecessary redraws.

Interview question

For which task would CustomPaint be the most suitable Flutter tool?

  • a.Applying a custom border and background color to a standard button widget.
  • b.Arranging a complex hierarchy of text, images, and input fields.
  • c.Drawing a unique, animated waveform visualization that requires pixel-level control.Correct
  • d.Displaying a list of items that can be scrolled and reordered by the user.
Why?

CustomPaint is ideal for performance-sensitive custom graphics and pixel-level control, like complex data visualizations or unique graphical effects. Simpler widgets like Container or DecoratedBox are typically sufficient for basic custom styling of standard widgets, making option A a less suitable use case for CustomPaint.

Just read this? Test yourself on what you have been reading.

Read the original → api.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles