•  


Export fonts from a package | Flutter

Export fonts from a package

Rather than declaring a font as part of an app, you can declare a font as part of a separate package. This is a convenient way to share the same font across several different projects, or for coders publishing their packages to pub.dev . This recipe uses the following steps:

  1. Add a font to a package.
  2. Add the package and font to the app.
  3. Use the font.

1. Add a font to a package

#

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

In this example, assume you've got a Flutter library called awesome_package with fonts living in a lib/fonts folder.

awesome_package/

  lib/

    awesome_package.dart

    fonts/

      Raleway-Regular.ttf

      Raleway-Italic.ttf

2. Add the package and fonts to the app

#

Now you can use the fonts in the package by updating the pubspec.yaml in the app's root directory.

Add the package to the app

#

To add the awesome_package package as a dependency, run flutter pub add :

$ flutter pub add awesome_package

Declare the font assets

#

Now that you've imported the package, tell Flutter where to find the fonts from the awesome_package .

To declare package fonts, prefix the path to the font with packages/awesome_package . This tells Flutter to look in the lib folder of the package for the font.

yaml
flutter
:

  fonts
:

    - 
family
: 
Raleway

      fonts
:

        - 
asset
: 
packages/awesome_package/fonts/Raleway-Regular.ttf

        - 
asset
: 
packages/awesome_package/fonts/Raleway-Italic.ttf

          style
: 
italic

3. Use the font

#

Use a TextStyle to change the appearance of text. To use package fonts, declare which font you'd like to use and which package the font belongs to.

dart
child: 
Text
(

  'Using the Raleway font from the awesome_package'
,

  style: 
TextStyle
(

    fontFamily: 
'Raleway'
,

  ),

),

Complete example

#

Fonts

#

The Raleway and RobotoMono fonts were downloaded from Google Fonts .

pubspec.yaml

#
yaml
name
: 
package_fonts

description
: 
An example of how to use package fonts with Flutter


dependencies
:

  awesome_package
:

  flutter
:

    sdk
: 
flutter


dev_dependencies
:

  flutter_test
:

    sdk
: 
flutter


flutter
:

  fonts
:

    - 
family
: 
Raleway

      fonts
:

        - 
asset
: 
packages/awesome_package/fonts/Raleway-Regular.ttf

        - 
asset
: 
packages/awesome_package/fonts/Raleway-Italic.ttf

          style
: 
italic

  uses-material-design
: 
true

main.dart

#
dart
import
 'package:flutter/material.dart'
;


void
 main
() => 
runApp
(
const
 MyApp
());


class
 MyApp
 extends
 StatelessWidget
 {

  const
 MyApp
({
super
.key});


  @override

  Widget
 build
(
BuildContext
 context) {

    return
 const
 MaterialApp
(

      title: 
'Package Fonts'
,

      home: 
MyHomePage
(),

    );

  }

}


class
 MyHomePage
 extends
 StatelessWidget
 {

  const
 MyHomePage
({
super
.key});


  @override

  Widget
 build
(
BuildContext
 context) {

    return
 Scaffold
(

      // The AppBar uses the app-default font.

      appBar: 
AppBar
(title: 
const
 Text
(
'Package Fonts'
)),

      body: 
const
 Center
(

        // This Text widget uses the Raleway font.

        child: 
Text
(

          'Using the Raleway font from the awesome_package'
,

          style: 
TextStyle
(

            fontFamily: 
'Raleway'
,

          ),

        ),

      ),

    );

  }

}

Package Fonts Demo

- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본