簡體   English   中英

Flutter:未應用自定義字體系列

[英]Flutter: Custom font family do not get applied

我正在嘗試為我的 Flutter 應用程序構建一個“主題”,因此它在整個項目中都是一致的。 所以我想到使用字體Roboto-RegularRoboto-BoldRoboto-Medium 下面是我的代碼

pubspec.yaml

name: customer
description: A new Flutter project.

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  google_fonts: ^0.2.0

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/images/logo.png
    - assets/images/lock_24px.png
    - assets/images/email_24px.png
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  fonts:
    - family: Roboto
      fonts:
        - asset: assets/fonts/Roboto-Regular.ttf
        - asset: assets/fonts/Roboto-Medium.ttf
        - asset: assets/fonts/Roboto-Bold.ttf
    - family: Ma Shan Zheng
      fonts:
        - asset: assets/fonts/MaShanZheng-Regular.ttf
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

Main.dart

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Customer App',
      theme: ThemeData(
          brightness: Brightness.light,
          primarySwatch: Colors.blue,
          fontFamily: 'Roboto',
          textTheme: TextTheme(
            headline: TextStyle(fontFamily: 'Roboto-Medium', fontSize: 20.0),
            button: TextStyle(fontFamily: 'Roboto-Bold', fontSize: 14.0, letterSpacing: 1.25),
          )),
      home: LoginPage(),
    );
  }
}

登錄.dart

Container(
            margin: EdgeInsets.only(top: 60, left: 25, right: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Flexible(
                    child: SizedBox(
                  width: double.infinity,
                  height: 45,
                  child: RaisedButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: new BorderRadius.circular(18.0),
                        side: BorderSide(color: Colors.red)),
                    child: Text("LOGIN", 
                    style: Theme.of(context).textTheme.button,),
                  ),
                ))
              ],
            ),
          )

您可以看到我使用了Raised Button的字體樣式。 然而,即使應用了其他樣式(例如:字體大小、字母間距), font family也從未被應用。 為什么是這樣?

我建議您使用新制作的可用包GoogleFonts 它使使用您想要的字體變得更加容易,並且它們是動態加載的,使您的應用程序更輕巧,並且它們具有您想要使用的 Roboto 字體:

MaterialApp(
  theme: ThemeData(
    textTheme: GoogleFonts.latoTextTheme(
      Theme.of(context).textTheme,
    ),
  ),
);

您對自定義字體的聲明不清楚。

改變這個:

 fonts:
    - family: Roboto
      fonts:
        - asset: assets/fonts/Roboto-Regular.ttf
        - asset: assets/fonts/Roboto-Medium.ttf
        - asset: assets/fonts/Roboto-Bold.ttf

變成這樣:

 fonts:
- family: Roboto
  fonts:
    - asset: assets/fonts/Roboto-Regular.ttf
    - asset: assets/fonts/Roboto-Medium.ttf
      weight: 600
    - asset: assets/fonts/Roboto-Bold.ttf
      weight: 700

並像這樣稱呼它們:

theme: ThemeData(
      brightness: Brightness.light,
      primarySwatch: Colors.blue,
      fontFamily: 'Roboto',
      textTheme: TextTheme(
        headline: TextStyle(fontWeight: FontWeight.normal, fontSize: 20.0),
        button: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, letterSpacing: 1.25),
      )),

用下面的代碼替換 Main.dart 中的代碼

 headline: TextStyle(fontFamily: 'Roboto',fontWeight: FontWeight.normal,fontSize: 20.0),
        button: TextStyle(fontFamily: 'Roboto',fontWeight: FontWeight.bold,fontSize: 14.0, letterSpacing: 1.25),

新年快樂 :)

您已為三種不同的字體類型定義了相同的名稱,因此當您致電 Roboto 時,他將不知道您想要哪一種。 例如,當您調用 Roboto-Medium 時,沒有定義該名稱,只是它的源文件調用 Roboto-Medium.ttf 並且不能直接調用。 嘗試為每種字體類型定義一個名稱,如下所示:

- family: Roboto-Regular
  fonts:
    - asset: assets/fonts/Roboto-Regular.ttf
- family: Roboto-Meduim
  fonts:
    - asset: assets/fonts/Roboto-Medium.ttf
- family: Roboto-Bold
  fonts: 
    - asset: assets/fonts/Roboto-Bold.ttf

之后,您可以通過“Roboto-Medium”在您的代碼中調用它們

這里flutter開發網站( https://flutter.dev/docs/cookbook/design/fonts )說的是......

文件夾路徑:

awesome_app/
  fonts/
    Raleway-Regular.ttf
    Raleway-Italic.ttf
    RobotoMono-Regular.ttf
    RobotoMono-Bold.ttf

宣布:

flutter:
  fonts:
    - family: Raleway
      fonts:
        - asset: fonts/Raleway-Regular.ttf
        - asset: fonts/Raleway-Italic.ttf
          style: italic
    - family: RobotoMono
      fonts:
        - asset: fonts/RobotoMono-Regular.ttf
        - asset: fonts/RobotoMono-Bold.ttf
          weight: 700

在整個應用程序中使用:

MaterialApp(
  title: 'Custom Fonts',
  // Set Raleway as the default app font.
  theme: ThemeData(fontFamily: 'Raleway'),
  home: MyHomePage(),
);

在特定小部件中使用:

Text(
  'Roboto Mono sample',
  style: TextStyle(fontFamily: 'RobotoMono'),
);

嘗試更換

- asset: assets/fonts/Roboto-Regular.ttf

- asset: fonts/Roboto-Regular.ttf

當您無法在項目中添加字體時,有兩個電源問題:

  1. 檢查你在 yaml 文件中的縮進。 這很重要,因為 yaml 文件中的空間是有意義的。

  2. 從頭開始重新加載您的模擬器。 第一次學習 Flutter 時,我堅持了 2 個小時。 重新啟動它,庫將在您的 yaml 文件中添加字體。 包和

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM