FlutterGen / flutter_gen

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
https://pub.dev/packages/flutter_gen
MIT License
1.44k stars 142 forks source link

how to get only String for image #195

Closed samithe7 closed 1 year ago

samithe7 commented 2 years ago

now i get

   static const AssetGenImage imagesChip1 = 
       AssetGenImage('assets/images/chip1.jpg'); 

i want this

 static const String imagesChip1 = 
       'assets/images/chip1.jpg'; 

if image path string is AssetGenImage i can't make the widget const

Abhishek01039 commented 2 years ago

Hello @samithe7 you can try like this Assets.images.imagesChip1.path

samithe7 commented 2 years ago

thanks but i want make the widget const . if i use Assets.images.imagesChip1.path widget don;t be const .

lcdsmao commented 2 years ago

We should consider adding an option to toggle this just like svg_intergation. Currently, how about making your widget path nullable:

class MyWidget extends StatlessWidget {
  const MyWidget(this.path);

  final String? path;

  Widget build(BuildContenxt context) {
    final effectivePath = path ?? 'default_path';
    return ...;
  }
}

This pattern is also common in flutter.

samithe7 commented 2 years ago
now i get  assets.gen.dart file

   static const AssetGenImage imagesChip1 = 
       AssetGenImage('assets/images/chip1.jpg'); 
Is there any way  in assets.gen.dart file only get String    like this

 static const String imagesChip1 = 
       'assets/images/chip1.jpg';
Hwan-seok commented 2 years ago

@lcdsmao I think the main goal of @samithe7 (also me) is making the Image widget as const. How about providing an option to generate assets separately?

e.g) TO-BE


class AssetImages {
  static const avatar = AssetGenImage('assets/images/avatar.png');
  ...
}
class AssetFonts{
  ...
}

AS-IS

class Assets {
  Assets._();

  static const $AssetsFontsGen fonts = $AssetsFontsGen();
  static const $AssetsImagesGen images = $AssetsImagesGen();
}
lcdsmao commented 2 years ago

@Hwan-seok You can generate const via setting the flutter_gen.assets.style. https://github.com/FlutterGen/flutter_gen/issues/185#issuecomment-1006183927

Hwan-seok commented 2 years ago

Yes, I am aware of that. But the whole assets are in the one class named Assets and the subcategory image is in the property name. The reason why we are using . separated style is that it divides the asset categories greatly. It will be great if they are separated as follows.

AssetImages.chip1
AssetJsons.accounts
AssetLotties.fire