text
stringlengths
1
372
paste the following YAML block after the flutter declaration.
this pubspec.yaml file defines the italic style for the
raleway font family as the Raleway-Italic.ttf font file.
when you set style: TextStyle(fontStyle: FontStyle.italic),
flutter swaps Raleway-Regular with Raleway-Italic.
the family value sets the name of the typeface.
you use this name in the fontFamily property of a TextStyle object.
the value of an asset is a relative path from the pubspec.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.
<topic_end>
<topic_start>
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 the style or weight property to adjust how they display.
if you do use those properties on a regular font file,
flutter attempts to simulate the look.
the visual result will look quite different from using the correct font file.
<topic_end>
<topic_start>
set styles and weights with font files
when you declare which font files represent styles or weights of a font,
you can apply the style or weight properties.
<topic_end>
<topic_start>
set font weight
the weight property specifies the weight of the outlines in
the file as an integer multiple of 100, between 100 and 900.
these values correspond to the FontWeight and can be used in the
fontWeight property of a TextStyle object.
in the pubspec.yaml shown in this guide,
you defined RobotoMono-Bold as the 700 weight of the font family.
to use the RobotoMono-Bold font that you added to your app,
set fontWeight to FontWeight.w700 in your TextStyle widget.
if hadn’t added RobotoMono-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 the weight property to override the weight of the font.
you can’t set RobotoMono-Bold to any other weight than 700.
if you set TextStyle(fontFamily: 'robotomono', fontWeight: FontWeight.w900),
the displayed font would still render as however bold RobotoMono-Bold looks.
<topic_end>
<topic_start>
set font style
the style property specifies whether the glyphs in the font file display as
either italic or normal.
these values correspond to the FontStyle.
you can use these styles in the fontStyle property
of a TextStyle object.
in the pubspec.yaml shown in this guide,
you defined Raleway-Italic as being in the italic style.
to use the Raleway-Italic font that you added to your app,
set style: TextStyle(fontStyle: FontStyle.italic).
flutter swaps Raleway-Regular with Raleway-Italic when rendering.
if hadn’t added Raleway-Italic to your app,
flutter attempts to make the font look italic.
the text then might appear to be leaning to the right.
you can’t use the style property to override the glyphs of a font.
if you set TextStyle(fontFamily: 'raleway', fontStyle: FontStyle.normal),
the displayed font would still render as italic.
the regular style of an italic font is italic.
<topic_end>
<topic_start>
set a font as the default
to apply a font to text, you can set the font as the app’s default font
in its theme.
to set a default font, set the fontFamily property in the app’s theme.
match the fontFamily value to the family name declared in the
pubspec.yaml file.
the result would resemble the following code.
<code_start>
return MaterialApp(
title: 'custom fonts',
// set raleway as the default app font.
theme: ThemeData(fontFamily: 'raleway'),
home: const MyHomePage(),
);
<code_end>
to learn more about themes,
check out the using themes to share colors and font styles recipe.
<topic_end>
<topic_start>
set the font in a specific widget
to apply the font to a specific widget like a text widget,
provide a TextStyle to the widget.
for this guide,
try to apply the RobotoMono font to a single text widget.
match the fontFamily value to the family name declared in the
pubspec.yaml file.
the result would resemble the following code.
<code_start>
child: text(
'roboto mono sample',
style: TextStyle(fontFamily: 'robotomono'),