Last month I participated to the Flutter clock challenge with a coworker. The goal was to build a clock with Flutter for an iot device. Our biggest challenge was to create an animation between digits. We wanted each digit to transform their shapes.
This type of shape animation is called path morphing.
In this tutorial, I will show you how to animate an image from one shape to another in your flutter app. At the end of this article, you will learn how to do this:
First if you're new to flutter, you can learn how to get started here.
You can find the source code for this app on this repo.
We found out that there are many ways to do path morphing with flutter.
Again it's a very powerful tool, but the drawback is that you have to edit the vertices manually. It means moving all the points of your path with your mouse like this:
It's an imprecise and repetitive process.
We are looking for a way to create a path morphing animation between any SVG images.
We finally found a flutter package which allows that: Path_moprh.
To understand how we created the animation, I need to talk about a few tools that we're going to use.
SVG
We first have to learn what an SVG image is.
SVG stands for Scalable vector graphics. It's used to define vector-based graphics for the Web. SVG defines the graphics in XML format. Every element and every attribute in SVG files can be animated.
SVG can be defined with many objects like rectangle, circle or path.
We need to use the path, it's defined with the following commands:
M = moveto
L = lineto
H = horizontal lineto
V = vertical lineto
C = curveto
S = smooth curveto
Q = quadratic Bézier curve
T = smooth quadratic Bézier curveto
A = elliptical Arc
Z = closepath
Figma
Figma is my favorite software to create an interface. We'll use it to export our SVG image. Let's create an animation between two digits.
To export an SVG, select a text and at the bottom right click on export.
We need two shapes to create the animation so let export a "1" and a "2".
Path_morph
Path_morph is a package that lets you smoothly morph one Flutter Path object into another.
SampledPathData data = PathMorph.samplePaths(path1, path2);
With this SampledPathData we can create an animation using the function:
PathMorph.generateAnimations();
But an SVG is not a Flutter path object. To use path_morph we need to create a path object from the SVG we exported.
Path_drawing
Path_drawing is flutter package that allows us to parse SVG data to create a flutter path object.
Path parseSvgPathData(String svg);
The function takes the "path" part of an SVG. Open your SVG file with a text editor.
We have our image, but we can't change his size with width and height. We need to transform the path with a Matrix4.
Without digging too much in the maths, if we transform our path with the identity matrix, it will stay the same. But with flutter, we can scale our identity matrix.
An Animation Controller is a class that allows you to Play an animation forward, in reverse, or stop the animation. It produces values that range from 0.0 to 1.0, during a given duration. These values are generated whenever the device's screen is ready to display a new frame.
Animation Controller needs a TickerProvider. We can extend our class with a SingleTickerProviderStateMixin.
Now in our widget tree, we need to specify the size of our painter, using the SizedBox widget. It allows us to define the height and width of our painter.
To allow other widgets to constrain its dimension, we just need to wrap our SizedBox with a FittedBow. Like this:
To finish this tutorial we just need to control our animation. In the widget tree, create a Button, check the controller.status and do a reverse() or a forward().
onPressed: () { if (controller.status == AnimationStatus.completed) { controller.reverse(); } if (controller.status == AnimationStatus.dismissed) { controller.forward(); } },
Conclusion
Congratulation now you know how to do path morphing animation with flutter! Now you can experiment with this library and create the animation you want. In the repo, you will find all the resources to do this animation:
Participating to the flutter clock challenge was the perfect opportunity to learn flutter and to discover all the possibilities it offers. As a mobile developer, I'm excited to start new projects with this technology.
Thank you Antoine Françon for participating in this challenge with me !