text stringlengths 1 474 |
|---|
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'), |
),<code_end> |
error Important |
If a TextStyle 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.<topic_end> |
<topic_start> |
Try the complete example |
<topic_end> |
<topic_start> |
Download fonts |
Download the Raleway and RobotoMono font files from Google Fonts.<topic_end> |
<topic_start> |
Update the pubspec.yaml file |
Open the pubspec.yaml file at the root of your Flutter project.Replace its contents with the following YAML.<topic_end> |
<topic_start> |
Use this main.dart file |
Open the main.dart file in the lib/ directory of your Flutter project.Replace its contents with the following Dart code. |
<code_start>import 'package:flutter/material.dart'; |
void main() => runApp(const MyApp()); |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'Custom Fonts', |
// Set Raleway as the default app font. |
theme: ThemeData(fontFamily: 'Raleway'), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.