【问题标题】:How do I use google fonts on defining a theme in flutter如何使用谷歌字体在颤振中定义主题
【发布时间】:2021-01-24 00:30:27
【问题描述】:

我是 Flutter 新手。

我安装了this 库以便能够使用谷歌字体。

现在我需要在主题数据定义中这样做,并尝试这样但不允许

ThemeData appTheme() {
  return ThemeData(
    ...
    fontFamily: GoogleFonts.openSans(),
  );
}

我该怎么做?非常感谢

【问题讨论】:

  • fontFamily 需要 String。使用GoogleFonts.openSans().fontFamily
  • 非常感谢@Riwen,如果你想添加它作为答案我可以接受它然后
  • 将我的评论重新发布为实际答案。谢谢。

标签: flutter fonts google-fonts


【解决方案1】:

将以下代码粘贴到您的主题文件中,并将其添加到您的 ThemeData(textTheme: textTheme)

final TextTheme textTheme = TextTheme(
  headline1: GoogleFonts.roboto(
      fontSize: 97, fontWeight: FontWeight.w300, letterSpacing: -1.5),
  headline2: GoogleFonts.roboto(
      fontSize: 61, fontWeight: FontWeight.w300, letterSpacing: -0.5),
  headline3: GoogleFonts.roboto(fontSize: 48, fontWeight: FontWeight.w400),
  headline4: GoogleFonts.roboto(
      fontSize: 34, fontWeight: FontWeight.w400, letterSpacing: 0.25),
  headline5: GoogleFonts.roboto(fontSize: 24, fontWeight: FontWeight.w400),
  headline6: GoogleFonts.roboto(
      fontSize: 20, fontWeight: FontWeight.w500, letterSpacing: 0.15),
  subtitle1: GoogleFonts.roboto(
      fontSize: 16, fontWeight: FontWeight.w400, letterSpacing: 0.15),
  subtitle2: GoogleFonts.roboto(
      fontSize: 14, fontWeight: FontWeight.w500, letterSpacing: 0.1),
  bodyText1: GoogleFonts.roboto(
      fontSize: 16, fontWeight: FontWeight.w400, letterSpacing: 0.5),
  bodyText2: GoogleFonts.roboto(
      fontSize: 14, fontWeight: FontWeight.w400, letterSpacing: 0.25),
  button: GoogleFonts.roboto(
      fontSize: 14, fontWeight: FontWeight.w500, letterSpacing: 1.25),
  caption: GoogleFonts.roboto(
      fontSize: 12, fontWeight: FontWeight.w400, letterSpacing: 0.4),
  overline: GoogleFonts.roboto(
      fontSize: 10, fontWeight: FontWeight.w400, letterSpacing: 1.5),
);

这个视频很好的解释了如何在flutter中使用主题和字体https://youtu.be/stoJpMeS5aY?t=640

【讨论】:

  • 获取她在视频中显示的代码的网站是否可用?
  • 可用,可以获取flutter、web和android xml的代码。 material.io/design/typography/the-type-system.html#type-scale
  • 谢谢。我不得不给日文接受的答案,因为这正是我所要求的
  • 如果您使用的是 Google 字体并想调整字体大小,这就是这种方式。
【解决方案2】:

重新发布我的评论:fontFamily 需要 String。使用GoogleFonts.openSans().fontFamily

【讨论】:

    猜你喜欢
    • 2020-05-29
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2022-01-14
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多