gpt4 book ai didi

flutter - 如何在flutter web中添加自定义字体?

转载 作者:行者123 更新时间:2023-12-03 23:51:21 26 4
gpt4 key购买 nike

所以我现在开发 flutter 移动应用程序有一段时间了,我想探索 flutter web,现在我尝试添加自定义字体,但它没有显示自定义字体。

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

我的主要.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,
),
);
}
}

我的公众号
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

最佳答案

在字体文件夹中添加字体,并在 pubspec.yaml 中添加例如引用给定的图像
字体文件夹:
enter image description here
pubspec.yaml
enter image description here
现在为网络添加 assets文件夹中的 web 部分并添加以下 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 应用程序很棒。

关于flutter - 如何在flutter web中添加自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com