Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theFlutter 3.41 blog post!

Use a custom font

How to use custom fonts.

What you'll learn
  • How to choose a font.
  • How to import font files.
  • How to set a font as a default.
  • How to use a font in a given widget.

Although Android and iOS offer high quality system fonts, designers want support for custom fonts. You might have a custom-built font from a designer, or perhaps you downloaded a font fromGoogle Fonts.

A typeface is the collection of glyphs or shapes that comprise a given style of lettering. A font is one representation of that typeface at a given weight or variation. Roboto is a typeface and Roboto Bold is a font.

Flutter lets you apply a custom font across an entire app or to individual widgets. This recipe creates an app that uses custom fonts with the following steps.

  1. Choose your fonts.
  2. Import the font files.
  3. Declare the font in the pubspec.
  4. Set a font as the default.
  5. Use a font in a specific widget.

You don't need to follow each step as you go. The guide offers completed example files at the end.

Note

This guide makes the following presumptions:

  1. You'veset up your Flutter environment.
  2. You'vecreated a new Flutter app namedcustom_fonts. If you haven't completed these steps yet, do so before continuing with this guide.
  3. You're performing the provided commands in a macOS or Linux shell and usingvi. You can substitute any text editor forvi. Windows users should use the appropriate commands and paths when performing the steps.
  4. You're adding the Raleway and RobotoMono fonts to your Flutter app.

Choose a font

#

Your choice of font should be more than a preference. Consider which file formats work with Flutter and how the font could affect design options and app performance.

Pick a supported font format

#

Flutter supports the following font formats:

  • OpenType font collections:.ttc
  • TrueType fonts:.ttf
  • OpenType fonts:.otf

Flutter does not support fonts in the Web Open Font Format,.woff and.woff2, on desktop platforms.

Choose fonts for their specific benefits

#

Few sources agree on what a font file type is or which uses less space. The key difference between font file types involves how the format encodes the glyphs in the file. Most TrueType and OpenType font files have similar capabilities as they borrowed from each other as the formats and fonts improved over time.

Which font you should use depends on the following considerations.

  • How much variation you need for fonts in your app?
  • How much file size you can accept fonts using in your app?
  • How many languages you need to support in your app?

Research what options a given font offers, like more than one weight or style per font file,variable font capability, the availability of multiple font files for a multiple font weights, or more than one width per font.

Choose the typeface or font family that meets the design needs of your app.

Import the font files

#

To work with a font, import its font files into your Flutter project.

To import font files, perform the following steps.

  1. If necessary, to match the remaining steps in this guide, change the name of your Flutter app tocustom_fonts.

    mv /path/to/my_app /path/to/custom_fonts
  2. Navigate to the root of your Flutter project.

    cd /path/to/custom_fonts
  3. Create afonts directory at the root of your Flutter project.

    mkdir fonts
  4. Move or copy the font files in afonts orassets folder at the root of your Flutter project.

    cp ~/Downloads/*.ttf ./fonts

The resulting folder structure should resemble the following:

custom_fonts/|- fonts/  |- Raleway-Regular.ttf  |- Raleway-Italic.ttf  |- RobotoMono-Regular.ttf  |- RobotoMono-Bold.ttf

Declare the font in the pubspec.yaml file

#

After you've downloaded a font, include a font definition in thepubspec.yaml file. This font definition also specifies which font file should be used to render a given weight or style in your app.

Define fonts in thepubspec.yaml file

#

To add font files to your Flutter app, complete the following steps.

  1. Open thepubspec.yaml file at the root of your Flutter project.

    vi pubspec.yaml
  2. Paste the following YAML block after theflutter declaration.

    yaml
    fonts:-family:Ralewayfonts:-asset:fonts/Raleway-Regular.ttf-asset:fonts/Raleway-Italic.ttfstyle:italic-family:RobotoMonofonts:-asset:fonts/RobotoMono-Regular.ttf-asset:fonts/RobotoMono-Bold.ttfweight:700

Thispubspec.yaml file defines the italic style for theRaleway font family as theRaleway-Italic.ttf font file. When you setstyle: TextStyle(fontStyle: FontStyle.italic), Flutter swapsRaleway-Regular withRaleway-Italic.

Thefamily value sets the name of the typeface. You use this name in thefontFamily property of aTextStyle object.

The value of anasset is a relative path from thepubspec.yaml file to the font file. These files contain the outlines for the glyphs in the font. When building the app, Flutter includes these files in the app's asset bundle.

Include font files for each font

#

Different typefaces implement font files in different ways. If you need a typeface with a variety of font weights and styles, choose and import font files that represent that variety.

When you import a font file that doesn't include either multiple fonts within it or variable font capabilities, don't use thestyle orweight property to adjust how they display. If you do use those properties on a regular font file, Flutter attempts tosimulate the look. The visual result will look quite different from using the correct font file.

Set styles and weights with font files

#

When you declare which font files represent styles or weights of a font, you can apply thestyle orweight properties.

Set font weight

#

Theweight property specifies the weight of the outlines in the file as an integer multiple of 100, between 100 and 900. These values correspond to theFontWeight and can be used in thefontWeight property of aTextStyle object.

In thepubspec.yaml shown in this guide, you definedRobotoMono-Bold as the700 weight of the font family. To use theRobotoMono-Bold font that you added to your app, setfontWeight toFontWeight.w700 in yourTextStyle widget.

If you hadn't addedRobotoMono-Bold to your app, Flutter attempts to make the font look bold. The text then might appear to be somewhat darker.

You can't use theweight property to override the weight of the font. You can't setRobotoMono-Bold to any other weight than700. If you setTextStyle(fontFamily: 'RobotoMono', fontWeight: FontWeight.w900), the displayed font would still render as however boldRobotoMono-Bold looks.

Set font style

#

Thestyle property specifies whether the glyphs in the font file display as eitheritalic ornormal. These values correspond to theFontStyle. You can use these styles in thefontStyle property of aTextStyle object.

In thepubspec.yaml shown in this guide, you definedRaleway-Italic as being in theitalic style. To use theRaleway-Italic font that you added to your app, setstyle: TextStyle(fontStyle: FontStyle.italic). Flutter swapsRaleway-Regular withRaleway-Italic when rendering.

If hadn't addedRaleway-Italic to your app, Flutter attempts to make the fontlook italic. The text then might appear to be leaning to the right.

You can't use thestyle property to override the glyphs of a font. If you setTextStyle(fontFamily: 'Raleway', fontStyle: FontStyle.normal), the displayed font would still render as italic. Theregular style of an italic fontis italic.

Set a font as the default

#

To apply a font to text, you can set the font as the app's default font in itstheme.

To set a default font, set thefontFamily property in the app'stheme. Match thefontFamily value to thefamily name declared in thepubspec.yaml file.

The result would resemble the following code.

dart
returnMaterialApp(title:'Custom Fonts',// Set Raleway as the default app font.theme:ThemeData(fontFamily:'Raleway'),home:constMyHomePage(),);

To learn more about themes, check out theUsing Themes to share colors and font styles recipe.

Set the font in a specific widget

#

To apply the font to a specific widget like aText widget, provide aTextStyle to the widget.

For this guide, try to apply theRobotoMono font to a singleText widget. Match thefontFamily value to thefamily name declared in thepubspec.yaml file.

The result would resemble the following code.

dart
child:Text('Roboto Mono sample',style:TextStyle(fontFamily:'RobotoMono'),),
Important

If aTextStyle object specifies a weight or style without a corresponding font file, the engine uses a generic file for the font and attempts to extrapolate outlines for the requested weight and style.

Avoid relying on this capability. Import the proper font file instead.

Try the complete example

#

Download fonts

#

Download the Raleway and RobotoMono font files fromGoogle Fonts.

Update thepubspec.yaml file

#
  1. Open thepubspec.yaml file at the root of your Flutter project.

    vi pubspec.yaml
  2. Replace its contents with the following YAML.

    yaml
    name:custom_fontsdescription:An example of how to use custom fonts with Flutterdependencies:flutter:sdk:flutterdev_dependencies:flutter_test:sdk:flutterflutter:fonts:-family:Ralewayfonts:-asset:fonts/Raleway-Regular.ttf-asset:fonts/Raleway-Italic.ttfstyle:italic-family:RobotoMonofonts:-asset:fonts/RobotoMono-Regular.ttf-asset:fonts/RobotoMono-Bold.ttfweight:700uses-material-design:true

Use thismain.dart file

#
  1. Open themain.dart file in thelib/ directory of your Flutter project.

    vi lib/main.dart
  2. Replace its contents with the following Dart code.

    dart
    import'package:flutter/material.dart';voidmain()=>runApp(constMyApp());classMyAppextendsStatelessWidget{constMyApp({super.key});@overrideWidgetbuild(BuildContextcontext){returnMaterialApp(title:'Custom Fonts',// Set Raleway as the default app font.theme:ThemeData(fontFamily:'Raleway'),home:constMyHomePage(),);}}classMyHomePageextendsStatelessWidget{constMyHomePage({super.key});@overrideWidgetbuild(BuildContextcontext){returnScaffold(// The AppBar uses the app-default Raleway font.appBar:AppBar(title:constText('Custom Fonts')),body:constCenter(// This Text widget uses the RobotoMono font.child:Text('Roboto Mono sample',style:TextStyle(fontFamily:'RobotoMono'),),),);}}

The resulting Flutter app should display the following screen.

Custom Fonts Demo

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-10-30.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp