Building a Water Tracking App with Flare & Flutter

Diving into Flare’s Interactive Side

she who codes
Rive
12 min readJul 30, 2019

--

Getting Started

Flare is a powerful animation tool that you can use for both UI and character design in apps and games. By using Flare, you can easily improve the user experience of your mobile application by adding fluid animations that your users can interact with. Flare has runtimes that support an array of languages, and for this, we’ll be using the Flare-Flutter runtimes found here. In this tutorial, I’ll walk you through how to export a Flare file and build it into an app using Flutter. You’ll need to sign up for a 2Dimensions account here and set up your Flutter dev environment using these instructions.

In this tutorial, you’ll learn how to:
1) Mix Animations in Flare
2) Play multiple animations simultaneously
3) Build a Flutter app with interactive animations

The App

The Water Tracker app is a simple way to keep track of how many glasses of water you’ve consumed today. Decide how many eight-ounce glasses of water you want to drink and each time you have one, just click the plus button. When you start a new day, easily reset your stats. Here’s what it’ll look like when we are done! The Flare file can be found here, and it encompasses the entire UI that we’ll be using to create our water tracking mobile app. Let’s get started with exporting the file, and then we’ll dive into some code!

Finished App

Exporting Fun!

Let’s jump right into the Flare here and export our file we’ll be using in Flutter! Flare works best when using Chrome or Firefox and if you have any issues, check out our FAQ here. Looking at the Editor, you’ll notice all of the animations at the bottom left corner that we’ll be using. As you play through each one, you see the animation name, length, and if it loops. These are also all parameters that are available to read in code. Feel free to fork this file and play around with the animations, bones, keyframes, etc.

At the top right, you’ll notice an export button with a few options available. For this tutorial, we’ll be using the “Export” feature and download it as a ‘Binary’ file with the toggle on for ‘Duration from last keyframe’. Save it in a place where you’ll remember, and we’ll drop it in our project directory in a bit!

Setting Up Our Project

First, let’s open Android studio and “Start a new Flutter application”. If you need help setting up your development environment for Flutter, you can find instructions here.

Now let’s set our “Project Name”, I called mine “water_tracker”, but feel free to set this to what you want, keeping in mind the naming convention that Android Studio requires. If you already set up your development environment, the “Flutter SDK path” should be set for you, but you can also navigate to it and set it. For the “Project location”, it is pre-filled in for you, but you may change this too. Finally, we have a short “Description” for our app. Once you decided on that, go ahead and click the next button. Next, we need to “Set the package name” that will be used in publishing our app. We’ll also include support for both iOS and Android since Flutter, and Flare can deploy to both! Hit the “Finish” button, and your app will be created.

The first thing we need to do once our app is created will be to update the “pubspec.yaml” file to include the packages for Flare. To do this, simply find the latest version here and reference it in the dependencies as below. Once you have the version set, make sure to click the “Packages get” button in blue to download the Flare library to the project.

Flare: Imports & Exports

Now that we have our basic project set up with our Flare libraries, we can create a new ‘directory’ called ‘assets’ so that we can import our ‘.flr’ file that we exported from the editor.

In order to utilize this file in code, we’ll first need to update our ‘pubspec.yaml’ to have a new section called ‘assets’ and add the ‘WaterApp.flr’ file to it. That should be it for the ‘pubspec’, now on to the fun stuff!

Diving Into Code

The first thing we want to do is clear out all of the boilerplate code in the ‘main.dart’ file. We’ll make ourselves a little checklist of the things we want to include in the code, so we can track our progress and keep our focus. Let’s also remove the UI overlays that are visible once we run the app in the simulator (or on the device) so that we can have a full-screen view of our app.

Finally, you’ll notice a few errors pointing to the ‘tracking_input.dart’, so let’s fix that now by adding a new Dart file to your project with the name ‘tracking_input’. Majority of the app will live in this file.

Let’s add the necessary code to our new file and import the Flare files we’ll be needing.

You’ll notice there’s an error on line 4, we need to create another class! The imported file ‘flare_controller.dart’ will be the class where we handle all of our animations, so let’s also add that to the project now.

Let’s set up the controller class first since we’ll be using a lot of this code in our ‘tracking_input.dart’.

This new file that will handle the majority of our animations, mixing animations and our fill logic for the water and our cute little ice cube. Let’s drop in our basic boilerplate code required for extending FlareController and add the imports we’ll be using.

We can go ahead and declare all the variables we’ll need in this class, such as the artboard, the animations, and the doubles for animating that will be passed to this class.

We can initialize some of our vars that we declared earlier and set up our ‘advance’ function that advances the animation of the artboard by the elapsed time.

Next, we need to set up our functions that will be called from our ‘tracking_input.dart’ class to manage the animations and status of the ‘water up’ animation.

Now that we have the animations all setup, we can jump back to ‘tracking_input.dart’ and utilize these methods we’ve set up here!

Firstly, our import on line 3 should now be free of any errors, and we can now declare the variables that we’ll use.

We can now add the methods we’ll call from our buttons to change our water intake goals, add or decrease our current water intake, reset our data for the day and toggle our settings tray to open or close.

We can start getting things set up for our animations and initialize our var at the start of the application.

We have our three classes set up now and without errors. But when you run the app, nothing happens! Let’s start building the fun widgets that our app is centered around.

Building Widgets

Finally, we get down to business with building out our widgets and seeing this app and IceBoy come to life! We’ll need a few buttons, a few containers for those buttons to place them properly, a bottom sheet for our tray, a text widget for changing our goal amount and most importantly our Flare widgets. Here’s what our main Button Widgets will look like:

We will have these widgets split. The first three will be the children in our parent widget, and the rest will go in the bottom sheet. When we put it all together, here’s what it looks like:

We should be able to run our app now and see IceBoy in all his cuteness moving about the screen as we change our water intake! If you press the ellipsis at the bottom of the screen, you can see our settings tray animate up, and we can adjust our daily goal or reset our stats. Congratulations, you’ve completed an interactive app using Flare in Flutter!

Where To Now?

Now that you’ve completed this tutorial, you should have a better understanding of how to integrate Flare into your Flutter apps! Adding Flare to our project not only brought the user experience to a new level, but it also gives designers more control over the look and feel of our app with little effort on the code side.

We have much more coming to Flare so stay tuned for the next tutorial in our series! You can view the full source code here. Please join in our conversation in Discord and follow us on social media for updates to Flare. If this tutorial helped you out, give us a clap (or 50!) and leave a comment below with any questions about this tutorial!

--

--

she who codes
Rive

Software Engineer | Gamer | Sharing my experiences of development in Flutter & Unity3d.