copyWith 上的 GoogleFont-粗细问题

Md.*_*ikh 3 google-font-api dart flutter flutter-web

我正在创建一个文本样式模型并使用 getter 来获得使用google_fonts 的文本样式。当我提供财产时会出现此问题fontWeight:。此外,它fontWeight不提供与 GoogleFont 类似的外观。

\n

我已经在另一个项目上进行了测试,使用html renderer. 我已经检查过这个问题,但它不起作用。

\n

风格比较

\n

对比图

\n
\n

但从GoogleFont 来看

\n
\n

在此输入图像描述

\n

flutter doctor -v没有任何问题

\n
Flutter (Channel stable, 2.5.2, on Microsoft Windows [Version\n    10.0.19043.1288], locale en-US)\n    \xe2\x80\xa2 Flutter version 2.5.2 at C:\\Tools\\flutter\n    \xe2\x80\xa2 Upstream repository https://github.com/flutter/flutter.git        \n    \xe2\x80\xa2 Framework revision 3595343e20 (3 weeks ago), 2021-09-30 12:58:18  \n      -0700\n    \xe2\x80\xa2 Engine revision 6ac856380f\n    \xe2\x80\xa2 Dart version 2.14.3\n
Run Code Online (Sandbox Code Playgroud)\n

模型类

\n
Flutter (Channel stable, 2.5.2, on Microsoft Windows [Version\n    10.0.19043.1288], locale en-US)\n    \xe2\x80\xa2 Flutter version 2.5.2 at C:\\Tools\\flutter\n    \xe2\x80\xa2 Upstream repository https://github.com/flutter/flutter.git        \n    \xe2\x80\xa2 Framework revision 3595343e20 (3 weeks ago), 2021-09-30 12:58:18  \n      -0700\n    \xe2\x80\xa2 Engine revision 6ac856380f\n    \xe2\x80\xa2 Dart version 2.14.3\n
Run Code Online (Sandbox Code Playgroud)\n

测试小工具

\n
class AppTextStyles {\n  static TextStyle get normalMidBlod => const TextStyle(\n        fontWeight: FontWeight.bold,\n        fontSize: 33,\n      );\n\n  static TextStyle get latoMidBlod => GoogleFonts.lato(\n        fontWeight: FontWeight.bold, //this one\n        color: Colors.black,\n        fontSize: 33,\n      );\n  static TextStyle get lato => GoogleFonts.lato(\n        color: Colors.black,\n        fontSize: 33,\n      );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Joe*_*ler 7

google_fonts_flutter 包存储库上有一个错误讨论了这一点: https: //github.com/material-foundation/google-fonts-flutter/issues/141

总而言之,当您创建 Google Font TextStyle 时,fontFamily 将设置为您指定的原始粗细(例如“poppins-w400”),而不是常规 fontFamily 名称(“poppins”)。

您可以通过在 copyWith 方法中指定 fontFamily 来解决此问题。

 TextSpan(
                  text: "  w900 \n",
                  style: AppTextStyles.latoMidBlod
                       .copyWith(
                          fontWeight: FontWeight.w900,
                          fontFamily: GoogleFonts.poppins().fontFamily,
                     ),
                ),
Run Code Online (Sandbox Code Playgroud)