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 add tokens directly to your existing Flutter app.
At the end of this guide, you will have added your Figma design tokens to your Flutter app, completely automatically.
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 add the design tokens.
First, install the Figmage CLI by running:
dart pub global activate figmage
Create a figmage.yaml
configuration file in your Flutter app’s root directory.
touch figmage.yaml
Configure figmage.yaml
with the following essential settings:
fileId: "YOUR_FIGMA_FILE_ID"asPackage: falsetokenPath: "design" # or any other path under lib
The key settings for app integration are:
asPackage: false
- This tells Figmage not to generate a standalone packagetokenPath: "design"
- Specifies where to create token files within your app’s lib
directoryCustomize which tokens to generate:
# Additional configuration optionscolors: generate: truetypography: generate: true useGoogleFonts: truenumbers: generate: falsespacers: generate: falsepaddings: generate: false
Optionally filter tokens by path:
# Generate only specific token paths if neededcolors: generate: true from: - "semantic/app/"typography: generate: true from: - "semantic/typography"
From your Flutter app’s root directory, run:
figmage forge -t YOUR_FIGMA_PERSONAL_ACCESS_TOKEN
If you don’t have your file ID or personal access token yet, see the Obtaining Figma Credentials guide.
Figmage will fetch your Tokens from Figma and generate the Dart code directly within your app. 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 files at ... (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.
Your app’s directory structure should now include the generated token files:
The generated token files will follow the same structure as in the package approach. You will notice that Figmage generated a class for each of your top-level Token collections.
Since tokens are now part of your app, you’ll need to ensure you have the necessary dependencies:
Open your app’s pubspec.yaml
file
Add required dependencies:
dependencies: flutter: sdk: flutter google_fonts: ^6.1.0 # Only if useGoogleFonts: true
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 'design/colors.dart';import 'design/typography.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 'design/colors.dart';import 'design/typography.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 design tokens change in Figma, simply rerun the Figmage command:
figmage forge -t YOUR_FIGMA_PERSONAL_ACCESS_TOKEN
This will update all token files in your app with the latest changes from Figma.