Repository: flutter
Issue: [flutter_svg] Resize of widget in constrained conditions not working as expected
Labels: package,p: flutter_svg

_Imported from https://github.com/dnfield/flutter_svg/issues/759_

Original report by @matecode on Aug 29, 2022

I want the SvgPicture widget to automatically resize its height.

Here is a code example:

The starting code is a an easy example of an SvgPicture with the original width an height of the svg image. To see better what happens there is a red background on the container.
```
Container(
          color: Colors.red,
          child: IntrinsicHeight(
            child: SvgPicture.asset(
              Assets.diningplan.images.basket.path,
              fit: BoxFit.scaleDown,
            ),
          ),
        ),
```
results to 
![image](https://user-images.githubusercontent.com/3225274/187167704-8565cd2b-38e3-47b5-b3d1-0ccb15c24b3e.png)

if i add `constraints: BoxConstraints(maxWidth: 20),` to the container, the result is:
![image](https://user-images.githubusercontent.com/3225274/187168200-1ee9cd52-4719-4eb7-98cc-cab75811599c.png)

I would expect that SvgImage resize its own height. 

---
Comment by @gabrielgarciagava on Aug 29, 2022
I have faced the same issue recently. I've created a simple project to show the issue, and I also have a suggestion of how to fix.

Example: https://github.com/gabrielgarciagava/flutter_svg_aspect_ratio_issue
Snippet:
```
class ImageVsSvgPicture extends StatelessWidget {
  const ImageVsSvgPicture({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          color: Colors.green,
          child: Image.asset("images/cube.png"),
        ),
        Container(
          color: Colors.red,
          child: SvgPicture.asset("images/logo.svg"),
        ),
      ],
    );
  }
}
```
<img width="1053" alt="Screenshot 2022-08-29 at 12 23 01" src="https://user-images.githubusercontent.com/1520279/187184425-611347eb-81ac-4e15-9b8e-24451e0df7ef.png">

Image and SvgPitcure behaves differently. SvgPicture behaves as the issue you described. Image behaves as you expected.

The issue is caused by this part of the code:
```    
double? width = widget.width;
double? height = widget.height;
if (width == null && height == null) {
  width = viewport.width;
  height = viewport.height;
} else if (height != null) {
  width = height / viewport.height * viewport.width;
} else if (width != null) {
  height = width / viewport.width * viewport.height;
}
```

In case width and height are not defined, the image is forced to have the dimensions specified by the svg file.
This code was injected to deal with this issue: https://github.com/dnfield/flutter_svg/issues/57#

I suggest using AspectRatio widget instead of setting the width and height.

---
Comment by @gabrielgarciagava on Aug 29, 2022
The code I wrote on my fork fixed the problem that I was facing.
https://github.com/dnfield/flutter_svg/compare/master...gabrielgarciagava:flutter_svg:master

I did not create a PR because I'm not confident if this is correct. All tests are passing though. I should also probably write a test to describe the behavior we expect.

@matecode You can test my fork of the project if you want. Let me know if it works for you.

---
Comment by @Lilian-C on Jul 18, 2023
I am still able to reproduce this error in the latest flutter_svg version (2.0.8).

Are there any fixes coming up soon, or is there any workaround to avoid this problem?