skip to Main Content

How can I manage image paths for different categories, like logos and backgrounds, in Dart? I tried using nested classes like in Java, but Dart doesn’t support that! What are the best practices or alternatives to handle this?


void main() {
  print(Images.background.lake); // it dosn't work!
}

class Images {
  class Portrait {
    final traditional = 'assets/images/portrait/traditional.png';
    final environmental = 'assets/images/portrait/environmental.png';
  }

  class Background {
    final nature = 'assets/images/background/nature.png';
    final lake = 'assets/images/background/lake.png';
  }
}


2

Answers


  1. Solution first:

    void main() {
      print(Images.background.lake); // This will work!
    }
    
    class Images {
      static final Portrait portrait = Portrait();
      static final Background background = Background();
    }
    
    class Portrait {
      final String traditional = 'assets/images/portrait/traditional.png';
      final String environmental = 'assets/images/portrait/environmental.png';
    }
    
    class Background {
      final String nature = 'assets/images/background/nature.png';
      final String lake = 'assets/images/background/lake.png';
    }
    
    
    

    Solution 2:

    void main() {
      print(Images.Portrait.traditional); // Access using static references
    }
    
    class Images {
      static const Portrait Portrait = _Portrait();
      static const Background Background = _Background();
    }
    
    class _Portrait {
      static const String traditional = 'assets/images/portrait/traditional.png';
      static const String environmental = 'assets/images/portrait/environmental.png';
    }
    
    class _Background {
      static const String nature = 'assets/images/background/nature.png';
      static const String lake = 'assets/images/background/lake.png';
    }
    
    
    
    Login or Signup to reply.
  2. The Images class contains nested classes _Portrait and _Background, organizing image paths for easy access and allowing for additional nested classes as needed.

    class Images {
      // Nested classes for image-specific path
    
      // portrait images
      static const portrait = _Portrait();
    
      // background images
      static const background = _Background();
    }
    
    class _Portrait {
      const _Portrait();
    
      // portrait directory path
      static const String _portraitDirPath = 'assets/images/portrait/';
    
      final String traditional = '${_portraitDirPath}traditional.png';
      final String environmental = '${_portraitDirPath}environmental.png';
    }
    
    class _Background {
      const _Background();
    
      // background directory path
      static const String _backgroundDirPath = 'assets/images/background/';
    
      final String nature = '${_backgroundDirPath}nature.png';
      final String lake = '${_backgroundDirPath}lake.png';
    }
    

    Then, you can access each image path through their chained member access (dotted notation), like:

    void main() {
      print(Images.portrait.traditional);
      print(Images.background.nature);
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search