Pakete, Repositories und App-Kategorien suchen

Paketdetailsv0.9.0

r_flutter

Erstellen Sie Konstanten für Ressourcen, die als Zeichenkette verwendet werden müssen, wie Schriftarten und Assets.

AndroidiOSmacOSWindowsLinux
30-Tage-Downloads
352
Likes
19
Pub-Punkte
130

Paket installieren

flutter pub add r_flutter

Paketübersicht

Aus öffentlichen Paketdaten erstellte Orientierung.

r_flutter ist ein Flutter-Paket, das Kompilierzeitkonstanten für Ressourcen wie Assets, Schriften und Bilder erzeugt, wodurch sichere und effiziente Zugriffe über Zeichenkettenreferenzen ermöglichen. Durch die Erzeugung einer speziellen Klasse (z. B. R) mit statischen Eigenschaften für jede Ressource wird das Risiko von Tippfehlern beseitigt und die Wartbarkeit des Codes verbessert. Die generierten Konstanten sind typensicher und leistungsoptimiert und eignen sich ideal für Anwendungen in großem Maßstab. Es unterstützt alle wichtigen Plattformen einschließlich Android, iOS, macOS, Windows und Linux.

Anwendungsfälle

  • Sichere Asset-Referenzen generieren
  • Vermeidung von String-Tippfehlern in Ressourcenpfaden
  • Leistung durch Kompilierzeit-Konstanten verbessern
  • Effizientes Management von Schriftarten und Bildern
  • Einfachere Ressourcenzugriffe über Plattformen hinweg

Hauptmerkmale

  • Generiert Ressourcenkonstanten automatisch
  • Typsichere Zugriffe auf Ressourcen und Schriftarten
  • Kompilierzeitüberprüfung
  • Unterstützt mehrere Plattformen
  • Minimale Laufzeitüberhead

Geeignet für

  • Große Flutter-Projekte
  • Teams, die Code-Sicherheit priorisieren
  • Apps mit umfangreichem Ressourcenausnutzung
  • Entwickler, die Leistungssteigerung suchen
  • Projekte, die eine konsistente Ressourcennamensgebung erfordern

Hinweise

  • Erfordert Schritt zur Codegenerierung
  • Zur Konfiguration erforderliche Initialisierung
  • Kann die Build-Zeit leicht erhöhen
  • Nicht geeignet für dynamisch geladene Ressourcen
  • Beschränkt auf statische Ressourcendefinitionen

Original-README

Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.

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. Add dependencies in your pubspec.yaml:
dependencies:
  flutter:
    sdk: flutter

dev_dependencies:
  build_runner:
  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
  add_file_path_comments: false # default is true
  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.
  • add_file_path_comments: specifies wither it should print file paths comments for assets files.
  • ignore: specifies a list of files/directories that should be skipped during code generation.
  1. 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

  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)
Organize I18n strings by feature

It is possible to organize i18n strings by feature so that they are not stored in one huge file.

  1. 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

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.

  1. Create the actual translation files for the features and run the generator as usual.

  2. Use it

import 'assets.dart'
Text(I18n.of(context).home.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

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)

Kategorien

Abhängigkeiten

Abhängigkeiten 8 Einträge

Quell-Repository

Projekte, die dieses Paket verwenden

Noch keine Daten verfügbar.