r_flutter 0.6.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

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. Ensure that your assets and localization files are inside lib directory. This is required for builder plugin to detect changes.

  2. Add dependencies in your pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter

builders:
  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
  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.
  • ignore: specifies a list of files/directories that should be skipped during code generation.
  1. Execute flutter generate command in your project's directory. You could also run tests or just build the app. Compiler must run at least once to generate the file. assets.dart will be generated into .dart_tool/build/generated/YOUR_PACKAGE_NAME/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)

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 #

incompatibility issued with build_runner

At the moment this integration does not work well together with build_runner. In this case the code generation can be executed using the command line tool.

dev_dependencies:
  r_flutter: <version>

Execution:

flutter packages pub run r_flutter:generate

assets.dart not found

Execute flutter generate command in your project's directory. You could also run tests or just build the app. Compiler must run at least once to generate the file. assets.dart will be generated into .dart_tool/build/generated/YOUR_PACKAGE_NAME/assets.dart

news keys not resolvable in IDE #

When assets.dart is regenerated, sometimes it is not correctly indexed by the IDE: Building should run anyway, even though that the IDE shows an error.

If the error constits, check the assets.dart and maybe add a Whitespace somewhere. That will trigger the IDE to re-index.

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)

0.6.0 #

  • add support for script code in i18n files. Thanks to @chen-yumin

0.5.1 #

  • fix locales with country code without support for the base language

0.5.0 #

  • align command line and builder capabilities

0.4.1 #

  • fix crash when none default locales have additional keys

0.4.0 #

  • generate complete i18n code.
    • there is no need for the RuntimeArb anymore
    • see the README for integration steps

0.3.1 #

  • fix NullPointerException when trying to access undefined asset classes

0.3.0 #

  • add custom asset classes support. Thanks to @szotp
  • add file uri comments to assets to get image preview in autocomplete
  • ignore .DS_Store from code generation

0.2.1 #

  • add support to dart builder to automatically integrate into the normal build process. Thanks to @szotp

example/README.md

example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  r_flutter: ^0.6.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:r_flutter/builder.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Jan 16, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/builder.dart. (-0.50 points)

Analysis of lib/builder.dart reported 1 hint:

line 84 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Format bin/generate.dart.

Run dartfmt to format bin/generate.dart.

Format lib/src/generator/i18n/i18n_generator.dart.

Run dartfmt to format lib/src/generator/i18n/i18n_generator.dart.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (recase).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
args >=0.12.1 <2.0.0 1.5.2
build ^1.1.4 1.2.2
build_config ^0.4.0 0.4.1+1
dart_style ^1.0.0 1.3.3
path >=0.9.0 <2.0.0 1.6.4
recase ^2.0.1 2.0.1 3.0.0
yaml ^2.1.15 2.2.0
Transitive dependencies
_fe_analyzer_shared 1.0.3
analyzer 0.39.4
async 2.4.0
charcode 1.1.2
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
json_annotation 3.0.1
logging 0.11.4
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
pedantic 1.9.0
pub_semver 1.4.2
pubspec_parse 0.1.5
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
Dev dependencies
build_test any
lint ^1.0.0
test any