首页 > 解决方案 > 在 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”等默认字体,但不适用于自定义字体。

标签: flutterdartfontsflutter-dependencies

解决方案


运行主功能后你还没有配置主题

像这样:

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)), ...);

推荐阅读