DecorationImage doesn't show the image - Flutter

16,851

You need to give width and height to your Container, like this

new Container(
  height: 100,
  width: 100,
  decoration: new BoxDecoration(
    image: new DecorationImage(
      image: new AssetImage('assets\\test.png'),
      fit: BoxFit.cover,
    ),
  ),
),
Share:
16,851

Related videos on Youtube

Aegletes
Author by

Aegletes

Updated on June 04, 2022

Comments

  • Aegletes
    Aegletes over 1 year

    I couldn't find anything related to this, so I guess I am doing something very wrong. I am trying to display a DecorationImage inside BoxDecoration, but nothing shows on my screen at all.

    I try to show the related asset with Image.asset('assets\\test.png'); and that works with no problem. I have tried to put things like AssetImage or FileImage inside DecorationImage, but it seems like none of them work for me.

    My code is basically as below:

        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
        body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Container(
                  decoration: new BoxDecoration(
                    image: new DecorationImage(
                      image: new AssetImage('assets\\test.png'),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ],
            ),
          )
       );
    

    What should I do for my test.png to show? Currently I just see an empty screen.

  • Aegletes
    Aegletes over 4 years
    Thanks a lot! I have been trying it with DecoratedBox and apparently I can't set height and width to it.
  • isarojdahal
    isarojdahal over 2 years
    Awesome. In my case, I have specified only the height for the container.