Custom Paint in Flutter

Updated 27 April 2023

Save

Custom Paint in Flutter is used to paint or customize the shape, and size of the widgets with the help of Custom Painter Class.

In this blog, we will be creating some shapes with the help of Custom Paint.

You may also check our Flutter app development company.

Let’s begin with the basic understanding of CustomPainter Class.

Custom Painter class overrides two methods-

  1. paint()
  2. shouldRepaint()

Within the paint() method, we need to provide all the details regarding painting the canvas.

Moreover, shouldRepaint() method, we can decide whether we want the Canvas to be redrawn or not when a new Custom Painter instance is provided.

Properties of Custom Paint

In CustomPaint class, there are three most important properties-

  1. child – It takes the widget over which the CustomPaint needs to be applied.
  2. painter – It paints before the widget has been built. It takes a class within it which extends CustomPainter.
  3. foregroundPainter- It paints after the widget has been built. It takes a class within it which extends CustomPainter.

Moreover, there are many other properties like size, isComplex etc. 

Code for Custom Paint

Firstly, we will need to create a class that extends CustomPainter class.

Secondly, we have created two objects, the first one is the Path and the second one is the Circle.

Here, for creating a path we are using Path() and its quadraticBezierTo property.

Finally, we have created a CustomPainter class to paint over the canvas.

Complete Code

Finally, here is the complete code for the Custom Paint –

Output

Here’s the final output-

Conclusion

In this blog, we have discussed how can we use Custom Paint in Flutter.

I hope it will help you out in understanding.

You can also check out our blog on Custom Clipper in Flutter – https://mobikul.com/customclipper-in-flutter/

Thanks for reading!!

References

https://medium.com/flutter-community/a-deep-dive-into-custompaint-in-flutter-47ab44e3f216 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home