Search code examples
flutterfontspubspec.yaml

Purpose of style and weight in pubspec.yaml file when still usable while using them in TextStyle?


I am learning to properly use custom fonts in my Flutter project. I downloaded from Google Fonts, the Merienda family which had both variable font file and static font file. I declared in my pubspec.yaml the following:

fonts:
  - family: Merienda
    fonts:
      - asset: assets/fonts/Merienda-Regular.ttf

I have not used any other of the static fonts apart from the Merienda-Regular.ttf as above, or the variable font from the font archive downloaded from Google Fonts.

Question: While I have only declared the Merienda-Regular.ttf file in pubspec.yaml, I can still apply different font weights to the font using fontWeight: FontWeight.bold or different styles using fontStyle: FontStyle.italic in the TextStyle. How is this possible? If so, are those declarations of weight and style under the respective font names e.g. Merienda-Bold.ttf, Merienda-ExtraBold.ttf redundant or useless?


Solution

  • As mentioned in the official documentation:

    When you import a font file that doesn't include either multiple fonts within it or variable font capabilities, don't use the style or weight property to adjust how they display. If you do use those properties on a regular font file, Flutter attempts to simulate the look. The visual result will look quite different from using the correct font file.

    So, no—they are not redundant or useless. Without declaring them when using a custom font, you only get a simulation of what different weights and styles should actually look like.