flutter_gen icon indicating copy to clipboard operation
flutter_gen copied to clipboard

Include package in AssetGenImage path property

Open kolotum opened this issue 3 years ago • 7 comments

Similiar to AssetImage's keyName property it would be helpful to change AssetGenImage's path property to include package so we can hide the package name inside of AssetGenImage:

-  String get path => _assetName;
+  String get path =>
+      _package == null ? _assetName : 'packages/$_package/$_assetName';

_package could be generated, e.g. final String? _package = 'example_resources';

without package_parameter_enabled: true: final String? _package;

SvgGenImage could be changed accordingly.

kolotum avatar May 31 '22 23:05 kolotum

Can you tell me your use case here?

so we can hide the package name inside of AssetGenImage

What's the benefit of this?

lcdsmao avatar Jun 01 '22 01:06 lcdsmao

What's the benefit of this?

The reason is to encapsulate the package name so we can use the AssetGenImage without parameter, e.g. use Image.asset(res.Assets.images.flutter3.path) instead of Image.asset(res.Assets.images.flutter3.path, package: 'example_resources') (Just an example, we would use res.Assets.images.flutter3.image() instead of Image.asset(...) ).

In other cases there is no package parameter available. One use case is to load the data of the AssetGenImage directly and convert it into ui.Image which is used in our particle system, e.g.:

final imageData = await rootBundle.load(res.Assets.images.flutter3.path);
final uiImage = await decodeImageFromList(Uint8List.view(imageData.buffer));

kolotum avatar Jun 02 '22 08:06 kolotum

On the Flame engine we don't use widgets. Having a generated path with the package name would be helpful. It was possible to get that before #229 (via .keyName property that AssetImage has).

renancaraujo avatar Jun 03 '22 15:06 renancaraujo

@kolotum @renancaraujo

When package_parameter_enabled is true, Can you solve this problem by generating the following?

String get path => _assetName;

// THIS
String get keyName => 'packages/$packageName/\$_assetName'; // 'packages/example_resources/assets/images/flutter3.jpg'

or do you need always it? when package_parameter_enabled is false.

wasabeef avatar Jun 08 '22 07:06 wasabeef

@kolotum @renancaraujo

When package_parameter_enabled is true, Can you solve this problem by generating the following?

String get path => _assetName;

// THIS
String get keyName => 'packages/$packageName/\$_assetName'; // 'packages/example_resources/assets/images/flutter3.jpg'

or do you need always it? when package_parameter_enabled is false.

If you would like to keep the path value to not break the current implementations it is okay to introduce a new getter keyName.

I would still prefer a property where I do not need to now the value of package_parameter_enabled.

So I think of something like

String get keyName =>  _package == null ? _assetName : 'packages/$packageName/$_assetName';

or

// when package_parameter_enabled is false
String get keyName => _assetName;

// when package_parameter_enabled is true:
String get keyName =>  'packages/$packageName/$_assetName';

kolotum avatar Jun 08 '22 08:06 kolotum

@kolotum Thank you for your response. I'd like to keep the path variable, so I will do the following (a bit redundant).

When package_parameter_enabled = true

String get path => _assetName;

String get keyName => 'packages/$packageName/\$_assetName';

When package_parameter_enabled = false

String get path => _assetName;

String get keyName => _assetName;

wasabeef avatar Jun 08 '22 08:06 wasabeef

@kolotum @renancaraujo I just released v4.3.0, Thank you all.

wasabeef avatar Jun 09 '22 08:06 wasabeef