vector_graphics icon indicating copy to clipboard operation
vector_graphics copied to clipboard

[Suggestion] Automatically convert and create asset class

Open ikbendewilliam opened this issue 1 year ago • 7 comments

I've played around with the vector graphics and have a suggestion, a simple annotation where you can set all data to convert the svg's into vector files. I've made a working PoC (fork) that does the following:

This package provides a tool to generate vector graphics from svg files and creates a dart file with the vector graphics locations.

For example if you have the following structure:

- assets
  - images
    - subfolder
      - squares.svg
    - flutter.svg
  - different_folder
    - green_rectangle.svg

And the following annotation:

@VectorGraphics(
  svgSources: [
    VectorGraphicsSource(
      input: 'assets/different_folder/green_rectangle.svg',
    ),
    VectorGraphicsSource(
      inputDir: 'assets/images',
    ),
  ],
)
class VectorGraphicsAssets {}

The generator will create .vec files for each svg and create a dart file with the following content:

class VectorGraphicsAssets {
  static const greenRectangle = 'assets/different_folder/green_rectangle.vec';

  static const squares = 'assets/images/subfolder/squares.vec';

  static const flutter = 'assets/images/flutter.vec';
}

I'd love to hear some opinions. The upside is that it is easier to convert, there is automatically a class with all paths and it is updated quite easily. The downside is the use of build_runner, this might not be the best place and the converter command might already be sufficient. 🤔

ikbendewilliam avatar Feb 05 '23 10:02 ikbendewilliam

Neat. I haven't though enough about this yet to know if it's really the right path forward but it seems interesting. We've been talking with the flutter_tools team about having some kind of asset transformation available in the build process itself.

/cc @jonahwilliams @andrewkolos FYI

dnfield avatar Feb 09 '23 18:02 dnfield

One big caveat is that we've intentionally avoided specing anything around generating source code. its really unclear what the path forward there would be, at least until we have more information on macros. I suspect that will only come after Dart 3.

jonahwilliams avatar Feb 09 '23 18:02 jonahwilliams

One big caveat is that we've intentionally avoided specing anything around generating source code. its really unclear what the path forward there would be, at least until we have more information on macros. I suspect that will only come after Dart 3.

I understand, however this might still take some time as they are indeed working hard on Dart 3. I think I'll have another look and discuss with my colleagues if it's worth releasing a package like this (under my account). It makes it easier to adapt to vector_graphics as you can just add the SVG and a class is generated. But will be obsolete or converted when macro's become available or something similar.

ikbendewilliam avatar Feb 09 '23 20:02 ikbendewilliam

If you're interested in maintaining this package I'd be happy to mention it in the readme in this repo. I'd rather keep it separate for now.

dnfield avatar Feb 09 '23 20:02 dnfield

That would be perfect, I'll let you know if we decide something 😄

ikbendewilliam avatar Feb 10 '23 14:02 ikbendewilliam

I'm going to check some stuff and make sure that it only generates bin files that it needs to generate etc. Optimise and add some tests. Then I'll publish it and let you know 👍

ikbendewilliam avatar Feb 13 '23 16:02 ikbendewilliam

You can find the repo here: https://github.com/ikbendewilliam/vector_graphics_generator I'll give you a pub.dev link when I'm ready to publish 😁

ikbendewilliam avatar Mar 06 '23 16:03 ikbendewilliam