Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
In order to draw custom shapes and lines in Flutter, there are basically four things involved:
- CustomPaint (Itās the exhibitor who gives you a paper to draw on, and then exhibits what you have drawn)Ā š¼ļø
- CustomPainter (Itās you! The painter!) šØāšØ
- Paint (Itās your brush)Ā šØšļø
- Canvas (Itās your paper)Ā ā¬
Yes, itās thatĀ simple!
So letās get started by creating our mainĀ file:
The drawing will happen in the DrawingPage class:
As usual, our page starts with Scaffold which has an appBar and a body which is set to an instance of CustomPaint widget.
The canvas is created and provided to you by the CustomPaint widget which has three important properties:
- painter: This is an instance of the CustomPainter class which draws the first layer of your painting on theĀ canvas.
- child: You can set this to any widget you want. After the painter is done with painting, the child widget is shown on top of the painting.
- foregroundPaint: Finally, this paint is drawn on top of the two previousĀ layers.
As I said, the CustomPaint object creates the canvas and gives it to the painter and foregroundPaint objects so that they can draw onĀ it.
The Size of theĀ Canvas
But what will be the size of the canvas? The same size as the whole of the screen? Half of the screen? orĀ what?
The CustomPaint object creates a canvas the same size as the size of the child parameter. If the child parameter is not provided (yes, thatās optional), the canvas size is determined by the size parameter that you can provide to CustomPaint object when instantiating it.
In our example, the child is a Center widget which is as big as the screen. Therefore, our canvas will be as big as the whole screenĀ too!
If you are wondering why the Center widget is as big as the whole screen, you can read this article I recently wrote about the Center widget: Understanding Center Widget inĀ Flutter
The upper left corner of the canvas is called origin. Itās the point with (0, 0) coordinates. The coordinates of the lower right corner of the canvas are (size.width, size.height):
The Painter
The painter parameter is of type CustomPainter.
This simply means that your painter class (that we are going to create) must extend the CustomPainter class.
You would usually name your CustomPainter class according to what itās going to paint. If itās going to paint a sky, name it SkyPainter. If itās going to draw a face, name it FacePainter. If you are going to draw a gun š« make sure you read thisĀ first:
If a person has to engage in gun drawing, one had better be sure that they do so in the right situation. There are circumstances in which it isnāt legal to draw a firearm. Continue reading at: http://gunbelts.com/blog/when-is-drawing-your-gun-legal/
š
Since I mainly intend to draw a dashed curved line on the canvas, Iād like to name my painter āCurvePainterā:
As you see, my painter has extended the CustomPainter class.
The CustomPainter class has two important functions to override:
- paint: The actual painting happens here. Did you notice the two parameters provided to this function? In this function, you have access to the canvas object which is indeed your paper, and also the size of the canvas on which you are going toĀ draw.
- shouldRepaint: In this function, you should either return true or false. If your painting depends on a variable and that variable changes, you return true here, so that Flutter knows that it has to call the paint method to repaint your painting. Otherwise, return false here to indicate you donāt need aĀ redraw.
Drawing Lines andĀ Shapes
Now everything is ready for you to start drawing. The canvas is ready and we know itsĀ size.
The canvas object provided to you has several helper functions that will help you draw something, to name aĀ few:
drawLine(Offset p1, Offset p2, Paint paint)
Draws a line from point 1 to point 2, with the givenĀ paint.
drawCircle(Offset c, double radius, Paint paint)
Draws a circle centered at the point given that has the radius given by the second argument, with the paint given in the third argument.
drawPath(Path path, Paint paint)
Draws the given path with the givenĀ paint.
moveTo(double x, double y)
Before you start drawing, your pen is by default on the origin point i.e. the top-left corner of the canvas. You can move your pen though, before starting to draw, with this function.
In all the shapes you draw, whether they are filled or stroked (or both) is controlled by Paint.style.
Ok, letās draw something real. I will draw a line, a circle and a path for you, and leave you with exploring the rest of the functions!
In the above example, I have first created the paint (which is like my pen) and have set the color and width of my pen. Then I have used the drawLine method to draw a line from the middle of the left edge to the middle of the right edge of theĀ canvas:
š Note how the āBlade Runnerā text widget (which is thechild parameter of CustomPaint) has been drawn after the painter is done. If you had provided a third foregroundPaint parameter, it would be drawn on top of the childĀ widget.
Now letās draw a blue circle, at the center of theĀ canvas:
As you can see, I used the same paint, but first changed its color to blue, and also set its style to stroke (rather than fill), so that the circle does not get filled. Iāve specified the center point of the canvas by Offset(size.width/2, size.height/2) and decided the radius of the circle to be proportional to the canvas width: size.width/4.
Tip: I could simply set the radius to some number like 10 or whatever, but since the screen could be of any size, itās better to size our objects according to the size of ourĀ canvas.
Drawing aĀ Path
Now letās draw a check mark āļø using the path object, below theĀ circle.
For this to happen, I have first moved my pen to a coordinate below the circle (using moveTo), and then added two lines to the path (using lineTo). You just need to imagine the appropriate X and Y coordinates, or to be more accurate, grab a piece of paper and calculate them patiently. The result of the code aboveĀ is:
Itās possible to close the path by calling path.close():
The result:
Do you want this closed path to be filled with color? No problem! Just change the style of your pen toĀ fill:
The resultĀ is:
The source code of this article is available onĀ GitHub.
In the next article, I will show you āhow to draw a dashed curvedĀ line.
Until then, stay tuned and thanks forĀ reading!
Drawing Custom Shapes and Lines Using Canvas and Path in Flutter was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.