如何在Flutter网站中添加自定义字体?

时间:2019-08-04 06:56:39

标签: flutter custom-font flutter-web

所以我现在正在开发Flutter移动应用程序一段时间,并且我想浏览Flutter Web,现在我尝试添加自定义字体,但未显示自定义字体。

我已经添加了所有依赖项,并为字体创建了一个单独的文件夹,并像在flutter mobile中一样将其添加到pubspec.yaml中。

我的main.dart


class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: Text("Dextroxd",
            style: TextStyle(
              fontSize: 36.0,
              fontFamily: 'CookieFam',
              color: Colors.white,
              fontWeight: FontWeight.w500
            )),
        centerTitle: true,
      ),
    );
  }
}

我的pubspec

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'


flutter:
  fonts:
    - family: CookieFam
      fonts:
       - asset: assets/Cookie-Regular.ttf



dependencies:
  flutter_web: any
  flutter_web_ui: any

dev_dependencies:
  build_runner: ^1.6.2
  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

1 个答案:

答案 0 :(得分:2)

在字体文件夹中添加字体,并在pubspec.yaml中添加例如引用给定图像

字体文件夹:

enter image description here

pubspec.yaml

enter image description here

现在进入网络,在网络部分添加assets文件夹,并在下面的JSON文件中添加名称为web / assets / FontManifest.json

enter image description here

[
  {
    "family": "MuliBold",
    "fonts": [
      {
        "asset": "fonts/Muli-Bold.tff"
      }
    ]
  },
  {
    "family": "MuliExtraBold",
    "fonts": [
      {
        "asset": "fonts/Muli-ExtraBold.ttf"
      }
    ]
  },
{
    "family": "MuliRegular",
    "fonts": [
      {
        "asset": "fonts/Muli-Regular.ttf"
      }
    ]
  }
]

太好了,现在您的字体已应用于Flutter Web应用程序。