[英]How to change the default font family in Flutter
How can I change every text of my app to use a specific font?如何更改我的应用程序的每个文本以使用特定字体? I can change them individually by using the
TextStyle()
but how can I make my app default to a specific font?我可以使用
TextStyle()
单独更改它们,但如何使我的应用程序默认为特定字体? Can you show me how?你能告诉我怎么做吗?
You can change the default font family of your Flutter app by following the below steps:您可以按照以下步骤更改 Flutter 应用程序的默认字体系列:
1. Add your font files into your project folder. 1. 将您的字体文件添加到您的项目文件夹中。 Say
Project Folder > assets > fonts > hind
.说
Project Folder > assets > fonts > hind
。
2. Declare the font family with font files with style in your project's pubspec.yaml
file as (An example): 2. 在项目的
pubspec.yaml
文件中使用带有样式的字体文件将字体系列声明为(示例):
MaterialApp
widget of your main class file, define the default font family as:MaterialApp
小部件中,将默认字体系列定义为:Flutter works with custom fonts and you can apply a custom font across an entire app or to individual widgets. Flutter 使用自定义字体,您可以在整个应用程序或单个小部件中应用自定义字体。 This recipe creates an app that uses custom fonts with the following steps:
本秘籍创建一个使用自定义字体的应用程序,步骤如下:
1. Import the font files 1. 导入字体文件
To work with a font, import the font files into the project.要使用字体,请将字体文件导入到项目中。 It's common practice to put font files in a
fonts
or assets
folder at the root of a Flutter project.通常的做法是将字体文件放在 Flutter 项目根目录下的
fonts
或assets
文件夹中。
For example, to import the Raleway and Roboto Mono font files into a project, the folder structure might look like this:例如,要将 Raleway 和 Roboto Mono 字体文件导入到项目中,文件夹结构可能如下所示:
awesome_app/
fonts/
Raleway-Regular.ttf
Raleway-Italic.ttf
RobotoMono-Regular.ttf
RobotoMono-Bold.ttf
2. Declare the font in the pubspec Once you've identified a font, tell Flutter where to find it. 2. 在 pubspec 中声明字体一旦你确定了一种字体,告诉 Flutter 在哪里可以找到它。 You can do this by including a font definition in the
pubspec.yaml
file.您可以通过在
pubspec.yaml
文件中包含字体定义来完成此操作。
flutter:
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
- asset: fonts/Raleway-Italic.ttf
style: italic
3. Set a font as the default You have two options for how to apply fonts to text: as the default font or only within specific widgets. 3. 将字体设置为默认字体您有两个选项可以将字体应用于文本:作为默认字体或仅在特定小部件中使用。
To use a font as the default, set the fontFamily
property as part of the app's theme
.要将字体用作默认字体,请将
fontFamily
属性设置为应用程序theme
。 The value provided to fontFamily
must match the family
name declared in the pubspec.yaml
.提供给
fontFamily
的值必须与pubspec.yaml
声明的family
名称相匹配。
MaterialApp(
title: 'Custom Fonts',
// Set Raleway as the default app font.
theme: ThemeData(fontFamily: 'Raleway'),
home: MyHomePage(),
);
4. Use the font in a specific widget 4.在特定小部件中使用字体
Text(
'Roboto Mono sample',
style: TextStyle(fontFamily: 'RobotoMono'),
);
Complete example pubspec.yaml完整示例pubspec.yaml
name: custom_fonts
description: An example of how to use custom fonts with Flutter
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
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
uses-material-design: true
main.dart main.dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Fonts',
// Set Raleway as the default app font.
theme: ThemeData(fontFamily: 'Raleway'),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
// The AppBar uses the app-default Raleway font.
appBar: AppBar(title: Text('Custom Fonts')),
body: Center(
// This Text widget uses the RobotoMono font.
child: Text(
'Roboto Mono sample',
style: TextStyle(fontFamily: 'RobotoMono'),
),
),
);
}
}
If you want to use one of these Google fonts then use the official google_fonts package.如果您想使用这些Google 字体之一,请使用官方google_fonts包。
dependencies:
google_fonts: ^2.1.0
MaterialApp(
theme: ThemeData(
textTheme: GoogleFonts.latoTextTheme(
Theme.of(context).textTheme,
),
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.