搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

暂无相关数据。