패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.9.0

r_flutter

글꼴 및 자산과 같이 문자열로 사용해야 하는 리소스에 대한 상수를 생성합니다.

AndroidiOSmacOSWindowsLinux
30일 다운로드
352
좋아요
19
Pub 점수
130

패키지 설치

flutter pub add r_flutter

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

r_flutter는 자산, 폰트, 이미지와 같은 리소스에 대해 컴파일 시 상수를 생성하는 Flutter 패키지로, 문자열 참조를 사용하여 안전하고 효율적으로 액세스할 수 있게 해줍니다. 각 리소스에 대한 전용 클래스(예: R)를 정적 속성과 함께 생성함으로써 오타의 위험을 제거하고 코드 유지보수성을 향상시킵니다. 생성된 상수는 타입 안전하고 성능 최적화되어 있어 대규모 애플리케이션에 이상적입니다. Android, iOS, macOS, Windows 및 Linux를 포함한 모든 주요 플랫폼을 지원합니다.

사용 사례

  • 안전한 자산 참조 생성
  • 리소스 경로에서 문자열 오타 방지
  • 컴파일 시 상수를 사용한 성능 향상
  • 글꼴과 이미지를 효율적으로 관리하기
  • 다양한 플랫폼 간 리소스 접근을 단순화

주요 기능

  • 리소스 상수를 자동으로 생성합니다
  • 자산과 폰트에 대한 타입 안전한 접근
  • 컴파일 시 검증
  • 여러 플랫폼을 지원합니다
  • 최소의 런타임 오버헤드

적합한 대상

  • 대규모 Flutter 프로젝트
  • 코드 안정성을 우선시하는 팀
  • 광범위한 자산 사용을 하는 앱
  • 성능 최적화를 원하는 개발자
  • 일관된 리소스 이름 지정이 필요한 프로젝트

고려 사항

  • 코드 생성 단계가 필요합니다
  • 구성에 필요한 초기 설정
  • 빌드 시간을 약간 증가시킬 수 있습니다
  • 동적으로 로드된 리소스에 적합하지 않습니다
  • 정적 리소스 정의에 한정

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

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)

카테고리

의존성

의존성 목록 8 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.