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

套件詳情v0.9.0

r_flutter

為需要以字串形式使用之資源(例如字型與資源)產生常數。

AndroidiOSmacOSWindowsLinux
近 30 天下載
352
喜歡數
19
Pub 分數
130

安裝套件

flutter pub add r_flutter

套件介紹

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

r_flutter 是一個 Flutter 套件,會為資源(例如資產、字型和影像)產生編譯時期常數,讓您能使用字串參考安全且高效地存取這些資源。透過為每個資源產生專用類別(例如 R),並具有靜態屬性,可消除拼字錯誤的風險,並提升程式碼可維護性。產生的常數是類型安全且效能優化的,非常適合大型應用程式。它支援所有主要平台,包含 Android、iOS、macOS、Windows 與 Linux。

適用情境

  • 產生安全的資產引用
  • 避免資源路徑中的字串拼寫錯誤
  • 使用編譯時常數提升效能
  • 高效管理字體和圖像
  • 簡化跨平台的資源存取

主要特色

  • 自動產生資源常數
  • 對資源和字型的安全類型存取
  • 編譯時驗證
  • 支援多個平台
  • 最小的執行時開銷

適合對象

  • 大型 Flutter 項目
  • 重視程式碼安全的團隊
  • 資產使用量較大的應用
  • 尋求效能優化的開發者
  • 需要一致資源命名的專案

使用建議

  • 需要程式碼產生步驟
  • 設定所需的初始設定
  • 可能會稍微增加建構時間
  • 不適用於動態載入的資源
  • 僅限靜態資源定義

原始 README

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

r_flutter

Generate constants for resources which require using them as a String like fonts and assets. Generated file will look like this: assets.dart

Setup

  1. Add dependencies in your pubspec.yaml:
dependencies:
  flutter:
    sdk: flutter

dev_dependencies:
  build_runner:
  r_flutter: <version>
  1. Add r_flutter configuration in your pubspec.yaml:
# important: this is root level option
r_flutter:
  intl: lib/i18n/en.arb
  add_file_path_comments: false # default is true
  ignore:
    - lib/assets/sub/ignore1 #use ignore option to skip 
    - lib/assets/sub/ignore2
    - lib/i18n

Options:

  • intl: Points to a localization file that would be used to generate localization keys. arb files are essentialy json files with some special, optional keys. Specifing this is optional.
  • add_file_path_comments: specifies wither it should print file paths comments for assets files.
  • ignore: specifies a list of files/directories that should be skipped during code generation.
  1. Execute flutter packages pub run build_runner build command in your project's directory. Alternativly you can run flutter pub run r_flutter:generate to only run r_flutter without using the whole build_runner. assets.dart will be generated into lib/assets.dart

  2. Import assets.dart and start using it:

import 'assets.dart'
Image(image: Images.image)

Note: if something doesn't work, check the example project.

I18n
  1. Add default localization file to pubspec.yaml
r_flutter:
  intl: lib/i18n/en.arb

Other locales will be searched under the same folder as the default localization file (e.g. lib/i18n/) for the following 4 formats:

  • <language_code>.arb (e.g.: en.arb, zh.arb)
  • <language_code>_<country_code>.arb (e.g.: en_US.arb, en_GB.arb)
  • <language_code>_<script_code>.arb (e.g.: zh_Hans.arb, zh_Hant.arb)
  • <language_code>_<script_code>_<country_code>.arb (e.g.: zh_Hans_CN.arb, zh_Hant_TW.arb, zh_Hant_HK.arb)

Where <language_code> consists of 2 lowercase letters (ISO 639-1); <country_code> consists of 2 uppercase letters (ISO_3166-2); <script_code> consists of 4 letters with the first letter being capitalized (ISO 15924).

  1. Add it to your app.
MaterialApp(
  title: 'r_flutter',
  supportedLocales: I18n.supportedLocales,
  localizationsDelegates: [
    I18n.delegate
  ],
  home: HomePage(),
)
  1. Use it
import 'assets.dart'
Text(I18n.of(context).hello)
Organize I18n strings by feature

It is possible to organize i18n strings by feature so that they are not stored in one huge file.

  1. Add default localization file and feature definitions to pubspec.yaml
r_flutter:
  intl: lib/i18n/en.arb
  intl_features:
    - name: home
      path: lib/custom/path/to/home/
    - name: announcement
    - name: profile

If a path is not specified it is assumed to be a subdirectory of the main intl file directory. In this example the announcement and profile translation files will be placed under lib/i18n/announcement/ and lib/i18n/profile/ respectively.

  1. Create the actual translation files for the features and run the generator as usual.

  2. Use it

import 'assets.dart'
Text(I18n.of(context).home.hello)
Custom asset classes

r_flutter supports third party packages like flutter_svg by providing option to convert generated constants directly into the desired class. To use it, you need to configure which file extension should by handled by which class, for example:

r_flutter:
  asset_classes:
    ".svg": 
      import: asset_classes.dart
      class: SvgFile

And then, r_flutter will use SvgFile class for all svg assets:

static const SvgFile svg = SvgFile("lib/assets/svg.svg")

Troubleshooting

iOS won't show the correct language

The iOS project need to be updated: Documentation

Examples

Images

Instead of writing:

Image(image: AssetImage("assets/path/to/image.png"))

you can write:

Image(image: Images.image)
Fonts

Instead of writing:

TextStyle(
    fontFamily: "Roboto",
)

you can write:

TextStyle(
    fontFamily: Fonts.roboto,
)
Fonts

Instead of writing:

await rootBundle.loadString("assets/path/to/data.json")

you can write:

await rootBundle.loadString(Assets.data)

所屬分類

依賴

依賴清單 8 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。