r_flutter

Creator: coderz1093

Last updated:

0 purchases

TODO
Add to Cart

Description:

r flutter

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 #

Add dependencies in your pubspec.yaml:

dependencies:
flutter:
sdk: flutter

dev_dependencies:
build_runner:
r_flutter: <version>
copied to clipboard

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
copied to clipboard
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.



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)
copied to clipboard
Note: if something doesn't work, check the example project.
I18n #

Add default localization file to pubspec.yaml

r_flutter:
intl: lib/i18n/en.arb
copied to clipboard
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).

Add it to your app.

MaterialApp(
title: 'r_flutter',
supportedLocales: I18n.supportedLocales,
localizationsDelegates: [
I18n.delegate
],
home: HomePage(),
)
copied to clipboard

Use it

import 'assets.dart'
Text(I18n.of(context).hello)
copied to clipboard
Organize I18n strings by feature
It is possible to organize i18n strings by feature so that they are not stored in one huge file.

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
copied to clipboard
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)
copied to clipboard
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
copied to clipboard
And then, r_flutter will use SvgFile class for all svg assets:
static const SvgFile svg = SvgFile("lib/assets/svg.svg")
copied to clipboard
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"))
copied to clipboard
you can write:
Image(image: Images.image)
copied to clipboard
Fonts
Instead of writing:
TextStyle(
fontFamily: "Roboto",
)
copied to clipboard
you can write:
TextStyle(
fontFamily: Fonts.roboto,
)
copied to clipboard
Fonts
Instead of writing:
await rootBundle.loadString("assets/path/to/data.json")
copied to clipboard
you can write:
await rootBundle.loadString(Assets.data)
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files:

Customer Reviews

There are no reviews.