Flutter自定义字体多重粗细无法正常工作 [英] Flutter custom font multi weight doesn't work properly

查看:96
本文介绍了Flutter自定义字体多重粗细无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在颤振中使用具有多重粗细的家庭字体.但是,当我使用粗体为粗体(w700)粗体(w800)的字体样式时,它不起作用.仅工作到 semibold/medium(w600)

I'm trying to use a family font with multi weight in a flutter. But when I use font style with weight bold (w700) and heavy(w800), it doesn't work. Only work until semibold/medium (w600)

这是我的字体资源文件夹结构:

Here is my font assets folder structure :

myapp\assets\fonts\unisans\unisans_Bold.ttf
myapp\assets\fonts\unisans\unisans_Heavy.ttf
myapp\assets\fonts\unisans\unisans_Reguler.ttf
myapp\assets\fonts\unisans\unisans_Semibold.ttf

pubspec.yaml中定义的字体:

Defined fonts in pubspec.yaml :

 fonts:
    - family: Unisans
      fonts:
        - asset: assets/fonts/unisans/unisans_Reguler.ttf
        - asset: assets/fonts/unisans/unisans_Semibold.ttf
          weight: 600
        - asset: assets/fonts/unisans/unisans_Bold.ttf
          weight: 700
        - asset: assets/fonts/unisans/unisans_Heavy.ttf
          weight: 800

然后像这样颤抖地称呼他们:

And call them in flutter like this :

Text(
  "Halo",
  style: TextStyle(
     color: Theme.of(context).primaryColor,
     fontFamily: "Unisans",
     fontWeight: FontWeight.w800,
     fontSize: 30),
)

我也尝试了FontWeight.w700,但仍然没有应用.它仅适用于 reguler w600 样式.

I also tried FontWeight.w700, but still didn't applied. It is only work with reguler and w600 style.

颤动是否不能与多种粗细字体一起使用,还是取决于字体?

Does flutter cannot work with multi weight of fonts or it depends on the fonts?

推荐答案

我突然发现了问题.非常琐碎的问题.这是因为每种字体具有不同的字体名称".例如:Unisans-Bold具有字体名称(UnisansBold)而不是Unisans.Unisans-Heavy具有字体名称(UnisansHeavy)而不是Unisans.在颤动中,要使粗细样式起作用,所有字体文件必须具有相同的字体名称.在Windows OS中打开字体文件时,我会检查字体名称.但是我仍然很好奇,即使我们在pubspec.yaml上声明了它,为什么还要慌张地考虑它.

I suddenly found the problem. Very trivial problem. This is because each font has different "Font Name". For example: Unisans-Bold has Font Name (UnisansBold) Instead of Unisans. Unisans-Heavy has Font Name (UnisansHeavy) instead of Unisans. In flutter, to make the weight style works, all of the font files must have same Font Name. I check the font name when open the font file in the windows OS. But I'm still curious why flutter consider it even when we declared it on pubspec.yaml .

这篇关于Flutter自定义字体多重粗细无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆