Flutter_svg: Image sizes should only be fixed on one dimension

Created on 23 Sep 2019  路  4Comments  路  Source: dnfield/flutter_svg

Consider the following repro, which I have attached in full as a zip at the end of this issue:

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final column = Column(
      children: <Widget>[
        Row(children: [
          ImageWithText(),
        ]),
        Row(
          children: <Widget>[
            ImageWithText(),
            ImageWithText(),
          ],
        ),
        Row(
          children: <Widget>[
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
          ],
        ),
        Row(
          children: <Widget>[
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
          ],
        ),
        Row(
          children: <Widget>[
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
            ImageWithText(),
          ],
        ),
      ],
    );
    final scaffold = Scaffold(
      appBar: AppBar(),
      body: column,
    );
    final materialApp = MaterialApp(
      home: scaffold,
    );
    return materialApp;
  }
}

class ImageWithText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final image = SvgPicture.asset('assets/sample.svg');
    //final image = Image.asset('assets/bitmap.png');
    final imageContainer = Container(
      child: image,
      color: Colors.amber,
    );
    final text = Text('Hello');
    final column = Column(
      children: <Widget>[
        imageContainer,
        text,
      ],
    );
    final expanded = Expanded(
      child: column,
    );
    return expanded;
  }
}

When displaying a bitmap (via Image.asset) I see:

Screenshot_20190923-165832

But when displaying an SVG (via SvgPicture.asset) I see:

Screenshot_20190923-165851

Notice how as the horizontal space gets tighter, the images correctly get narrower, but the SVGs incorrectly retain their height as they get narrower.

Debugging through the code, I believe the problem is here. Setting both the width and height means that the SizedBox only shrinks in one direction. I think it should be setting whichever dimension is largest. I tried commenting out the assignment to height (which works for this case) and it worked as expected. I think the code should be something like:

if (width == null && height == null) {
    width = viewport.width > viewport.height ? viewport.width : null;
    height = viewport.height > viewport.width ? viewport.height : null;
} 

repro_svg_sizing_issue.zip

Most helpful comment

any update on this issue? kind of an annoying bug

All 4 comments

I agree that box sizing is broken at the moment, but removing the SizedBox as in #228 is not a valid fix as it breaks many other use cases, especially the automatic scaling and fitting of the image.

any update on this issue? kind of an annoying bug

Argh. This ended up being much more breaking than anticipated. I'm probably going to have to at least temporarily revert it. Looking into that more later though.

Remaining issue fixed by #529

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Skyost picture Skyost  路  3Comments

coupestartup picture coupestartup  路  6Comments

ifiokjr picture ifiokjr  路  5Comments

LeandroDoldan picture LeandroDoldan  路  3Comments

zjiekai picture zjiekai  路  6Comments