パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。