Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter not picking custom fonts based on fontWeight

How to choose different font weights without specifying new family for each weight?

  fonts:  - family: Montserrat    fonts:      - asset: assets/fonts/Montserrat-Regular.ttf        weight: 100      - asset: assets/fonts/Montserrat-Bold.ttf        weight: 700  - family: MontserratBold    fonts:      - asset: assets/fonts/Montserrat-Bold.ttf 

and the widgets:

                      child: Text(                     'TEST',                     style: TextStyle(                       fontSize: 17.4,                       fontFamily: 'Montserrat',                       fontWeight: FontWeight.w700,                       color: Colors.black87,                     ),                   ), 

..

                      child: Text(                     'TEST2',                     style: TextStyle(                         fontSize: 17.4,                         fontFamily: 'MontserratBold',                         color: Colors.black87),                   ), 

The actual Montserrat-Bold is only used with 'TEST2'. I have tried using 'Packages get' in pubspec.yaml and restarting the app.

like image 314
Android Avatar asked Dec 08 '18 21:12

Android


People also ask

Does Flutter support OTF fonts?

The flutter recent version v1.12.13 does not support the open type font(otf) directly. You have to convert the otf to true type font(ttf) somehow. You can use this third-party website to covert the font to ttf before using it in your project.

How do I use TTF in Flutter?

Importing font files in a project After downloading, we need to add the font files to our Flutter project. Create a folder named fonts in the root of the Flutter project. Next, move the Montserrat font files or . ttf files into the fonts folder that you just created.


2 Answers

From the docs we get this list of constants:

w100 Thin, the least thick
w200 Extra-light
w300 Light
w400 Normal / regular / plain
w500 Medium
w600 Semi-bold
w700 Bold
w800 Extra-bold
w900 Black, the most thick

So in pubspec you can define your custom font like this:

  fonts:    - family: Montserrat      fonts:        - asset: fonts/Montserrat-Regular.ttf        - asset: fonts/Montserrat-SemiBold.ttf          weight: 600        - asset: fonts/Montserrat-Bold.ttf          weight: 700 

and use it in your code like this:

final h1 = new TextStyle(     fontSize: 24.0,      fontWeight: FontWeight.w600  // semi-bold ); 
like image 155
Jannie Theunissen Avatar answered Sep 20 '22 11:09

Jannie Theunissen


Font & style settings in pubspec are actually ignored: https://github.com/flutter/flutter/issues/36739#issuecomment-521806077

You need to check what weight is described in metadata inside Montserrat-Bold.ttf, perhaps it's not 700.

For displaying the metadata, you can use this site: https://fontdrop.info/, the font weight is displayed under Data/usWeightClass.

like image 42
szotp Avatar answered Sep 19 '22 11:09

szotp