无法在 Flutter 中添加自定义字体
Trouble adding custom fonts in Flutter
我在 Flutter 中添加自定义字体时没有收到错误,但它没有应用字体。
我已经尝试过完全重建、重新安装以及我从其他类似主题中读到的东西,但到目前为止没有任何效果。这是我的部分 pubspec.yaml
flutter:
uses-material-design: true
fonts:
- family: Bauhaus
fonts:
- asset: fonts/Bauhaus.ttf
这是一个代码示例...
new TextSpan(
text: 'Started!',
style: new TextStyle(
decoration: TextDecoration.none,
fontSize: 50.0,
fontFamily: "Bauhaus",
color: Colors.red,
),
),
它适用于 "Times New Roman" 等默认字体,但不适用于自定义字体。
你还没有在运行主函数
之后配置主题
像这样:
fonts:
- family: SFUIText
fonts:
- asset: assets/fonts/SFUIText-Regular.ttf
- asset: assets/fonts/SFUIText-Semibold.ttf
weight: 600
- asset: assets/fonts/SFUIText-RegularItalic.ttf
style: italic
final app = MaterialApp(
theme: ThemeData(
primaryIconTheme: const IconThemeData(color: mColorIcon),
primaryColor: mColorPrimary,
accentColor: mColorAccent,
backgroundColor: mColorBackground,
indicatorColor: mColorPrimary,
splashColor: mColorPrimary,
canvasColor: mColorCanvas,
appBarTheme: Styles.appBar,
fontFamily: 'SFUIText',
textTheme: TextTheme(
title: Styles.title,
body1: Styles.textSecondary,
body2: Styles.textSecondaryBold,
headline: Styles.headline,
subhead: Styles.subHeading,
button: Styles.button,
overline: Styles.overline)), ...);
我在 Flutter 中添加自定义字体时没有收到错误,但它没有应用字体。
我已经尝试过完全重建、重新安装以及我从其他类似主题中读到的东西,但到目前为止没有任何效果。这是我的部分 pubspec.yaml
flutter:
uses-material-design: true
fonts:
- family: Bauhaus
fonts:
- asset: fonts/Bauhaus.ttf
这是一个代码示例...
new TextSpan(
text: 'Started!',
style: new TextStyle(
decoration: TextDecoration.none,
fontSize: 50.0,
fontFamily: "Bauhaus",
color: Colors.red,
),
),
它适用于 "Times New Roman" 等默认字体,但不适用于自定义字体。
你还没有在运行主函数
之后配置主题像这样:
fonts:
- family: SFUIText
fonts:
- asset: assets/fonts/SFUIText-Regular.ttf
- asset: assets/fonts/SFUIText-Semibold.ttf
weight: 600
- asset: assets/fonts/SFUIText-RegularItalic.ttf
style: italic
final app = MaterialApp(
theme: ThemeData(
primaryIconTheme: const IconThemeData(color: mColorIcon),
primaryColor: mColorPrimary,
accentColor: mColorAccent,
backgroundColor: mColorBackground,
indicatorColor: mColorPrimary,
splashColor: mColorPrimary,
canvasColor: mColorCanvas,
appBarTheme: Styles.appBar,
fontFamily: 'SFUIText',
textTheme: TextTheme(
title: Styles.title,
body1: Styles.textSecondary,
body2: Styles.textSecondaryBold,
headline: Styles.headline,
subhead: Styles.subHeading,
button: Styles.button,
overline: Styles.overline)), ...);