Skip to content

Automatically generated constants for all flutter assets.

License

Notifications You must be signed in to change notification settings

Mooibroek/r_flutter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
  runtime_arb:
    git: 
      url: https://github.com/timfreiheit/r_flutter.git
      path: runtime_arb

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. 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(),
)
  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.

  2. Import assets.yaml and start using it:

import 'assets.yaml'
Text(i18n.hello_there)

Note: if something doesn't work, check the example project.

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)

About

Automatically generated constants for all flutter assets.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 94.9%
  • Objective-C 3.4%
  • Java 1.7%