Figma Design File
You should have a Figma file that represents your Design System, or just a simple app design. In both cases, you should have at least a few Tokens defined in your file.
This guide will teach you how to get Figmage running and set you up for success.
At the end of this guide, you will have a finished Flutter package that contains all your Tokens as Dart code. Exciting, right?
Before you start, we assume that you have a few things ready.
Figma Design File
You should have a Figma file that represents your Design System, or just a simple app design. In both cases, you should have at least a few Tokens defined in your file.
Figma Account
In most cases, you will only need an account that can access the file you want to sync with your project. However, if you are using Variables in Figma, you might need a paid plan. Find out more here.
First, you need to install the Figmage CLI. You can do this by running the following command in your terminal:
Next, decide where you want the generated package to live. You can create the package anywhere on your machine, use a monorepo, check it into your version control system separately, or even publish it to pub.dev.
Decide on a name and location for your package. Make sure to follow the Dart package naming conventions when choosing a name.
Create a directory with that name for your package to live in.
Letβs say you want to call your package design_tokens
, you would run the following command in your terminal:
Create a figmage.yaml
file in the root of your package directory.
While this is not strictly necessary, it is recommended to have a figmage.yaml
file in the root of your package directory.
This file will contain the configuration for Figmage.
Your package directory should now look like this:
Now that you have your package set up, you need to tell Figmage where to find your Tokens in Figma.
Get your Figma fileβs ID. You can find the ID of your Figma file in the URL when you have the file open in your browser. It looks something like this:
An easy way to obtain the link from the Figma app is to click the Share button in the top right corner of the app and select the Copy link option.
Add the Figma file ID to your figmage.yaml
file.
Open the file you created earlier and add the following content:
Obtain a Personal Access Token from Figma.
You can generate a Personal Access Token in the Figma app by going to your account settings and selecting the Generate new token option. Make sure to copy the token to a safe place, as you will not be able to see it again.
Now that you have everything set up, you can run Figmage to generate your Tokens package.
Navigate to your package directory in your terminal.
Run Figmage with the following command:
Replace <<your-personal-access-token>>
with the Personal Access Token you obtained earlier.
Let Figmage do its magic.
Figmage will now fetch your Tokens from Figma and generate the Dart code for your package. Pay attention to the output in your terminal. Unless you are running Figmage with an Enterprise Figma account, you will see an error message:
This is expected and does not affect the generation of your Style-based Tokens. Take a look at the Figma Account Requirements for more information.
If everything went well, you should now have a Dart package in your package directory that contains all your Tokens as Dart code.
Your package should look something like this:
Go ahead and open one of the files in the src
directory to see your Tokens as Dart code.
You will notice that Figmage generated a class for each of your top-level Token collections.
So if your Tokens were structured like this in Figma:
You will see a Dart class like this in your colors.dart
file:
Now that you have your Tokens as Dart code, you can use them in your Flutter app.
Add your Tokens package as a dependency to your Flutter app.
Open the pubspec.yaml
file of your Flutter app and add the following line to the dependencies
section:
If you decided to give your package a different name, make sure to replace design_tokens
with the name you chose.
Add the generated Tokens to your appβs Theme
Figmage generates ThemeExtension
classes, which you will need to add to your appβs ThemeData
to use them.
For example, this is how you would add the generated ColorsSemantic
and TypographySemantic
to your appβs ThemeData
:
If your Tokens were generated from Variables, you will get multiple contructors, one for each mode.
Letβs say you have a dark
and a light
mode, you would use them like this:
Use your Tokens in your app
You are ready to use your tokens wherever you want!
Thatβs it! You now have your Tokens as Dart code in your Flutter app. π
Whenever your Tokens change in Figma, you can run Figmage again in the same directory to automatically update your package with the latest changes.