Expanded in SingleChildScrollView crashes the application

485 Views Asked by At

I'm trying to make an application on Flutter. I ran into a problem: I can't make the images stretch in width.

I need 2 columns, each has an image, each column should be 50% of the width of the screen, and stretch image with column. Everything works as shown in the picture, until I insert it into SingleChildScrollView. After that, the application crashes until I set the exact height of the image.

enter image description here

Question: how to do this WITHOUT specifying the height of the image? I can't set height anywhere inside Row, because I don't know exact size а the image.

2

There are 2 best solutions below

0
eamirho3ein On BEST ANSWER

try this:

SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Row(
                children: [
                  Expanded(
                      child: Image.asset(
                    'assets/images/test.jpeg',
                    fit: BoxFit.contain,
                  )),
                  Expanded(
                      child: Image.asset(
                    'assets/images/test.jpeg',
                    fit: BoxFit.contain,
                  ))
                ],
              ),
            ),

enter image description here

1
Darshan Sheta On

Try flexible instead of expanded

SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Row(
                children: [
                  Flexible(
                      child: Image.asset(
                    'assets/images/test.jpeg',
                    fit: BoxFit.contain,
                  )),
                  Flexible(
                      child: Image.asset(
                    'assets/images/test.jpeg',
                    fit: BoxFit.contain,
                  ))
                ],
              ),
            ),