android - Flutter 不选择基于 fontWeight 的自定义字体

标签 android ios fonts flutter

如何在不为每个粗细指定新系列的情况下选择不同的字体粗细?

  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/

相关文章:

android - 从内部类/监听器中获取对象

iphone - Cocos2d - 自上而下的旋转相机 View

android - 如何清除启动 Activity 的 Intent?

java - 即使我完成 Activity ,Firebase 连接仍保持 Activity 状态吗?

java - Android Studio : clang error: unknown argument: '-mandroid'

fonts - Windows 字体可清楚地消除数字零与字母 O 的歧义

java - 更改 JTextArea(不是 JTextPane)中的行距

ios - Swift:for循环中的异步调用

ios - 获取元素的值而不解析ios中的所有xml文件

java - 错误插入符位置消息