搜尋套件、儲存庫與 APP 分類

套件詳情v1.2.0

flutter_asset_generator

透過掃描 pubspec.yaml 中定義的資源,產生資源檔案,模擬 R 檔案的簡短名稱。

AndroidiOSmacOSWindowsLinux
近 30 天下載
8200
喜歡數
112
Pub 分數
130

安裝套件

flutter pub add flutter_asset_generator

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個用於 Flutter 的程式碼產生工具,會自動掃描 pubspec.yaml 中列出的資源,並產生資源類別,類似於 Android 的 R.java。它透過類型安全、編譯時期檢查的參考來簡化資源存取,減少錯誤並提升可維護性。產生的檔案為影像、字型及其他資源提供簡短且易讀的名稱,從而簡化跨多個平台的開發工作流程。

適用情境

  • 簡化大型Flutter專案中的資產管理
  • 減少因拼寫錯誤的資源路徑導致的執行時錯誤
  • 啟用對應用資源的類型安全存取
  • 在建構過程中自動執行資源類別產生
  • 透過簡潔的資源引用改善程式碼可讀性

主要特色

  • 從 pubspec.yaml 自動產生資源類別
  • 提供簡短、乾淨的資源名稱
  • 類型安全的資源存取
  • 支援影像、字型及其他資源
  • 跨平台相容性

適合對象

  • 在 Flutter 應用中管理大量資源的開發者
  • 希望減少資產相關錯誤的團隊
  • 需要一致的資源命名規範的專案
  • 建構需要自動化資源產生的流程
  • 針對多個平台的應用程式

使用建議

  • 需要設定 build_runner 和依賴項
  • 產生的檔案不應手動編輯
  • 可能會稍微增加建構時間
  • 需要正確配置 pubspec.yaml
  • 最佳搭配程式碼分析工具使用

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

flutter_asset_generator

English | 中文文档

Automatically generate the dart file for pubspec.yaml

The purpose of this library is to help flutter developers automatically generate asset corresponding dart files to help developers release their hands from this meaningless job, and the open source community has a lot of the same functionality.

This library is based on dartlang's build library.

screenshot

img

Usage

Run from source

Add dart to your $PATH environment.

git clone https://github.com/fluttercandies/flutter_asset_generator
cd flutter_asset_generator
dart pub get
dart bin/asset_generator.dart $flutter_project
Run from pub global
  1. Install using pub global:
dart pub global activate flutter_asset_generator

# or 

dart pub global activate -s git https://github.com/fluttercandies/flutter_asset_generator.git                 
  1. Run below commands: fgen or fgen -s $flutter_project
Support options

Use $ fgen -h or $ fgen --help see usage document.

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's 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

File name

Space, '.' and '-' in the path will be converted to _. @ will be converted to _AT_.

convert filed name example:

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

Errors will occur in the following situations:

images/
├── main_login.png
├── main/
    ├── login.png

Because the two field names will be exactly the same.

Config file

The location of the configuration file is conventional. Configuration via commands is not supported. The specified path is fgen.yaml in the flutter project root directory.

Config schema for vscode

Install YAML Support plugin.

Config your vscode settings.json file.

It can be used to prompt the configuration file.

{
  "yaml.schemas": {
    "https://raw.githubusercontent.com/fluttercandies/flutter_asset_generator/master/fgen_schema.json": ["fgen.yaml"]
  }
}
exclude and include rules

The file is yaml format, every element is glob style.

The name of the excluded file is under the exclude node, and the type is a string array. If no rule is included, it means no file is excluded.

The include node is the name of the file that needs to be imported, and the type is a string array. If it does not contain any rules, all file are allowed.

In terms of priority, exclude is higher than include, in other words:

First import the file according to the include nodes, and then exclude the files.

Replacement Rules

File names can be replaced according to the configuration file as shown below:


replace:
  - from: “
    to: 
  - from: ”
    to: 
  - from: ’
    to:
  - from: (
    to:
  - from: )
    to:
  - from: "!"
    to:
Example
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 [asset_generator](https://github.com/fluttercandies/flutter_asset_generator) library.
/// PLEASE DO NOT EDIT MANUALLY.
class R {
  const 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';
}
Other config

Since version 1.1.0:

Next command config option also support in config file.

But the command line option has a higher priority than the config file.


watch: false
# watch: true

preview: false

output: lib/const/r.dart
# output: lib/const/resource.dart

name: RRR

所屬分類

依賴

依賴清單 6 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。