导入和使用自定义字体无效

时间:2020-03-30 03:31:40

标签: flutter

我正在使用Android Studio作为我的IDE。现在,我正在尝试使用自定义字体。但是我的代码无法正常工作。到目前为止,这是我所做的。我从fonts.google.com下载字体,并将TTF文件导入到我的项目中,并在项目根文件夹下创建了一个名为fonts的新文件夹。

enter image description here

然后,将以下代码添加到pubspec.yml文件中。

fonts:
    - family: Roboto
      fonts:
        - asset: fonts/Roboto-Regular.ttf
        - asset: fonts/Roboto-Bold.ttf
          weight: 400

然后我将自定义字体设置为“文本”小部件,如下所示。

Text("Flight", textDirection: TextDirection.ltr,style: TextStyle(fontSize: 40.0, color: Colors.white, fontFamily: "Roboto", fontWeight: FontWeight.w400),)

启动应用程序时,它不起作用。字体不变。我的代码有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

在pubspec.yml中添加代码后,应在终端中运行flutter pub get。此命令获取pubspec.yaml中列出的所有依赖项