Pubspec 文件的 Flutter 部分
每个 Flutter 项目都包含一个 pubspec.yaml
文件,通常被称为 pubspec。当你创建一个新的 Flutter 项目时,会生成一个基本的 pubspec。它位于项目的根目录,包含 Dart 和 Flutter 工具需要了解的项目元数据。
pubspec 是用 YAML 写的,它具有可读性,但要注意 缩进符号(制表符和空格)很重要。
Every Flutter project includes a pubspec.yaml
file,
often referred to as the pubspec.
A basic pubspec is generated when you create
a new Flutter project. It’s located at the top
of the project tree and contains metadata about
the project that the Dart and Flutter tooling
needs to know. The pubspec is written in
YAML, which is human readable, but be aware
that white space (tabs v spaces) matters.
pubspec 文件指定了项目所需的依赖,如特定的 package(及其版本)、字体或图像文件。它还指定了其他配置,如对开发者 package 的依赖(如测试或模拟 package),或对 Flutter SDK 版本的特殊限制。
The pubspec file specifies dependencies that the project requires, such as particular packages (and their versions), fonts, or image files. It also specifies other requirements, such as dependencies on developer packages (like testing or mocking packages), or particular constraints on the version of the Flutter SDK.
Dart 和 Flutter 项目共有的字段在 dart.dev 的 pubspec 文件 中描述。本页列出了只对 Flutter 项目有效的 Flutter 特定的 字段。
Fields common to both Dart and Flutter projects are described in the pubspec file on dart.dev. This page lists Flutter-specific fields that are only valid for a Flutter project.
当你用 flutter create
命令创建一个新项目时(或通过使用你的 IDE 中的相应按钮),它会为每一个 Flutter 应用程序创建 pubspec。
When you create a new project with the
flutter create
command (or by using the
equivalent button in your IDE), it creates
a pubspec for a basic Flutter app.
下面是一个 Flutter 项目的 pubspec 文件的示例。只有 Flutter 可用的字段会高亮显示。
Here is an example of a Flutter project pubspec file. The Flutter only fields are highlighted.
name: <project name>
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter: # Required for every Flutter project
sdk: flutter # Required for every Flutter project
cupertino_icons: ^1.0.2 # Only required if you use Cupertino (iOS style) icons
dev_dependencies:
flutter_test:
sdk: flutter # Required for a Flutter project that includes tests
flutter:
uses-material-design: true # Required if you use the Material icon font
assets: # Lists assets, such as image files
- images/a_dot_burr.jpeg
- images/a_dot_ham.jpeg
fonts: # Required if your app uses custom 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
资源
Assets
常见的资源包括静态数据(例如 JSON 文件)、配置文件、图标和图像(JPEG、WebP、GIF、动画 WebP/GIF、PNG、BMP 和 WBMP)。
Common types of assets include static data (for example, JSON files), configuration files, icons, and images (JPEG, WebP, GIF, animated WebP/GIF, PNG, BMP, and WBMP).
除了列出应用 package 中包含的图片,一个图片资源还可以引用一个或多个特定分辨率的「变体」。想要了解更多信息,请参阅 资源和图像 页面的 分辨率相关 部分。关于从 package 的依赖关系中添加资源的信息,见同一页的 package 依赖关系中的图片资源 部分。
Besides listing the images that are included in the app package, an image asset can also refer to one or more resolution-specific “variants”. For more information, see the resolution aware section of the Assets and images page. For information on adding assets from package dependencies, see the asset images in package dependencies section in the same page.
字体
Fonts
如上例所示,字体部分的每个条目都应该有一个包含字体家族名称的 family
键,以及一个包含指定字体的资源和其他描述符的 fonts
键。
As shown in the above example,
each entry in the fonts section should have a
family
key with the font family name,
and a fonts
key with a list specifying the
asset and other descriptors for the font.
关于使用字体的例子,请参见 Flutter 实用教程 中的 使用自定义字体 和 从 package 中导出字体 教程。
For examples of using fonts see the Use a custom font and Export fonts from a package recipes in the Flutter cookbook.
更多信息
More information
要查看更多有关 package、插件和 pubspec 的信息,请参考下面文档:
For more information on packages, plugins, and pubspec files, see the following:
-
dart.dev 上介绍的 创建 package
Creating packages on dart.dev
-
dart.dev 上介绍的 package 的术语表
Glossary of package terms on dart.dev
-
dart.dev 上介绍的 package 的依赖
Package dependencies on dart.dev
-
dart.dev 上介绍的 使用 package
-
dart.dev 上介绍的 避免提交的内容
What not to commit on dart.dev