Flutter脚本执行插件flutter_scripts的使用

发布于 1周前 作者 yibo5220 来自 Flutter

Flutter脚本执行插件flutter_scripts的使用

flutter_scripts

coverage

Very Good CLI 生成 🤖

flutter_scripts 允许你在 pubspec.yaml 文件中定义脚本,并通过 run 命令来执行这些脚本,类似于在 Node.js 中使用 npm run


入门指南 🚀

安装

如果你希望通过 Pub.dev 安装该 CLI 工具,可以全局安装:

dart pub global activate flutter_scripts

或者,你可以从本地路径安装:

dart pub global activate --source=path <path to this package>

使用

以下是一些常用的命令示例:

# 运行所有定义的脚本
$ flutter_scripts run

# 指定要运行的命令
$ flutter_scripts run --c update_db

# 显示 CLI 版本
$ flutter_scripts --version

# 显示帮助信息
$ flutter_scripts --help

pubspec.yaml 中添加脚本

你可以在任何 Flutter/Dart 项目中的 pubspec.yaml 文件中添加脚本。例如:

scripts:
  generate_icons: flutter pub run flutter_launcher_icons:main
  generate_files: flutter pub run build_runner build --delete-conflicting-outputs

完整示例 Demo

以下是一个完整的示例,展示了如何在 Flutter 项目中使用 flutter_scripts 插件。

1. 创建一个新的 Flutter 项目

flutter create my_flutter_app
cd my_flutter_app

2. 安装 flutter_scripts

dart pub global activate flutter_scripts

3. 修改 pubspec.yaml 文件

pubspec.yaml 文件中添加一些脚本。例如,我们添加两个脚本:一个用于生成应用图标,另一个用于生成代码文件。

name: my_flutter_app
description: A new Flutter project.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.17.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_launcher_icons: ^0.9.2  # 用于生成应用图标
  build_runner: ^2.1.7           # 用于生成代码文件

flutter:
  uses-material-design: true

scripts:
  generate_icons: flutter pub run flutter_launcher_icons:main
  generate_files: flutter pub run build_runner build --delete-conflicting-outputs

4. 配置 flutter_launcher_icons

为了生成应用图标,我们需要在 pubspec.yaml 文件中添加 flutter_launcher_icons 的配置。在 flutter 部分下添加以下内容:

flutter:
  uses-material-design: true

  # 添加图标配置
  flutter_icons:
    android: "launcher_icon"
    ios: true
    image_path: "assets/icon/icon.png"

确保在 assets/icon/ 目录下有一个名为 icon.png 的图标文件。

5. 执行脚本

现在你可以使用 flutter_scripts 来执行定义的脚本。例如,生成应用图标:

flutter_scripts run --c generate_icons

或者生成代码文件:

flutter_scripts run --c generate_files

6. 查看帮助信息

如果你想查看 flutter_scripts 的更多用法,可以使用以下命令:

flutter_scripts --help

更多关于Flutter脚本执行插件flutter_scripts的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter脚本执行插件flutter_scripts的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用flutter_scripts插件来执行自定义脚本的示例。flutter_scripts是一个Flutter插件,允许你在pubspec.yaml文件中定义和执行自定义的Dart脚本。

1. 添加flutter_scripts依赖

首先,你需要在你的pubspec.yaml文件中添加flutter_scripts依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_scripts: ^x.y.z  # 请替换为最新版本号

然后运行flutter pub get来安装依赖。

2. 定义自定义脚本

在你的Flutter项目根目录下创建一个名为tools的文件夹,并在其中创建一个Dart脚本文件,例如custom_script.dart。这个文件将包含你想要执行的自定义逻辑。

// tools/custom_script.dart

void main() {
  print('Hello from custom script!');
  // 在这里添加你的自定义逻辑
}

3. 配置flutter_scripts

在你的pubspec.yaml文件中,添加flutter_scripts配置部分来定义你的自定义脚本:

flutter:
  # 其他配置...

flutter_scripts:
  custom:
    script: tools/custom_script.dart

4. 执行自定义脚本

现在,你可以通过运行以下命令来执行你的自定义脚本:

flutter pub run flutter_scripts:custom

运行该命令后,你应该会在终端中看到输出:

Hello from custom script!

完整示例

以下是一个完整的示例,包括pubspec.yamlcustom_script.dart的内容:

pubspec.yaml

name: my_flutter_app
description: A new Flutter application.

# 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.3
# 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.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_scripts: ^x.y.z  # 请替换为最新版本号

# Development dependencies are flutter tools that can be run via "flutter pub get"
# for development. They are not included in your app's build.
dev_dependencies:
  flutter_test:
    sdk: flutter

# 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

flutter_scripts:
  custom:
    script: tools/custom_script.dart

tools/custom_script.dart

// tools/custom_script.dart

void main() {
  print('Hello from custom script!');
  // 在这里添加你的自定义逻辑
}

通过这种方式,你可以轻松地在Flutter项目中定义和执行自定义的Dart脚本。

回到顶部