flutter_assets_generator 0.1.0 copy "flutter_assets_generator: ^0.1.0" to clipboard
flutter_assets_generator: ^0.1.0 copied to clipboard

Automatically generate a resource file by scanning pubspec.yaml to correspond to the flutter's asset, imitation R file short name.

flutter_assets_generator #

在 lib/const/resource.dart 中 自动生成 R 文件(仿安卓短命名)

中文文档

截图 #

img

安装及使用 #

使用源码的方式 #

添加 dart 至环境变量

git clone https://github.com/goodswifter/flutter_assets_generator.git
cd flutter_assets_generator
dart bin/flutter_assets_generator.dart ./example

./example 是 flutter 项目的地址

pub global #

比较推荐这种方式, pub global 的详情参阅官方文档

添加 pub,dart 到 $PATH 环境变量下, 如果不添加也可以, 使用 dart,pub 全路径也可以

参阅 官方文档 添加~/.pub-cache/bin 至环境变量下(window 请查阅文档)

保证

dart --version
pub --version

有正确的输出

安装: $ dart pub global activate flutter_assets_generator

使用: 在 flutter 目录下执行

fgen

or

fgen -s .

注意这个. , 这里第二个目录就是你的 flutter 目录, 可以省略,省略后默认在当前文件夹

也就是在 flutter 项目下使用fgen即可

支持的命令行参数 #

使用 $ fgen -h$ fgen --help 可以查看帮助文档

fgen -h
-w, --[no-]watch    Continue to monitor changes after execution of orders.
                    (defaults to on)

-p, --[no-]preview  Generate file with preview comments.
                    (defaults to on)

-o, --output        Your resource file path.
                    If it is a relative path, the relative flutter root directory
                    (defaults to "lib/const/resource.dart")

-s, --src           Flutter project root path
                    (defaults to ".")

-n, --name          The class name for the constant.
                    (defaults to "R")

-h, --[no-]help     Help usage

-d, --[no-]debug    debug info

-s 是 flutter 目录

-o 是生成的资源文件地址,需要包含.dart

如果你在 flutter 目录下执行, 仅需 fgen 即可

可以加 --no-watch 参数来不监听文件变化,仅生成一次资源文件

关于文件名 #

文件中的空格、/-. 会被转为 _@ 会被转为 _AT_

转化的例子如下

    images/1.png => IMAGES_PNG
    images/hello_world.jpg => IMAGES_HELLO_WORLD_JPG
    images/hello-world.jpg => IMAGES_HELLO_WORLD_JPG

会包含文件夹名称的原因是你 pubspec 中可能会包含多个文件夹目录, 或你的文件夹会包含多层级,甚至你的资产目录中会包含非图片(如数据库,json 等)资产

如下情况会出现错误

  images/
    main_login.png
    main/
      login.png

因为两个的字段命名方式是完全相同的

配置文件 #

配置文件为约定式,不支持通过命令指定,该文件为项目根目录下(与pubspec.yaml同级)下的fgen.yaml

排除和导入 #

使用glob语法

其中 exclude节点下为排除的文件名,类型是字符串数组,如未包含任何规则则表示不排除任何文件

include表示需要导入的文件名,字符串数组,如果未包含任何规则表示允许所有

优先级方面,exclude 高于 include,换句话说:

先根据 include 节点导入文件,然后 exclude 排除文件

典型示例

exclude:
  - "**/add*.png"
  - "**_**"

include:
  - "**/a*.png"
  - "**/b*"
  - "**/c*"
assets
├── address.png   # exclude by "**/add*.png"
├── address@at.png  # exclude by "**/add*.png"
├── bluetoothon-fjdfj.png
├── bluetoothon.png
└── camera.png

images
├── address space.png  # exclude by "**/add*.png"
├── address.png  # exclude by "**/add*.png"
├── addto.png  # exclude by "**/add*.png"
├── audio.png
├── bluetooth_link.png  # exclude by **_**
├── bluetoothoff.png
├── child.png
└── course.png
/// Generate by [resource_generator](https://github.com/CaiJingLong/flutter_resource_generator) library.
/// PLEASE DO NOT EDIT MANUALLY.
class R {

  /// ![preview](file:///Users/jinglongcai/code/dart/self/flutter_resource_generator/example/assets/bluetoothon-fjdfj.png)
  static const String ASSETS_BLUETOOTHON_FJDFJ_PNG = 'assets/bluetoothon-fjdfj.png';

  /// ![preview](file:///Users/jinglongcai/code/dart/self/flutter_resource_generator/example/assets/bluetoothon.png)
  static const String ASSETS_BLUETOOTHON_PNG = 'assets/bluetoothon.png';

  /// ![preview](file:///Users/jinglongcai/code/dart/self/flutter_resource_generator/example/assets/camera.png)
  static const String ASSETS_CAMERA_PNG = 'assets/camera.png';

  /// ![preview](file:///Users/jinglongcai/code/dart/self/flutter_resource_generator/example/images/audio.png)
  static const String IMAGES_AUDIO_PNG = 'images/audio.png';

  /// ![preview](file:///Users/jinglongcai/code/dart/self/flutter_resource_generator/example/images/bluetoothoff.png)
  static const String IMAGES_BLUETOOTHOFF_PNG = 'images/bluetoothoff.png';

  /// ![preview](file:///Users/jinglongcai/code/dart/self/flutter_resource_generator/example/images/child.png)
  static const String IMAGES_CHILD_PNG = 'images/child.png';

  /// ![preview](file:///Users/jinglongcai/code/dart/self/flutter_resource_generator/example/images/course.png)
  static const String IMAGES_COURSE_PNG = 'images/course.png';
}
2
likes
130
pub points
49%
popularity

Publisher

unverified uploader

Automatically generate a resource file by scanning pubspec.yaml to correspond to the flutter's asset, imitation R file short name.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

args, dart_style, flutter, glob, io, path, yaml

More

Packages that depend on flutter_assets_generator