4
votes

I use in my flutter project images with different resolutions as its described here. Now I want to add custom images for dark mode theme. My assets declaration in pubspec.yaml look like this:

flutter:
  uses-material-design: true
  assets:
    - assets/images/img_menu.png
    - ...

My assets structure:

  .../pubspec.yaml
  .../assets/images/img_menu.png
  .../assets/images/0.75x/img_menu.png
  .../assets/images/1.5x/img_menu.png
  .../assets/images/2.0x/img_menu.png
  .../assets/images/3.0x/img_menu.png
  .../assets/images/4.0x/img_menu.png

I load images in project this way:

Image.asset('assets/images/img_menu.png')

I know that for simple one color images I can change its color like that:

Image.asset('assets/images/img_menu.png', 
                      color: Theme.of(context).brightness == Brightness.light
                      ? Colors.black
                      : Colors.white,)

but most of my images are more complex, so I have to load custom assets for them.

As it is described here I can leave pubspec without any changes after adding dark mode assets. I've tried different assets structures for dark mode, but none of them works.

1:

  .../pubspec.yaml
  .../assets/images/img_menu.png
  .../assets/images/dark/img_menu.png
  .../assets/images/0.75x/img_menu.png
  .../assets/images/0.75x/dark/img_menu.png
  .../assets/images/1.5x/img_menu.png
  .../assets/images/1.5x/dark/img_menu.png
  .../assets/images/2.0x/img_menu.png
  .../assets/images/2.0x/dark/img_menu.png
  .../assets/images/3.0x/img_menu.png
  .../assets/images/3.0x/dark/img_menu.png
  .../assets/images/4.0x/img_menu.png
  .../assets/images/4.0x/dark/img_menu.png

2:

  .../pubspec.yaml
  .../assets/images/img_menu.png
  .../assets/images/0.75x/img_menu.png
  .../assets/images/1.5x/img_menu.png
  .../assets/images/2.0x/img_menu.png
  .../assets/images/3.0x/img_menu.png
  .../assets/images/4.0x/img_menu.png
  .../assets/images/dark/img_menu.png
  .../assets/images/dark/0.75x/img_menu.png
  .../assets/images/dark/1.5x/img_menu.png
  .../assets/images/dark/2.0x/img_menu.png
  .../assets/images/dark/3.0x/img_menu.png
  .../assets/images/dark/4.0x/img_menu.png

3:

  .../pubspec.yaml
  .../assets/images/img_menu.png
  .../assets/images/0.75x/img_menu.png
  .../assets/images/1.5x/img_menu.png
  .../assets/images/2.0x/img_menu.png
  .../assets/images/3.0x/img_menu.png
  .../assets/images/4.0x/img_menu.png
  .../assets/dark/images/img_menu.png
  .../assets/dark/images/0.75x/img_menu.png
  .../assets/dark/images/1.5x/img_menu.png
  .../assets/dark/images/2.0x/img_menu.png
  .../assets/dark/images/3.0x/img_menu.png
  .../assets/dark/images/4.0x/img_menu.png

Solution for single image is to load it like that:

Image.asset(Theme.of(context).brightness == Brightness.light 
                      ? 'assets/images/img_menu.png'
                      : 'assets/images/dark/img_menu.png',)

but this is not a convenient solution for the entire project.

What's stranger, in the 2 variant app loads dark images for both: light and dark theme (tested on iPhone and Android).

So the question is: What should the file structure for dark mode and resolution-aware image assets look like?

1

1 Answers

1
votes

seem so far, the only supported variant currently is for scale (device pixel ratio), not dark mode.

See: https://github.com/flutter/flutter/issues/44482