Dart R-file generator for build_runner

Overview

r_resources

pub package style: effective dart Analyzer + Tests License: MIT

This package is made for R-file code generation using build_runner.

R file contains static access to application asset names and inherited access (via BuildContext) to localized String resources.

With this approach you will not be able to make a typo in any resource name.

How to use

To use the resources code generation add dev dependency in your pubspec.yaml:

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

To generate R-file, run build_runner: flutter pub run build_runner build. r.dart file will be created in lib folder

Plain images and SVG

In order to generate R-names for image assets, you need to follow these steps:

  1. Create assets folder in project root folder (your_app/assets)
  2. Create images folder for plain image files (such as .png, .jpg, etc.). And/Or create svg folder for .svg files.
  3. Add image assets to these folders.
  4. Add assets to pubspec.yaml:
flutter:
  assets:
    - assets/images/
    - assets/svg/
  1. Run code generation

Note: to add plain images for different scale factors you may add scaled folders inside images folder.

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

After r.dart is generated, you can reference assets like follows:

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

Configuration file

r_resources provides some configuration to code gen.

Add the r_options.yaml config file to project root folder. Example:

path: 'lib/codegen'

generate_strings: true

supported_locales:
  - en_US
  - en_GB
  - ru

fallback_locale: en_US

Parameters:

path - parameter describing where r.dart would be saved. Path should always start with lib folder. Equals lib by default.

generate_strings - parameter that turns on and off string resources generation. Equals false by default, since you may use other localized strings gen packages.

supported_locales - parameter describing which locales will be supported by your app. Equals en by default. It is only makes sence to add this parameter when generate_strings is true otherwise it will be ignored.

fallback_locale - parameter describing which locale translations will be used in case of missing translations. Equals en by default. It is only makes sence to add this parameter when generate_strings is true otherwise it will be ignored.

Strings

r_resources provides a simple way to generate string translations to your app.

This generation is turned off dy default.

How to use Strings gen

To start generating localized strings resources you should set generate_strings parameter to true in configuration file.

Also you may need to configure supported_locales and fallback_locale parameters.

All locale naming used by codegen is using the following format: <language_code>_<country_code>. country_code may be ommited to use generic language code for all nested countries. Examples: en_GB, ru, en. Note that en_GB is not the same as en.

After you configured generate_strings, supported_locales and fallback_locale parameters, you should add translation files to your_app/assets/strings.

In case you specified r_options.yaml like this:

generate_strings: true

supported_locales:
  - en_US
  - en_GB
  - ru

fallback_locale: en_US

then you should add en_US.json, en_GB.json and en_US.json files. And you need to make sure that en_US.json file contains all possible translations, since it will be used as a fallback.

Translations files are single json-s. Field names in different tranlstion files should match.

en_US.json (fallback locale):

{
    "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}"
}

With these files have been used to generate R, you can access localized values in code as follows:

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

After successfull code generation r.dart will have 2 new classes:

  1. _Strings which instance can be accesed by R.stringsOf(context) and used as localized values container
  2. RStringsDelegate - the LocalizationsDelegate for type _Strings

You should use RStringsDelegate to configure localizations of your app widget:

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

Formatting

This package is also supports localized formatting:

en_US.json (fallback locale):

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

ru.json:

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

It looks like string interpolation in json files. After generation you will have the following function instead of getter in _Strings class:

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

It can be used in code as follows:

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

Planned features

Plain images

SVG

String resources

☐ Colors

☐ Text styles

☐ Themes

License

Licensed under the MIT License.

You might also like...

Destiny is a new, mock-data generator for Dart/Flutter

Destiny is a new, mock-data generator for Dart/Flutter. It uses static methods couched in a destiny namespace as the API.

Sep 16, 2022

Starter project and code generator for Flutter/Redux

Starter project and code generator for Flutter/Redux

Flutter Redux Starter/Code Generator Videos Short video ~ 1 minute Long video ~ 10 minutes We're using this approach to develop the Flutter app for In

Dec 12, 2022

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs. Inspired by SwiftGen. Motivation Using asset path str

Jan 6, 2023

GPT-3 recipe generator for the GPT-3 Makeathon by TUM.AI. Developed by team Taste the data.

GPT-3 recipe generator for the GPT-3 Makeathon by TUM.AI. Developed by team Taste the data.

GPT-3 Makeathon by TUM.AI - Team: Taste the Data Team - Taste the Data: Carmen Heger @stedomedo David Stiftl @stiftlD Christopher Schütz @cdschtz

Dec 4, 2022

A generator to create config class from json files that support many environments

A generator to create config class from json files that support many environments. Motivation If you use a json file to config your applications, perp

Oct 9, 2021

A Flutter curl-command generator for Dio

A Flutter curl-command generator for Dio

curl_logger_dio_interceptor A Flutter curl-command generator for Dio. Easily test your Flutter-made requests in your favorite terminal or even in Post

Nov 17, 2022

Flutter Word generator

wordpair_generator A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you start

Dec 3, 2021

Provide route generator to create route map quickly by annotations.

ff_annotation_route Languages: English | 中文简体 Description Provide a route generator to create route map quickly by annotations. ff_annotation_route De

Nov 25, 2022
Owner
Ivanov Nikita
Flutter (ex. Android and IOS) developer
Ivanov Nikita
A very basic prototype of macros using build_runner

Description This is a basic prototype for 3 phase macros using package:build. The general idea is that macros run in 3 different phases, and each phas

Jacob MacDonald 64 Dec 14, 2022
Cache json map to local file with Dart:io. Read file with sync api.

local_cache_sync 一个非常简单易用的Flutter本地储存库,适用于在本地储存一列轻量数据(例如用户保存在本地的设备信息,或者缓存一系列用户信息)。 local_cache_sync的所有方法都是同步,而不是异步的。这意味着你不需要使用await就可以获取数据。在flutter中,这

null 16 Jun 24, 2022
VS Code `.code-workspace` file generator

VS Code .code-workspace file generator (for monorepositories with Dart and Flutter projects) TL;DR; Create yaml file config.yaml (check #Format sectio

Mike T 1 Feb 18, 2022
Environment specific config generator for Dart and Flutter applications during CI/CD builds

Environment Config Generator Environment specific config generator. Allows to specify env configuration during CI/CD build. Primarily created to simpl

Denis Beketsky 86 Dec 2, 2022
🚀The Flutter dart code generator from zeplin. ex) Container, Text, Color, TextStyle, ... - Save your time.

Flutter Gen Zeplin Extension ?? The Flutter dart code generator from zeplin. ex) Container, Text, Color, TextStyle, ... - Save your time. ⬇ 1.1k Getti

NAVER 49 Oct 12, 2022
Dart Code Generator for generating mapper classes

Smartstruct - Dart bean mappings - the easy nullsafe way! Code generator for generating type-safe mappers in dart, inspired by https://mapstruct.org/

Nils 28 Nov 29, 2022
The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. There is no way to get the package version from the code in the Dart ecosystem. Installation Add bu

Daichi Furiya 12 Dec 14, 2022
OpenAPI generator for Dart & Flutter

Fantom Fantom is a cli tool for generating API layer based on OpenAPI Spec. Usage Install fantom $ dart pub global activate fantom Generate API client

6thSolution 13 Oct 18, 2022
OpenAPI generator for Dart & Flutter

Fantom Fantom is a cli tool for generating API layer based on OpenAPI Spec. Usage Install fantom $ dart pub global activate fantom Generate API client

REKAB 13 Oct 18, 2022
Arissounddart - a Command-line SoundSprite generator for Dart

SoundDart SoundDart is a Command-line SoundSprite generator for Dart. It require

Behruz Hurramov 1 Jan 9, 2022