【问题标题】:How to use a specific font from a list of fonts specified in a asset family in pubspec.yaml如何使用 pubspec.yaml 中资产系列中指定的字体列表中的特定字体
【发布时间】:2019-04-12 16:51:47
【问题描述】:

假设我在 pubspec.yaml 文件中添加了以下几行 字体:

fonts:
- family: GreatVibes
  fonts:
    - asset: fonts/GreatVibes-Regular.ttf
    - asset: fonts/GreatVibes-Bold.ttf

我通过以下代码行在我的应用程序中使用它。

new Text('My New Font',
        style: new TextStyle(
          color: Colors.white,
          fontFamily: 'GreatVibes',
          fontSize: 16.0,
        )),

我的问题是,在前面提供的两个 .ttf 文件中,flutter 是如何决定使用哪个文件的?

假设 Flutter 决定使用 GreatVibes-Bold.ttf,我该怎么做才能让它使用 GreatVibes-regular.ttf

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies


    【解决方案1】:

    【讨论】:

    • 你确定吗,你说的是颤振?
    • @Sandep,否决它,因为您的解决方案适用于 Android。问的问题是针对 Flutter 的。
    • @CopsOnRoad 我把它改成了flutter。
    • 不要使用链接来提供答案。为此,您有 cmets。
    【解决方案2】:

    如果我正确理解了这些字体 - 它必须是这样的:

    fonts:
    - family: GreatVibes
      fonts:
        - asset: fonts/GreatVibes-Regular.ttf
        - asset: fonts/GreatVibes-Bold.ttf
          weight: 700
        - asset: fonts/GreatVibes-Italic.ttf
          style: italic
    

    然后

    new Text('My New Font',
        style: new TextStyle(
          color: Colors.white,
          fontFamily: 'GreatVibes',
          fontWeight: FontWeight.w700,
          fontSize: 16.0,
        )),
    

    【讨论】:

    • 对不起,它只是改变字体的粗细,如果我在那里放置了斜体样式字体,我想为应用程序用户提供多种字体选择,但如果我必须指定'family ' 对于这些字体中的每一种,都会变得非常难以管理,这是我提出这个问题的唯一目的。
    • 我已编辑答案 - 添加斜体选项。我不知道如何将许多不同的字体放在一个系列中并像列表一样使用它们
    • 是的,事实上我们可以使用任何家族名称,它不需要与字体名称相同,所以如果必须选择黑白,比如说roboto和open sans,它会选择哪一个
    【解决方案3】:

    您可以将它直接用于小部件样式,或者使用我更喜欢从一个位置控制整个应用程序的 Theme 对象,这里是 main.dart 的示例:

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.purple,
          accentColor: Colors.deepOrange,
          **fontFamily: 'Lato'**),
    
     ....
      },
    );
    

    【讨论】:

      猜你喜欢
      • 2013-08-02
      • 1970-01-01
      • 2015-07-13
      • 2018-06-02
      • 2018-11-05
      • 2011-06-13
      • 2012-05-11
      • 2012-05-20
      • 2021-11-25
      相关资源
      最近更新 更多