r_resources

此包专为使用 build_runner 进行R文件代码生成而设计。

R 文件包含应用程序资源名称的静态访问,以及通过 BuildContext 对本地化字符串资源的继承访问。

通过这种方法,您将不会在任何资源名称中出现拼写错误。

如何使用

要使用资源代码生成,请在您的 pubspec.yaml 中添加开发依赖项

dev_dependencies:
  build_runner: <build_runner version here>
  r_resources: ^1.0.0

要生成R文件,请运行 build_runner:flutter pub run build_runner buildr.dart 文件将被创建在 lib 文件夹中

纯图像和SVG

为了生成图像资源的R名称,您需要遵循以下步骤

  1. 在项目根文件夹(your_app/assets)中创建 assets 文件夹
  2. 为纯图像文件(如 .png, .jpg 等)创建 images 文件夹。以及/或为 .svg 文件创建 svg 文件夹。
  3. 将图像资源添加到这些文件夹中。
  4. assets 添加到 pubspec.yaml

flutter:
  assets:
    - assets/images/
    - assets/svg/
  1. 运行代码生成

注意:要为不同比例因子添加纯图像,您可以在 images 文件夹内添加缩放文件夹。

your_app:
  assets:
    images:
      2.0x:
        img.png
      3.0x:
        img.png
      img.png

在生成 r.dart 后,您可以按如下方式引用资源

Image.asset(R.images.ic_individual_schools);
SvgPicture.asset(R.svg.ic_filter);

配置文件

r_resources 为代码生成提供了一些配置。

r_options.yaml 配置文件添加到项目根文件夹。例如

path: 'lib/codegen'

generate_strings: true

supported_locales:
  - en_US
  - en_GB
  - ru

fallback_locale: en_US

参数

path – 描述 r.dart 将保存到何处的参数。路径应始终以 lib 文件夹开头。默认为 lib

generate_strings – 启用和禁用字符串资源生成的参数。默认等于 false,因为您可能使用其他本地化字符串生成包。

supported_locales – 描述您的应用程序将支持哪些区域设置的参数。默认等于 en。只有当 generate_stringstrue 时,此参数才有意义,否则将被忽略。

fallback_locale – 描述在缺少翻译时将使用哪个区域设置翻译的参数。默认等于 en。只有当 generate_stringstrue 时,此参数才有意义,否则将被忽略。

字符串

r_resources 提供了一种为您的应用程序生成字符串翻译的简单方法。

此生成默认关闭。

如何使用字符串生成

要开始生成本地化字符串资源,您应该在配置文件中将 generate_strings 参数设置为 true

您可能还需要配置 supported_localesfallback_locale 参数。

代码生成使用的所有区域设置命名都遵循以下格式:<language_code>_<country_code>country_code 可以省略,以便为所有嵌套国家使用通用的语言代码。示例:en_GBruen。请注意,en_GBen 不同。

在配置了 generate_stringssupported_localesfallback_locale 参数后,您应该将翻译文件添加到 your_app/assets/strings

如果您像这样配置了 r_options.yaml

generate_strings: true

supported_locales:
  - en_US
  - en_GB
  - ru

fallback_locale: en_US

然后您应该添加 en_US.jsonen_GB.jsonen_US.json 文件。并且您需要确保 en_US.json 文件包含所有可能的翻译,因为它将用作备用。

翻译文件是单个 json 文件。不同翻译文件中的字段名称应匹配。

en_US.json (备用区域设置)

{
    "label_lorem_ipsum": "Lorem ipsum",
    "label_color": "Color",
    "format_example": "Your object is ${object} and other is ${other}",
    "label_with_newline": "HELLO!\nI'm new line symbol (\\n)"
}

ru.json:

{
    "label_color": "Цвет",
    "format_example": "Ты передал object = ${object}"
}

使用这些文件生成R后,您可以在代码中按如下方式访问本地化值

Text(R.stringsOf(context).label_lorem_ipsum);
Text(R.stringsOf(context).label_color);

成功生成代码后,r.dart 将包含 2 个新类

  1. _Strings 实例可以通过 R.stringsOf(context) 访问,并用作本地化值的容器
  2. RStringsDelegate_Strings 类型的 LocalizationsDelegate

您应该使用 RStringsDelegate 来配置应用程序小部件的本地化

return MaterialApp(
  ...,
  supportedLocales: RStringsDelegate.supportedLocales,
  localizationsDelegates: [
    RStringsDelegate(),
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  ...,
 );

格式化

此包还支持本地化格式化

en_US.json (备用区域设置)

{
    "format_example": "Your object is ${object} and other is ${other}"
}

ru.json:

{
    "format_example": "Ты передал object = ${object}"
}

它看起来像 json 文件中的字符串插值。生成后,您将在 _Strings 类中获得如下函数,而不是 getter

String format_example({
  required Object object,
  required Object other,
});

它可以在代码中按如下方式使用

Text(
  R.stringsOf(context).format_example(
    object: 12345,
    other: 'OTHER',
  ),
),

计划中的功能

☑ 纯图像

☑ SVG

☑ 字符串资源

☐ 颜色

☐ 文本样式

☐ 主题

许可证

根据 MIT 许可 许可。

GitHub

查看 Github