概要
- 开发工具配置及基础项目创建
- 项目多环境配置
- 基于多环境运行
一、项目创建
开发环境的基础配置可以参考Flutter官网教程有详细的介绍步骤,在此我就不再做简介了。开发环境配置好后创建一个基础项目。
本项目环境信息如下:
Flutter SDK:2.10.2
关于SDK版本,建议安装fvm作版本管理,类似nvm。具体的配置可以自行百度配置安装。安装配置后,对于vscode工具的使用者需要修改setting配置,讲SDK版本指定fvm来处理。
二、flutter_flavorizr多环境配置
通过flutter_flavorizr包实现多环境配置切换。配置方式可以参考pub.dev官网简介。
安装相关包
fvm flutter pub add flutter_flavorizr
配置pubspec.yaml
name: copy_jd
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# 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.16.1 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
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: ^1.0.2
flutter_flavorizr: ^2.1.2
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
# 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:
# - 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.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: 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.dev/custom-fonts/#from-packages
flavorizr:
app:
android:
flavorDimensions: "flavor-type"
ios:
flavors:
dev:
app:
name: "copy_jd_dev"
android:
applicationId: "com.jd.dev"
ios:
bundleId: "com.jd.dev"
dit:
app:
name: "copy_jd_dit"
android:
applicationId: "com.jd.dit"
ios:
bundleId: "com.jd.dit"
sit:
app:
name: "copy_jd_sit"
android:
applicationId: "com.jd.sit"
ios:
bundleId: "com.jd.sit"
prod:
app:
name: "copy_jd_prd"
android:
applicationId: "com.jd"
ios:
bundleId: "com.jd"
执行生成命令
fvm flutter pub run flutter_flavorizr
命令执行之后,可以看到自动生成了很多文件 ,这些文件就是针对各个环境的程序入口
根据各个入口,创建及配置launch.json方便不同环境的调试
选择虚拟机
修改flavors.dart根据各个环境配置不同变量值,在入口文件中设置对应值
启动项目
三、全局启动控制
实际开发中,我们往往会遇到有些事情需要在app启动前进行初始化。因此我们需要在统一的地方处理这些事情。
创建一个app.start.dart.dart文件,在app启动前先执行,并指定runApp文件。
修改入口文件的入口程序