Skip to content Skip to sidebar Skip to footer

What Is the Purpose of Listing Multiple Font Families in a Style?

Use a custom font

Contents
  • 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:

  1. Import the font files.
  2. Declare the font in the pubspec.
  3. Set a font as the default.
  4. 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 the FontWeight and can be used in the fontWeight belongings of a TextStyle object. For example, if you want to utilise the RobotoMono-Bold font divers higher up, you would set fontWeight to FontWeight.w700 in your TextStyle.

    Note that defining the weight property does non override the bodily weight of the font. You lot would non be able to access RobotoMono-Assuming with FontWeight.w100, even if its weight was set to 100.

  • The style holding specifies whether the outlines in the file are italic or normal. These values represent to the FontStyle and can be used in the fontStyle property of a TextStyle object. For case, if you want to use the Raleway-Italic font defined above, you would set up fontStyle to FontStyle.italic in your TextStyle.

    Annotation that defining the way property does non override the actual style of the font; You would not be able to access Raleway-Italic with FontStyle.normal, even if its style 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'),         ),       ),     );   } }

Custom Fonts Demo

lawsonmemered.blogspot.com

Source: https://docs.flutter.dev/cookbook/design/fonts

Post a Comment for "What Is the Purpose of Listing Multiple Font Families in a Style?"