介绍

你可以在你的Flutter应用程序中使用不同的字体。例如,您可能会使用您的设计人员创建的自定义字体,或者您可能会使用Google Fonts(需翻墙))中的字体。

本页介绍如何为Flutter应用配置字体,并在渲染文本时使用它们。

使用字体

在Flutter应用程序中使用字体分两步完成。首先在pubspec.yaml中声明它们,以确保它们包含在应用程序中。然后通过TextStyle属性使用字体。

在asset中声明

要在应用中包含字体,请先在pubspec.yaml中声明它。然后将字体文件复制到您在pubspec.yaml中指定的位置。

  1. flutter:
  2. fonts:
  3. - family: Raleway
  4. fonts:
  5. - asset: assets/fonts/Raleway-Regular.ttf
  6. - asset: assets/fonts/Raleway-Medium.ttf
  7. weight: 500
  8. - asset: assets/fonts/Raleway-SemiBold.ttf
  9. weight: 600
  10. - family: AbrilFatface
  11. fonts:
  12. - asset: assets/fonts/abrilfatface/AbrilFatface-Regular.ttf

使用字体

  1. // declare the text style
  2. const textStyle = const TextStyle(
  3. fontFamily: 'Raleway',
  4. );
  5. // use the text style
  6. var buttonText = const Text(
  7. "Use the font for this text",
  8. style: textStyle,
  9. );

依赖包中的字体{#from-packages}

要使用依赖包中定义的字体,必须提供package参数。例如,假设上面的字体声明位于pubspec.yaml中声明的my_package包中。然后创建TextStyle的过程如下:

  1. const textStyle = const TextStyle(
  2. fontFamily: 'Raleway',
  3. package: 'my_package',
  4. );

如果包内部使用它定义的字体,则也应该在创建文本样式时指定package参数,如上例所示。

一个包也可以提供字体文件而不需要在pubspec.yaml中声明。 这些文件应该包的lib/文件夹中。字体文件不会自动绑定到应用程序中,应用程序可以在声明字体时有选择地使用这些字体。假设一个名为my_package的包中有一个:

  1. lib/fonts/Raleway-Medium.ttf

然后,应用程序可以声明一个字体,如下面的示例所示:

  1. flutter:
  2. fonts:
  3. - family: Raleway
  4. fonts:
  5. - asset: assets/fonts/Raleway-Regular.ttf
  6. - asset: packages/my_package/fonts/Raleway-Medium.ttf
  7. weight: 500

lib/是隐含的,所以它不应该包含在asset路径中。

在这种情况下,由于应用程序本地定义了字体,所以创建的TextStyle没有package参数:

  1. const textStyle = const TextStyle(
  2. fontFamily: 'Raleway',
  3. );

使用Material Design字体图标

如果要使用Material Design字体图标,可以通过向pubspec.yaml文件添加属性uses-material-design: true来简单包含它。

  1. flutter:
  2. # The following line ensures that the Material Icons font is
  3. # included with your application, so that you can use the icons in
  4. # the Icons class.
  5. uses-material-design: true

pubspec.yaml 选项定义

family 是字体的名称, 你可以在TextStylefontFamily 属性中使用.

asset 是相对于 pubspec.yaml 文件的路径.这些文件包含字体中字形的轮廓。在构建应用程序时,这些文件会包含在应用程序的asset包中。

可以给字体设置粗细、倾斜等样式

  • weight属性指定字体的粗细,取值范围是100到900之间的整百数(100的倍数). 这些值对应 FontWeight, 可以用于 TextStylefontWeight属性

TextStyle

如果一个 TextStyle 对象指定了一个没有确切字体文件的weight或style,那么引擎会为该字体使用一个通用的文件,并尝试为指定的weight和style推断一个轮廓。

例子

以下是在应用程序中声明和使用字体的示例。

iOS Android
Display of the fonts on ios. Display of the fonts on android.

在pubsec.yaml中声明字体。

  1. name: my_application
  2. description: A new Flutter project.
  3. dependencies:
  4. flutter:
  5. sdk: flutter
  6. flutter:
  7. # Include the Material Design fonts.
  8. uses-material-design: true
  9. fonts:
  10. - family: Rock Salt
  11. fonts:
  12. # https://fonts.google.com/specimen/Rock+Salt
  13. - asset: fonts/RockSalt-Regular.ttf
  14. - family: VT323
  15. fonts:
  16. # https://fonts.google.com/specimen/VT323
  17. - asset: fonts/VT323-Regular.ttf
  18. - family: Ewert
  19. fonts:
  20. # https://fonts.google.com/specimen/Ewert
  21. - asset: fonts/Ewert-Regular.ttf

源码是:

  1. import 'package:flutter/material.dart';
  2. const String words1 = "Almost before we knew it, we had left the ground.";
  3. const String words2 = "A shining crescent far beneath the flying vessel.";
  4. const String words3 = "A red flair silhouetted the jagged edge of a wing.";
  5. const String words4 = "Mist enveloped the ship three hours out from port.";
  6. void main() {
  7. runApp(new MyApp());
  8. }
  9. class MyApp extends StatelessWidget {
  10. @override
  11. Widget build(BuildContext context) {
  12. return new MaterialApp(
  13. title: 'Flutter Fonts',
  14. theme: new ThemeData(
  15. primarySwatch: Colors.blue,
  16. ),
  17. home: new FontsPage(),
  18. );
  19. }
  20. }
  21. class FontsPage extends StatefulWidget {
  22. @override
  23. _FontsPageState createState() => new _FontsPageState();
  24. }
  25. class _FontsPageState extends State<FontsPage> {
  26. @override
  27. Widget build(BuildContext context) {
  28. // Rock Salt - https://fonts.google.com/specimen/Rock+Salt
  29. var rockSaltContainer = new Container(
  30. child: new Column(
  31. children: <Widget>[
  32. new Text(
  33. "Rock Salt",
  34. ),
  35. new Text(
  36. words2,
  37. textAlign: TextAlign.center,
  38. style: new TextStyle(
  39. fontFamily: "Rock Salt",
  40. fontSize: 17.0,
  41. ),
  42. ),
  43. ],
  44. ),
  45. margin: const EdgeInsets.all(10.0),
  46. padding: const EdgeInsets.all(10.0),
  47. decoration: new BoxDecoration(
  48. color: Colors.grey.shade200,
  49. borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
  50. ),
  51. );
  52. // VT323 - https://fonts.google.com/specimen/VT323
  53. var v2t323Container = new Container(
  54. child: new Column(
  55. children: <Widget>[
  56. new Text(
  57. "VT323",
  58. ),
  59. new Text(
  60. words3,
  61. textAlign: TextAlign.center,
  62. style: new TextStyle(
  63. fontFamily: "VT323",
  64. fontSize: 25.0,
  65. ),
  66. ),
  67. ],
  68. ),
  69. margin: const EdgeInsets.all(10.0),
  70. padding: const EdgeInsets.all(10.0),
  71. decoration: new BoxDecoration(
  72. color: Colors.grey.shade200,
  73. borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
  74. ),
  75. );
  76. // https://fonts.google.com/specimen/Ewert
  77. var ewertContainer = new Container(
  78. child: new Column(
  79. children: <Widget>[
  80. new Text(
  81. "Ewert",
  82. ),
  83. new Text(
  84. words4,
  85. textAlign: TextAlign.center,
  86. style: new TextStyle(
  87. fontFamily: "Ewert",
  88. fontSize: 25.0,
  89. ),
  90. ),
  91. ],
  92. ),
  93. margin: const EdgeInsets.all(10.0),
  94. padding: const EdgeInsets.all(10.0),
  95. decoration: new BoxDecoration(
  96. color: Colors.grey.shade200,
  97. borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
  98. ),
  99. );
  100. // Material Icons font - included with Material Design
  101. String icons = "";
  102. // https://material.io/icons/#ic_accessible
  103. // accessible: &#xE914; or 0xE914 or E914
  104. icons += "\u{E914}";
  105. // https://material.io/icons/#ic_error
  106. // error: &#xE000; or 0xE000 or E000
  107. icons += "\u{E000}";
  108. // https://material.io/icons/#ic_fingerprint
  109. // fingerprint: &#xE90D; or 0xE90D or E90D
  110. icons += "\u{E90D}";
  111. // https://material.io/icons/#ic_camera
  112. // camera: &#xE3AF; or 0xE3AF or E3AF
  113. icons += "\u{E3AF}";
  114. // https://material.io/icons/#ic_palette
  115. // palette: &#xE40A; or 0xE40A or E40A
  116. icons += "\u{E40A}";
  117. // https://material.io/icons/#ic_tag_faces
  118. // tag faces: &#xE420; or 0xE420 or E420
  119. icons += "\u{E420}";
  120. // https://material.io/icons/#ic_directions_bike
  121. // directions bike: &#xE52F; or 0xE52F or E52F
  122. icons += "\u{E52F}";
  123. // https://material.io/icons/#ic_airline_seat_recline_extra
  124. // airline seat recline extra: &#xE636; or 0xE636 or E636
  125. icons += "\u{E636}";
  126. // https://material.io/icons/#ic_beach_access
  127. // beach access: &#xEB3E; or 0xEB3E or EB3E
  128. icons += "\u{EB3E}";
  129. // https://material.io/icons/#ic_public
  130. // public: &#xE80B; or 0xE80B or E80B
  131. icons += "\u{E80B}";
  132. // https://material.io/icons/#ic_star
  133. // star: &#xE838; or 0xE838 or E838
  134. icons += "\u{E838}";
  135. var materialIconsContainer = new Container(
  136. child: new Column(
  137. children: <Widget>[
  138. new Text(
  139. "Material Icons",
  140. ),
  141. new Text(
  142. icons,
  143. textAlign: TextAlign.center,
  144. style: new TextStyle(
  145. inherit: false,
  146. fontFamily: "MaterialIcons",
  147. color: Colors.black,
  148. fontStyle: FontStyle.normal,
  149. fontSize: 25.0,
  150. ),
  151. ),
  152. ],
  153. ),
  154. margin: const EdgeInsets.all(10.0),
  155. padding: const EdgeInsets.all(10.0),
  156. decoration: new BoxDecoration(
  157. color: Colors.grey.shade200,
  158. borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
  159. ),
  160. );
  161. return new Scaffold(
  162. appBar: new AppBar(
  163. title: new Text("Fonts"),
  164. ),
  165. body: new ListView(
  166. children: <Widget>[
  167. rockSaltContainer,
  168. v2t323Container,
  169. ewertContainer,
  170. materialIconsContainer,
  171. ],
  172. ),
  173. );
  174. }
  175. }