Flutter custom font multi weight doesn't work properly

…衆ロ難τιáo~ 提交于 2021-01-29 12:14:43

问题


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

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

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?


回答1:


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 .



来源:https://stackoverflow.com/questions/61252792/flutter-custom-font-multi-weight-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!