flutter_gen icon indicating copy to clipboard operation
flutter_gen copied to clipboard

Support path method to an asset folder

Open vladimirgoncharov opened this issue 3 years ago • 2 comments

For example I have the next settings:

# pubspec.yaml
flutter:
  assets:
    - assets/images/profile.jpg

And the library generates path to the profile image.

Widget build(BuildContext context) {
  // Assets.images.profile.path = 'assets/images/profile.jpg'
  return Image.asset(Assets.images.profile.path);
}

But sometime I need to write a specific code and have access to the folder assets/images. Could you support the logic?

var imagesPath = Assets.images.path;
// do some magic in the folder

vladimirgoncharov avatar Jan 19 '21 20:01 vladimirgoncharov

Same here. Working on a project (online game) which is split into mobile app and backend API. The mobile app keeps all the image assets, so for the player avatars I have:

...
AssetGenImage get femaleAristocrat =>
      const AssetGenImage('assets/images/players/female_aristocrat.png');
  AssetGenImage get femaleCommander =>
      const AssetGenImage('assets/images/players/female_commander.png');
...

but the API just returns the player avatar as female_aristocrat. Would be great to have some way to get the part assets/images/players/ somehow, without hardcoding it in the app.

So instead of calling:

String get imagePath => 'assets/images/players/$avatar.png';

I could make it like:

String get imagePath => '{${Assets.images.players.path}/$avatar.png';

Not a big deal, but already made a dozen of typos in these kind of paths.

vintage avatar Feb 15 '21 09:02 vintage

Similarly easy_localization only accepts a path, so I'm hardcoding the path. Note you have to declare the translations within assets in pubspec already, so flutter_gen is picking it up regardless - it's just not accessible since only the values for the files: en-us etc are available - but not helpful in this instance.

 EasyLocalization(
    supportedLocales: [Locale('en', 'US'), Locale('de', 'DE')],
    path: 'assets/translations', // <-- change the path of the translation files 
    fallbackLocale: Locale('en', 'US'),
    child: MyApp()
)

DJTB avatar Dec 20 '22 06:12 DJTB