Home > other >  how to load svg stored in string variable in flutter
how to load svg stored in string variable in flutter

Time:03-31

i got svg String from barcode generator dependency (i am using for generate barcode from numbers) and stored in variable and i want to display this svg as image format in flutter

final svg = Barcode.code39().toSvg(
      "CODE 39",
      width: 200,
      height: 80,
      fontHeight: 10,
    );
   print(svg.toString());

How to load svg into image or icon widget stored variable

note :- my svg is stored in variable like below . svg variable printed below .

flutter: <svg viewBox="0.00000 0.00000 200.00000 80.00000" 
    xmlns="http://www.w3.org/2000/svg"><path d="M 0.00000 0.00000 h 1.70940 v 66.00000 h 
    -1.70940 z M 5.12821 0.00000 h 1.70940 v 66.00000 h -1.70940 z M 8.54701 0.00000 h 
    3.41880 v 66.00000 h -3.41880 z M 13.67521 0.00000 h 3.41880 v 66.00000 h -3.41880 z 
    M 18.80342 0.00000 h 1.70940 v 66.00000 h -1.70940 z M 22.22222 0.00000 h 3.41880 v 
    66.00000 h -3.41880 z M 27.35043 0.00000 h 3.41880 v 66.00000 h -3.41880 z M 32.47863 
    0.00000 h 1.70940 v 66.00000 h -1.70940 z M 37.60684 0.00000 h 1.70940 v 66.00000 h 
    -1.70940 z M 41.02564 0.00000 h 1.70940 v 66.00000 h -1.70940 z M 44.44444 0.00000 h 
    3.41880 v 66.00000 h -3.41880 z M 49.57265 0.00000 h 1.70940 v 66.00000 h -1.70940 z 
    M 52.99145 0.00000 h 3.41880 v 66.00000 h -3.41880 z M 58.11966 0.00000 h 1.70940 v 
    66.00000 h -1.70940 z M 63.24786 0.00000 h 1.70940 v 66.00000 h -1.70940 z M 66.66667 
    0.00000 h 1.70940 v 66.00000 h -1.70940 z M 70.08547 0.00000 h 1.70940 v 66.00000 h 
    -1.70940 z M 73.504<…>

CodePudding user response:

Use the flutter_svg package

final svg = Barcode.code39().toSvg(
  "CODE 39",
  width: 200,
  height: 80,
  fontHeight: 10,
);


SvgPicture.string(
  svg.toString(),
  width: 200,
  height: 80,
);

This will return a widget that you can display as a child of any other widget.

CodePudding user response:

Try using this constructor, you'll have to provide the width or height though. Let me know if this works.

According to the docs:

The bytes argument must not be null.

Either the width and height arguments should be specified, or the widget should be placed in a context that sets tight layout constraints. Otherwise, the image dimensions will change as the image is loaded, which will result in ugly layout changes.

  • Related