r_flutter
文字列として使用する必要があるリソース(フォントやアセットなど)用の定数を生成します。
すべてのFlutterアセット用に自動生成された定数。
^2.0.0>=0.9.0 <2.0.0^3.1.0>=4.0.0 < 5.0.0>=2.0.0 <3.0.0>=1.0.0 <2.0.0^1.15.0^2.0.0any^2.0.0^1.0.0以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。
Generate constants for resources which require using them as a String like fonts and assets. Generated file will look like this: assets.dart
dependencies:
flutter:
sdk: flutter
dev_dependencies:
build_runner:
r_flutter: <version>
# 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.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
Import assets.dart and start using it:
import 'assets.dart'
Image(image: Images.image)
Note: if something doesn't work, check the example project.
pubspec.yamlr_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).
MaterialApp(
title: 'r_flutter',
supportedLocales: I18n.supportedLocales,
localizationsDelegates: [
I18n.delegate
],
home: HomePage(),
)
import 'assets.dart'
Text(I18n.of(context).hello)
It is possible to organize i18n strings by feature so that they are not stored in one huge file.
pubspec.yamlr_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.
Create the actual translation files for the features and run the generator as usual.
Use it
import 'assets.dart'
Text(I18n.of(context).home.hello)
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")
The iOS project need to be updated: Documentation
Instead of writing:
Image(image: AssetImage("assets/path/to/image.png"))
you can write:
Image(image: Images.image)
Instead of writing:
TextStyle(
fontFamily: "Roboto",
)
you can write:
TextStyle(
fontFamily: Fonts.roboto,
)
Instead of writing:
await rootBundle.loadString("assets/path/to/data.json")
you can write:
await rootBundle.loadString(Assets.data)