Search code examples
flutterassetsflutter-assetimage

How to organize the images in the assets folder such in a way if we refactor developers dont need to change it every where


Guess I have a flutter project with assets in the following folder structure

assets -> 
images ->
 img1.png
 img2.png ...

So assume I am using this img2.png in multiple files in my flutter project, and after some time I change the assets folder structure as follows since my assets folder gets overloaded with images. So the new asset folder structure would be like,

assets ->
 images ->
  login ->
   img1.png
  signin ->
   img2.png
   ...

So in the above scenario I have changed the image folder structure based on a feature. If so, since I have used the img2.png image file in multiple files, I have to change it in all the files which is really a burden. Is there any other alternative or possibly an easier way to reflect the image path changes based on the folder structure we have redefined in the assets folder?

Thanks in advance.


Solution

  • You could create a static class for your app assets, so that you'll only have to change the file path in the class

    For Example

    class AppAssets{
    static const loginImg1 = "assets/images/login/img1.png";
    static const loginImg2 = "assets/images/login/img2.png";
    } 
    
    // Then access it in your widgets like
    
    Image.asset(AppAssets.loginImg1)