What Is the Purpose of Listing Multiple Font Families in a Style?
Use a custom font
- 1. Import the font files
- Supported font formats
- 2. Declare the font in the pubspec
- pubspec.yaml option definitions
- 3. Set a font as the default
- 4. Employ the font in a specific widget
- TextStyle
- Complete case
- Fonts
- pubspec.yaml
- master.dart
Although Android and iOS offering high quality system fonts, 1 of the near common requests from designers is for custom fonts. For case, you might take a custom-built font from a designer, or perhaps you downloaded a font from Google Fonts.
Flutter works with custom fonts and you can apply a custom font across an unabridged app or to individual widgets. This recipe creates an app that uses custom fonts with the following steps:
- Import the font files.
- Declare the font in the pubspec.
- Set a font as the default.
- Apply a font in a specific widget.
1. Import the font files
To work with a font, import the font files into the projection. It's mutual practice to put font files in a fonts
or assets
folder at the root of a Flutter project.
For example, to import the Raleway and Roboto Mono font files into a project, the binder structure might expect like this:
awesome_app/ fonts/ Raleway-Regular.ttf Raleway-Italic.ttf RobotoMono-Regular.ttf RobotoMono-Bold.ttf
Supported font formats
Palpitate supports the following font formats:
-
.ttf
-
.otf
Flutter does not support .woff
and .woff2
fonts for all platforms.
2. Declare the font in the pubspec
Once you've identified a font, tell Palpitate where to discover it. You can exercise this by including a font definition in the pubspec.yaml
file.
flutter : fonts : - family : Raleway fonts : - asset : fonts/Raleway-Regular.ttf - asset : fonts/Raleway-Italic.ttf fashion : italic - family : RobotoMono fonts : - asset : fonts/RobotoMono-Regular.ttf - asset : fonts/RobotoMono-Assuming.ttf weight : 700
pubspec.yaml
choice definitions
The family
determines the name of the font, which you employ in the fontFamily
property of a TextStyle
object.
The nugget
is a path to the font file, relative to the pubspec.yaml
file. These files contain the outlines for the glyphs in the font. When building the app, these files are included in the app'southward nugget bundle.
A single font can reference many unlike files with different outline weights and styles:
-
The
weight
property specifies the weight of the outlines in the file as an integer multiple of 100, betwixt 100 and 900. These values stand for to theFontWeight
and can be used in thefontWeight
belongings of aTextStyle
object. For example, if you want to utilise theRobotoMono-Bold
font divers higher up, you would setfontWeight
toFontWeight.w700
in yourTextStyle
.Note that defining the
weight
property does non override the bodily weight of the font. You lot would non be able to accessRobotoMono-Assuming
withFontWeight.w100
, even if itsweight
was set to 100. -
The
style
holding specifies whether the outlines in the file areitalic
ornormal
. These values represent to theFontStyle
and can be used in thefontStyle
property of aTextStyle
object. For case, if you want to use theRaleway-Italic
font defined above, you would set upfontStyle
toFontStyle.italic
in yourTextStyle
.Annotation that defining the
way
property does non override the actual style of the font; You would not be able to accessRaleway-Italic
withFontStyle.normal
, even if itsstyle
was fix to normal.
3. Set a font as the default
You have 2 options for how to apply fonts to text: as the default font or but within specific widgets.
To use a font every bit the default, set the fontFamily
property as part of the app's theme
. The value provided to fontFamily
must match the family
proper name declared in the pubspec.yaml
.
return MaterialApp( title: 'Custom Fonts', // Set Raleway as the default app font. theme: ThemeData(fontFamily: 'Raleway'), home: const MyHomePage(), );
For more information on themes, see the Using Themes to share colors and font styles recipe.
If you lot want to apply the font to a specific widget, such every bit a Text
widget, provide a TextStyle
to the widget.
In this instance, apply the RobotoMono font to a unmarried Text
widget. Once again, the fontFamily
must friction match the family
proper name declared in the pubspec.yaml
.
child: Text( 'Roboto Mono sample', style: TextStyle(fontFamily: 'RobotoMono'), ),
TextStyle
If a TextStyle
object specifies a weight or style for which there is no exact font file, the engine uses i of the more than generic files for the font and attempts to extrapolate outlines for the requested weight and style.
Complete example
Fonts
The Raleway and RobotoMono fonts were downloaded from Google Fonts.
pubspec.yaml
name : custom_fonts description : An case of how to employ custom fonts with Palpitate dependencies : flutter : sdk : flutter dev_dependencies : flutter_test : sdk : flutter flutter : fonts : - family : Raleway fonts : - asset : fonts/Raleway-Regular.ttf - nugget : fonts/Raleway-Italic.ttf style : italic - family : RobotoMono fonts : - nugget : fonts/RobotoMono-Regular.ttf - asset : fonts/RobotoMono-Assuming.ttf weight : 700 uses-textile-design : true
main.dart
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({Primal? key}) : super(primal: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Custom Fonts', // Set up Raleway equally the default app font. theme: ThemeData(fontFamily: 'Raleway'), abode: const MyHomePage(), ); } } class MyHomePage extends StatelessWidget { const MyHomePage({Key? cardinal}) : super(key: central); @override Widget build(BuildContext context) { render Scaffold( // The AppBar uses the app-default Raleway font. appBar: AppBar(title: const Text('Custom Fonts')), body: const Centre( // This Text widget uses the RobotoMono font. kid: Text( 'Roboto Mono sample', style: TextStyle(fontFamily: 'RobotoMono'), ), ), ); } }
Source: https://docs.flutter.dev/cookbook/design/fonts
Post a Comment for "What Is the Purpose of Listing Multiple Font Families in a Style?"