我正在尝试从 Google Fonts 导入 Roboto Slab 字体,但不断收到错误消息。 我正在使用 Visual Studio 代码。字体的目录位置是 lib/fonts/Robo_Slab
这是我正在使用该字体的文件
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AppBar Title',
style: TextStyle(
fontFamily: 'RobotoSlab'
),
),
actions: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.search)
),
IconButton(
onPressed: () {},
icon: const Icon(Icons.more_vert)
)
],
)
);
}
}
这是 pubspec.yaml 文件
name: flutter_application_2
description: "A new Flutter project."
publish_to: 'none'
version: 0.1.0
environment:
sdk: '>=3.2.5 <4.0.0'
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
fonts:
- family: RobotoSlab
fonts:
- asset: fonts/Roboto_Slab/RobotoSlab-VariableFont_wght.ttf
感谢您的帮助。
您忘记了路径的
lib/
部分。必须是
- asset: lib/fonts/Roboto_Slab/RobotoSlab-VariableFont_wght.ttf