简体   繁体   English

如何在此 flutter 代码中添加自定义字体

[英]How can I add custom font in this flutter code

I need to add my own custom font file to the code below.我需要将我自己的自定义字体文件添加到下面的代码中。 I don't want to use google fonts here.我不想在这里使用谷歌 fonts。 How can I add custom font.如何添加自定义字体。 I already added the font file in pubspec.yaml and the folder.我已经在 pubspec.yaml 和文件夹中添加了字体文件。

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

/// Google fonts constant setting: https://fonts.google.com/
TextTheme kTextTheme(theme, String language) {
  switch (language) {
    case 'vi':
      return GoogleFonts.montserratTextTheme(theme);
    case 'ar':
      return GoogleFonts.ralewayTextTheme(theme);
    default:
      return GoogleFonts.robotoTextTheme(theme);
  }
}

TextTheme kHeadlineTheme(theme, [language = 'en']) {
  switch (language) {
    case 'vi':
      return GoogleFonts.montserratTextTheme(theme);
    case 'ar':
      return GoogleFonts.ralewayTextTheme(theme);
   
    default:
      return GoogleFonts.robotoTextTheme(theme);
  }
}

you can follow my answer你可以按照我的回答

How to use custom fonts in Flutter?如何在 Flutter 中使用自定义 fonts?

The default font used in Flutter is Roboto. Flutter 中使用的默认字体是 Roboto。 If you want to add a custom font in Flutter, you have 2 ways.如果要在 Flutter 中添加自定义字体,有两种方法。 If the font you want to add is available in Google Fonts, you can use google_fonts package available in Dart Pub.如果您要添加的字体在 Google Fonts 中可用,您可以使用 Dart Pub 中提供的 google_fonts package。 If the font you want to use is not available in Google fonts, you must add a TTF file of the font to assets and load it using pubspec.yaml.如果您要使用的字体在 Google fonts 中不可用,则必须将字体的 TTF 文件添加到资产并使用 pubspec.yaml 加载它。 Both methods are described in the following tutorial.以下教程中描述了这两种方法。

  1. Using google_fonts Package Install Package Add google_fonts to pubspec.yaml as shown in below.使用 google_fonts Package 安装 Package 将 google_fonts 添加到 pubspec.yaml 中,如下所示。 There are no other configurations required for the plugin on both iOS and Android operating systems. iOS 和 Android 操作系统上的插件不需要其他配置。

    dependencies: flutter: sdk: flutter google_fonts: ^1.1.0依赖项:flutter:sdk:flutter google_fonts:^1.1.0

Usage google_fonts package can be used to apply any desired font available on google fonts both as TextStyle of a Text() widget and inside the Theme() widget.使用 google_fonts package 可用于应用 google fonts 上可用的任何所需字体,既可用作 Text() 小部件的 TextStyle,也可用作 Theme() 小部件内部。 Both the examples are shown below.这两个示例如下所示。

/// Example usage in text widget
/// as style property
Text(
  'This is an example of using GoogleFonts in text widget',
  style: GoogleFonts.montserrat(
    textStyle: Theme.of(context).textTheme.headline3,
    fontWeight: FontWeight.w400,
    fontStyle: FontStyle.italic,
    color: primaryColor),
)

/// Example usage in TextTheme
TextTheme(
      headline2: GoogleFonts.lato(
          color: Colors.black,
          fontSize: 48.0,
          letterSpacing: -1.5
      ),
),
  1. Adding fonts not available in Google fonts Add TTF file of the font to assets The first step is to get the TTF file of the font you would like to use and add it to the assets of your app.添加 fonts 在 Google 中不可用 fonts 将字体的 TTF 文件添加到资产 第一步是获取您要使用的字体的 TTF 文件并将其添加到应用程序的资产中。 The folder structure is shown in the image below.文件夹结构如下图所示。

Add font to pubspec.yaml Now we need to add the font to pubspec.yaml as shown below.将字体添加到 pubspec.yaml 现在我们需要将字体添加到 pubspec.yaml,如下所示。 This will load the font for usage inside our app.这将加载字体以在我们的应用程序中使用。

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  fonts:
    - family: Lora
      fonts:
        - asset: assets/fonts/Lora-Regular.ttf
        - asset: assets/fonts/Lora-Bold.ttf
          weight: 800
        - asset: assets/fonts/Lora-Medium.ttf
          weight: 600
        - asset: assets/fonts/Lora-SemiBold.ttf
          weight: 500
        - asset: assets/fonts/Lora-Italic.ttf
          style: italic
          weight: 500
  uses-material-design: true
  assets:
    - assets/icons/

Usage Example usage of the font both as style of a Text() widget.用法 字体的示例用法都作为 Text() 小部件的样式。

/// Example usage of cutom font in Text widget
Text(
  "Example usage of cutom font in Text",
  textAlign: TextAlign.center,
    style: Theme.of(context).textTheme.subtitle1.copyWith(
      fontFamily: 'Lora',
    ),
)

in details https://educity.app/flutter/how-to-use-custom-fonts-in-flutter详细说明https://educity.app/flutter/how-to-use-custom-fonts-in-flutter

You can simply copy your font file to the assets folder or where ever you wish and add it to pubspec.yaml font section.您可以简单地将字体文件复制到资产文件夹或任何您希望的位置,然后将其添加到 pubspec.yaml 字体部分。

.yaml file: .yaml 文件:

# example:
  fonts:
    - family: Lato
      fonts:
        - asset: fonts/Lato-Regular.ttf

After this basic setup you can directly use the added font in the TextStyle and return that instead of the GoogleFont完成此基本设置后,您可以直接使用 TextStyle 中添加的字体并返回它而不是 GoogleFont

TextTheme kTextTheme(theme, String language) {
  switch (language) {
    case 'vi':
      return return TextStyle(
      fontFamily: 'Lato',
      color: Colors.black,
      fontSize: 18,
    );
    case 'ar':
      return return TextStyle(
      fontFamily: 'Lato',
      color: Colors.black,
      fontSize: 18,
    );
    default:
      return return TextStyle(
      fontFamily: 'Lato',
      color: Colors.black,
      fontSize: 18,
    );
  }
}
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM