Playing with Paths in Flutter
A deep dive into paths in Flutter
Everything is a widget in Flutter. And there are so many awesome widgets available in Flutter but one of my favourite widget is CustomPaint.
CustomPaint is a widget that provides a canvas on which to draw during the paint phase.
There are different ways to draw on a canvas, one of the most efficient ways is using Path. In this blog, we are going to learn to draw and animate some advance paths. If you are unfamiliar with Paths
please check this excellent post on paths in Flutter by Muhammed Salih Guler:
Drawing a line
Drawing a line is probably the easiest thing to do with paths. First, move the current point of the path to the starting point using the moveTo
function. Then draw the line using the lineTo
function to the endpoint. That’s it.
Drawing a dashed line
Drawing a line is easy but the tricky part comes when you wish to draw the dashed line in Flutter. There isn’t any simple function to draw the dashed line but we can achieve this using the PathMetric.
PathMetric is a utility for measuring a Path and extracting sub-paths.
First, we will draw a line as shown above. Then we will get the PathMetrics
using path.computeMetrics()
function. For each PathMetric we will extract the path where the starting point is the distance
and length equal to dashWidth
.
dashPath.addPath(
pathMetric.extractPath(distance, distance + dashWidth),
Offset.zero,
);
Circles
In this section, we are going to draw circles… circles… and more circles. We will be building something like this.
A circle is a type of ellipse. We can make an ellipse using addOval
function. It takes Rect as a param. To make that oval as a circle we can use Rect.fromCircle
.
@override
void paint(Canvas canvas, Size size) {
var path = Path();
path.addOval(Rect.fromCircle(
center: Offset(0, 0),
radius: 80.0,
));
canvas.drawPath(path, myPaint);
}
This will give us a single circle with the centre as (0,0) and radius 80.
Ok, that was pretty easy but now the question is how to make something like this.
We can achieve this with the help of basic Trigonometry but before that let’s see what pattern can we find in the above image. One pattern which we can find is that if we draw a circle of the same radius from the centre (0,0) then this circle contains the centres of all the circles(shown in blue dots).
We need to find the coordinates of these centres. Suppose there are n circles. Let’s take any circle and mark its centre as (x,y). The angle between x-axis and line (0,0) — (x,y) is θ. Our aim is to find the x and y coordinates.
We know that the total angle is 2π. Which means that the angle(θ) created by the iᵗʰ centre with x-axis will be:
To find the value of x and y the trigonometry from our high school come in handy. From the above figure, we can calculate the value of sin(θ) and cos(θ).
And we this gives us the value of x and y.
That’s it. We will apply the same formula in our code and draw the n number of circles of radius r on (x,y) coordinates.
Path Tracing
The path tracing animation can be implemented by drawing the small extracted path of the whole path using PathMetric. It’s hard to do it with path
as it won’t give us the length as well as the extracted path.
Let’s first define an AnimationController
.
class _CirclesState extends State<Circles> with SingleTickerProviderStateMixin {AnimationController _controller;@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 3),
);
_controller.value = 1.0;
}
Now, we will pass the controller value to our CirclesPainter
class.
CustomPaint(
painter: CirclesPainter(
circles: circles,
progress: _controller.value,
showDots: showDots,
showPath: showPath,
),
),
To animate the path we are going to draw the path fraction by fraction. PathMetric has an extractPath
function which will return the segment of a path for the given length. The extractPath
function takes the start
and end
value. The end
value is equal to the pathMetric.length * progress
. Which is doing nothing but drawing the path in the time interval of 3 seconds.
Full code for circles is available here.
Polygon
Another important figure in geometry is a polygon. A polygon is a closed figure where the sides are all line segments. We will be building something like this.
The logic for building the polygon is the same as of the circle we have seen above but instead of drawing a circle on (x,y), we will be drawing a line to (x,y) coordinates.
Full code for the polygon is available here.
Spiral
A spiral is a curve which emanates from a point, moving farther away as it revolves around the point. Drawing a spiral is a little tricky part as there isn't an easy way to do it.
To achieve this end result we will be drawing the line to (x,y) coordinates. For every new point(x,y) we will increase the radius by 0.75 and angle by 2π/50. Because the radius and the angle is quite small the result we get looks like a curve rather than a straight line.
We can animate it the same way we animated other views using the PathMetric. Full code for the spiral is available here.
Bonus
Here is an animation of revolving planets. Check out the code here.
You can see the full source code of the project here.