Generate constants for resources which require using them as a String like fonts and assets. Generated file will look like this: assets.dart
-
Ensure that your assets and localization files are inside lib directory. This is required for builder plugin to detect changes.
-
Add dependencies in your pubspec.yaml:
dependencies:
flutter:
sdk: flutter
runtime_arb:
git:
url: https://github.com/timfreiheit/r_flutter.git
path: runtime_arb
builders:
r_flutter: <version>
- 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.
- Import
runtime_arb
package and add RuntimeArbDelegate to your localization delegates:
MaterialApp(
title: 'r_flutter',
localizationsDelegates: [
// runtimeArbDelegate will expect lib/i18n/en.arb and lib/i18n/en.arb to exist in your app
// make sure they have been added to your assets
RuntimeArbDelegate({'en', 'pl'})
],
home: HomePage(),
)
-
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. -
Import
assets.yaml
and start using it:
import 'assets.yaml'
Text(i18n.hello_there)
Note: if something doesn't work, check the example project.
Instead of writing:
Image(image: AssetImage("assets/path/to/image.png"))
you can write:
Image(image: Images.image)
Instead of writing:
TextStyle(
fontFamily: "Roboto",
)
you can write:
TextStyle(
fontFamily: Fonts.roboto,
)
Instead of writing:
await rootBundle.loadString("assets/path/to/data.json")
you can write:
await rootBundle.loadString(Assets.data)