4

I am trying to place an image in the center of a box (container with border). The image size is set by surrounding it with a sized box, the the border or box is being created by surrounding that with a container with box decoration like this:

            InkWell(
              child: Container(
                  decoration: BoxDecoration(border: Border.all()),
                  height: 50,
                  width: 70,
                  child: SizedBox(
                      height: 10,
                      width: 10,
                      child: Image.asset('assets/store_physical.png',
                          fit: BoxFit.cover)),
              ),
            ),

The problem is that the image asset it ignoring the dimensions of the sized box and taking the size from the surrounding container making the image too big.

I am not sure why this is happening unless it gets it size from the top of the widget tree which doesn't seem to make sense.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89

3 Answers3

1

I also had the same problem. You can try adding the Image.asset inside another container and then change the size of that container accordingly.

InkWell(
  child: Container(
      decoration: BoxDecoration(border: Border.all()),
      height: 50,
      width: 70,
      child: SizedBox(
          height: 10,
          width: 10,
          child: Container(
                   height: 40.0,
                   width: 40.0,
                   child: Image.asset(
                      'assets/store_physical.png',
                      fit: BoxFit.cover
                    )
              )
      ),
  ),
)
Can
  • 76
  • 10
Ojasv Singh
  • 41
  • 1
  • 6
1

Remove width and height from Container and SizedBox, instead provide it in Image.asset()

Container(
  decoration: BoxDecoration(border: Border.all(color: Colors.blue, width: 5)),
  child: Image.asset(
    'assets/store_physical.png',
    fit: BoxFit.cover,
    height: 50, // set your height
    width: 70, // and width here
  ),
)
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • 2
    Unfortunately that doesn't work. I still need the container for the border. I tried removing the sized box and giving the dimensions just to the image but is still inheriting from the container. – Nicholas Muir Jul 21 '19 at 05:19
  • 1
    Please check out the updated answer. You can now give border too. – CopsOnRoad Jul 21 '19 at 06:36
1

When the child container is smaller than the parent, the parent doesn't know where to place it, so it forces it to have the same size. If you include the parameter alignment in the parent container, it will respect the size of the child Container:

InkWell(
      child: Container(
          alignment: Alignment.topLeft, //Set it to your specific need
          decoration: BoxDecoration(border: Border.all()),
          height: 50,
          width: 70,
          child: SizedBox(
              height: 10,
              width: 10,
              child: Image.asset('assets/store_physical.png',
                  fit: BoxFit.cover)),
      ),
    ),
JAgüero
  • 403
  • 1
  • 4
  • 14