fontFamily属性无法正常运行

时间:2019-03-09 09:23:30

标签: dart flutter

我试图在flutter中更改fontFamily属性,以显示外观与默认外观不同的文本。不管我以何种方式分配给fontfamily的字体名称,默认字体都不会更改(常规字体类型如Arial,comic san,Times New Roman,Lucida等),在这里没有任何自定义。我认为,像这样微不足道的事情不应该引发这个问题。任何帮助将不胜感激。 以下是我的颤动代码:

pubspec.yaml文件:

name: dramil
description: A new Flutter application.

# 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.
# Read more about versioning at semver.org.
version: 1.0.0+1

environment:
  sdk: ">=2.0.0-dev.68.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
  #dart2_constant: ^1.0.2+dart2
  math_expressions: ^1.0.0
  shared_preferences: ^0.4.3

  material_search: ^0.2.8
  path_provider: '>=0.3.0'
  sqflite: any
  flutter_colorpicker: ^0.2.1
  intl: ^0.15.7
  #auto_size_text: ^0.3.0
  cloud_firestore: ^0.9.5+2
  animated_text_kit: ^1.3.0
  share: ^0.6.0+1
  url_launcher: ^5.0.1
  flutter_launcher_icons: ^0.7.0



dev_dependencies:
  flutter_test:
    sdk: flutter




# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/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

  assets:
  - assets/
  - assets/images/
  - assets/icons/launcher_icon.png




  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg

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

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/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: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - 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.io/custom-fonts/#from-packages
flutter_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/icons/Launcher.png"

这是我尝试更改fontfamily属性的代码段:

DrawerHeader(
                child: Column(
                  children: <Widget>[
                    Text("The text i am trying to change",style: TextStyle(fontSize: 20,fontFamily: "Arial",color: Colors.yellowAccent),),
                  ],
                ),
                //decoration: BoxDecoration(color: Colors.brown[400],),
                decoration: BoxDecoration(
                  gradient: LinearGradient(colors: [Colors.blueGrey,Colors.orangeAccent],
                      begin: FractionalOffset.topLeft,
                      end: FractionalOffset.bottomRight,
                      stops: [0.0,1.0],
                      tileMode: TileMode.clamp
                  ),

                ),
              ),

1 个答案:

答案 0 :(得分:0)

Flutter只有一个默认的fontFamily,即Roboto。默认情况下,您的应用中使用的其他fontFamily未包括在内,并且不会引起任何错误,因为当flutter无法找到您指定的fontFamily值时,会使用fontFamilyFallback 。因此,您必须使用以下步骤将自定义字体导入到flutter项目中:

  • Font.ttf添加到项目assets,并在您的pubspec.yaml文件中对其进行定义:

    fonts:
        - family: Raleway // you can give it any name to call it later
          fonts:
            - asset: Raleway-Regular.ttf //this is the name of the font file you added itside your assets folder
    
  • 运行命令flutter packages get,以便可以在项目中使用字体。

  • 然后您可以包括它并注意样式更改:

        Text("The text i am trying to change",style: TextStyle(fontSize: 20,fontFamily: 'Raleway',color: Colors.yellowAccent),),
    
  • 您可以从google font网站下载字体,也可以选择使用自定义字体。