The pubspec file in Flutter is similar to a simple Dart package. Besides that, it contains an additional section for configurations specific to Flutter. Let's see the pubspec.yaml file's contents in details:
name: hello_flutter
description: A new Flutter project.
version: 1.0.0+1
The beginning of the file is simple. As we already know, the name property is defined when we execute the pub create command, followed by the default project description.
The version property follows the Dart package conventions: the version number, plus an optional build version number separated by +. In addition to that, Flutter allows you to override these values during the build. We will take a more detailed look at that in Chapter 12, Testing, Debugging, and Deployment, in the App deployment section.
Then we have the dependencies section of the pubspec file:
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
dev_dependencies:
flutter_test:
sdk: flutter
Now, have a look at the explanation of the preceding code:
- We start with the environment property with the Dart SDK version constraints defined. You are OK to use the version provided by the tool because it's followed by the Flutter SDK updates as well.
- Then we have the dependencies property, which starts with the main dependency of a Flutter application, the Flutter SDK itself, which contains many of Flutter's core packages.
- As an additional dependency, the generator adds the cupertino_icons package, which contains icon assets used by the built-in Flutter Cupertino widgets (there's more on that in the next chapter).
- The dev_dependencies property contains only the flutter_test package dependency provided by the Flutter SDK itself, and contains Flutter-specific extensions to the already-known Dart test package.
In the final block of the file, there's a dedicated flutter section:
flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# ...
# To add custom fonts to your application, add a fonts section here,
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
#
This flutter section allows us to configure resources that are bundled in the application to be used during runtime, such as images, a fonts, and a JSON file, typically, any non-source code file that helps in the app's composition:
- uses-material-design: We will see the Material widgets provided by Flutter in the next chapter. In addition to them, we can use also Material Design icons (https://material.io/tools/icons/?style=baseline), which are in a custom font format. For this to work properly, we need to activate this property (set it to true) so the icons are included in the application.
- assets: This property is used to list the resource paths that will be bundled with the final application. Check the following code for more details on how to use it. The assets files can be organized in any way; what matters for Flutter is the path of the files. You specify the path of the file relative to the project's root. This is used later in Dart code when you need to refer to an asset file. Here's an example:
assets:
- images/home_background.jpeg
To add an image to be used later, we add the path in the assets list, or if we want to add all files inside the directory, we just specify the directory path:
assets:
- images/
This includes all files inside the directory. Note the / character at the end.
- fonts: This property allows us to add custom fonts to the application. There's more on that in Chapter 6, Theming and Styling, in the Custom fonts section.