Search code examples
flutterdartflutter-layout

Flutter How put a widget inside an Image


I would like to incorporate a widget inside my image. But the problem is that all the ways that I found dont have a child method so I can't put a widget in there. The last thing that I tried was to use BoxDecoration inside a Container and use child for the widget that I want to display inside. But the size of the container don't match with this image I don't know why. Anyone have an idea ?

my code:

enter image description here


Solution

  • You need to use fit, like this:

    Container(
                height: 200,
                width: 100,
                decoration: BoxDecoration(
                    color: Colors.red,
                    image: DecorationImage(
                      fit: BoxFit.cover,//<--- add this
                      image: AssetImage('assets/images/test.jpeg'),
                    )),
                child: ...
              ),
    

    result before:

    enter image description here

    result after:

    enter image description here