Rather than declaring a font as part of an app, you can declare a font as partof a separate package. This is a convenient way to share the same font acrossseveral different projects, or for coders publishing their packages to thePub site.

Directions

  • Add a font to a package
  • Add the package and font to the app
  • Use the font

1. Add fonts to a package

To export a font from a package, you need to import the font files into thelib folder of the package project. You can place font files directly in thelib folder or in a subdirectory, such as lib/fonts.

In this example, assume you’ve got a Flutter library calledawesome_package with fonts living in a lib/fonts folder.

  1. awesome_package/
  2. lib/
  3. awesome_package.dart
  4. fonts/
  5. Raleway-Regular.ttf
  6. Raleway-Italic.ttf

2. Add the package and fonts to the app

You can now consume the package and use the fonts it provides.This involves updating the pubspec.yaml in the app’s root directory.

Add the package to the project

  1. dependencies:
  2. awesome_package: <latest_version>

Declare the font assets

Now that you’ve imported the package, you need to tell Flutter where tofind the fonts from the awesome_package.

To declare package fonts, you must must prefix the path to the font withpackages/awesome_package. This tells Flutter to look in the lib folderof the package for the font.

  1. flutter:
  2. fonts:
  3. - family: Raleway
  4. fonts:
  5. - asset: packages/awesome_package/fonts/Raleway-Regular.ttf
  6. - asset: packages/awesome_package/fonts/Raleway-Italic.ttf
  7. style: italic

3. Use the font

You can use a TextStyle to change the appearance of text.To use package fonts, you need to not only declare which font you’d like to use,you need to declare the package the font belongs to.

  1. Text(
  2. 'Using the Raleway font from the awesome_package',
  3. style: TextStyle(
  4. fontFamily: 'Raleway',
  5. package: 'awesome_package',
  6. ),
  7. );

Complete example

Fonts

The Raleway and RobotoMono fonts were downloaded fromGoogle Fonts.

pubspec.yaml

  1. name: package_fonts
  2. description: An example of how to use package fonts with Flutter
  3. dependencies:
  4. awesome_package:
  5. flutter:
  6. sdk: flutter
  7. dev_dependencies:
  8. flutter_test:
  9. sdk: flutter
  10. flutter:
  11. fonts:
  12. - family: Raleway
  13. fonts:
  14. - asset: packages/awesome_package/fonts/Raleway-Regular.ttf
  15. - asset: packages/awesome_package/fonts/Raleway-Italic.ttf
  16. style: italic
  17. uses-material-design: true

main.dart

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Package Fonts',
  8. home: MyHomePage(),
  9. );
  10. }
  11. }
  12. class MyHomePage extends StatelessWidget {
  13. @override
  14. Widget build(BuildContext context) {
  15. return Scaffold(
  16. // The AppBar uses the app-default Raleway font.
  17. appBar: AppBar(title: Text('Package Fonts')),
  18. body: Center(
  19. // This Text Widget uses the RobotoMono font.
  20. child: Text(
  21. 'Using the Raleway font from the awesome_package',
  22. style: TextStyle(
  23. fontFamily: 'Raleway',
  24. package: 'awesome_package',
  25. ),
  26. ),
  27. ),
  28. );
  29. }
  30. }

Package Fonts Demo