如何在不为每个粗细指定新系列的情况下选择不同的字体粗细?
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
和小部件:
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),
),
实际的 Montserrat-Bold 仅与“TEST2”一起使用。我尝试在 pubspec.yaml 中使用“Packages get”并重新启动应用程序。
最佳答案
来自 the docs我们得到这个常量列表:
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
所以在 pubspec 中你可以像这样定义你的自定义字体:
fonts:
- family: Montserrat
fonts:
- asset: fonts/Montserrat-Regular.ttf
- asset: fonts/Montserrat-SemiBold.ttf
weight: 600
- asset: fonts/Montserrat-Bold.ttf
weight: 700
并像这样在您的代码中使用它:
final h1 = new TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.w600 // semi-bold
);
关于android - Flutter 不选择基于 fontWeight 的自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53687104/