一尘不染

如何更改Flutter for Web Font?

flutter

我正在尝试更改Flutter for Web Application中标题的字体,该怎么办?我发现pubsec.yaml与移动版本完全不同,这里是文件:

name: projectbaseclient
description: An app built using Flutter for web

environment:
  # You must be using Flutter >=1.5.0 or Dart >=2.3.0
  sdk: '>=2.3.0 <3.0.0'

dependencies:
  flutter_web: any
  flutter_web_ui: any

dev_dependencies:
  build_runner: ^1.5.0
  build_web_compilers: ^2.1.0
  pedantic: ^1.7.0

dependency_overrides:
  flutter_web:
    git:
      url: https://github.com/flutter/flutter_web
      path: packages/flutter_web
  flutter_web_ui:
    git:
      url: https://github.com/flutter/flutter_web
      path: packages/flutter_web_ui

这是FontManifest.json:

[
    {
      "family": "Poppins",
      "fonts": [
        {
          "asset": "fonts/Poppins-Regular.ttf"
        }
      ]
    },
    {
      "family": "KronaOne",
      "fonts": [
        {
          "asset": "fonts/KronaOne-Regular.ttf"
        }
      ]
    },
    {
      "family": "Ubuntu",
      "fonts": [
        {
          "asset": "fonts/Ubuntu-Regular.ttf"
        }
      ]
    }
  ]

我已经创建了一个文件夹asset /
fonts,并在该文件夹中添加了字体,我也创建了FontManifest.json并将其放在assets文件夹中,但是它的代码无法正常工作,当我在文本中更改fontFamily参数时,它给了我错误目的。

如何在Flutter for Web项目中使用字体?


阅读 296

收藏
2020-08-13

共1个答案

一尘不染

检查assets文件夹是否位于web文件夹中。接下来,将FontManifest.json内容与以下示例进行比较:

[
  {
    "family": "Rubik",
    "fonts": [
      {
        "asset": "fonts/Rubik-Regular.ttf"
      },
      {
        "asset": "fonts/Rubik-Medium.ttf",
        "weight": 500
      }
    ]
  }
]

更新

由于Flutter for
Web
项目当前已与主Flutter存储库合并,因此不再建议使用此方法。在web文件夹FontManifest.json文件中创建与由编译器一创建的冲突。在pubspec.yaml文件中指定字体。

2020-08-13