最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked 2 days ago rustyrusty 2341 gold badge1 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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.

To be completely honest a lot of work and too much to be confused on your first try, I would suggest just use Google_Fonts package. Reason being is that you don't have to hard code in into the yaml file and bloat it with unnecessary space.

Text(
  'Text', 
  style: GoogleFonts.merienda,
),

For further information, you should read more from the link I provided.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论