Figma Credentials
You’ll need your Figma file ID and a personal access token. See the Obtaining Figma Credentials guide for instructions.
This guide will teach you how to get Figmage running to generate a standalone design package.
At the end of this guide, you will have a finished Flutter package that contains all your Tokens as Dart code.
Before you start, we assume that you have a few things ready.
Figma Credentials
You’ll need your Figma file ID and a personal access token. See the Obtaining Figma Credentials guide for instructions.
Design Tokens
You’ll need at least some design tokens in your Figma file. See the Design Tokens reference for instructions.
Flutter App
An existing Flutter app where you want to use the design tokens package.
First, install the Figmage CLI by running:
dart pub global activate figmage
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:
mkdir design_tokens
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.
touch design_tokens/figmage.yaml
Your package directory should now look like this:
Open the figmage.yaml
file you created earlier and add your Figma file ID:
fileId: "YOUR_FIGMA_FILE_ID"
If you don’t have your file ID yet, see the Obtaining Figma Credentials guide.
Optionally, configure additional settings as needed:
# Additional configuration optionscolors: generate: truetypography: generate: true useGoogleFonts: truenumbers: generate: truespacers: generate: true
For more detailed configuration, see the Configuration Options reference.
From your package directory, run:
figmage forge -t YOUR_FIGMA_PERSONAL_ACCESS_TOKEN
If you don’t have your personal access token yet, see the Obtaining Figma Credentials guide.
Figmage will 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:
✗ Failed to fetch variables: Unauthorized. Make sure you have a valid access token that can access the file and that you are a Figma Enterprise team member. (0.7s)✓ Found ... styles (3.0s)✓ Generated package at ... with ... files (0.1s)...
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:
semantic├── surface│ ├── primary│ └── secondary└── onSurface ├── primary └── secondary
You will see a Dart class like this in your colors.dart
file:
// ...class ColorsSemantic extends ThemeExtension<ColorsSemantic> { const ColorsSemantic({ required this.surfacePrimary, required this.surfaceSecondary, required this.onSurfacePrimary, required this.onSurfaceSecondary, });
// ...}
Now 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:
dependencies: design_tokens: path: relative/path/to/your/design_tokens/
If you decided to give your package a different name, make sure to replace design_tokens
with the name you chose.
Run flutter pub get:
flutter pub get
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.
import 'package:design_tokens/design_tokens.dart';// ...@overrideWidget build(BuildContext context) { return MaterialApp( theme: ThemeData( // ... extensions: [ ColorsSemantic.standard(), // or .light() if you have modes TypographySemantic.standard(), ], ), // Add dark theme if you have it darkTheme: ThemeData( // ... extensions: [ ColorsSemantic.dark(), // Only if you have a dark mode TypographySemantic.standard(), ], ), themeMode: ThemeMode.system, home: MyHomePage(), );}
Use your Tokens in your widgets:
import 'package:design_tokens/design_tokens.dart';import 'package:flutter/material.dart';
class ExampleButton extends StatelessWidget { @override Widget build(BuildContext context) { return ElevatedButton( style: ElevatedButton.styleFrom( backgroundColor: context.colorsSemantic.surfacePrimary, foregroundColor: context.colorsSemantic.onSurfacePrimary, textStyle: context.typographySemantic.headline1, ), onPressed: () {}, child: Text('Click me!'), ); }}
Whenever your Tokens change in Figma, you can run Figmage again in the same package directory to automatically update your package with the latest changes:
figmage forge -t YOUR_FIGMA_PERSONAL_ACCESS_TOKEN