Arief Wijaya
Arief Wijaya

Reputation: 896

Flutter custom font multi weight doesn't work properly

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?

Upvotes: 2

Views: 1154

Answers (1)

Arief Wijaya
Arief Wijaya

Reputation: 896

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 .

Upvotes: 4

Related Questions