简体   繁体   中英

Display a svg from a String in Flutter/Dart

I'm trying to display an image from a svg String to the screen.

I'm using: https://pub.dartlang.org/packages/flutter_svg for svg support.

final String svgAsString = '''<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="109" height="109" viewBox="0 
0 109 109">
<g id="kvg:StrokePaths_080a1" style="fill:none;stroke:#000000;stroke- 
width:3;stroke-linecap:round;stroke-linejoin:round;">
<path id="kvg:080a1-s1" 
d="M20.22,18.74c0.77,0.77,0.79,2.14,0.8,3.05C21.38,62,20.62,75,11.5,89.39"/>
</g>
</svg>''';

I'm having trouble getting this rendered to either an image widget or draw onto a canvas. I've tried both ways and not getting anywhere.

The full code I will paste below: I'm not sure if I'm on the right tracks.

import 'dart:async';
import 'dart:ui';

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

 class KanjiGradeGenerator extends StatefulWidget {
     @override
     _KanjiGradeState createState() => _KanjiGradeState();
 }

 class _KanjiGradeState extends State<KanjiGradeGenerator> {

 _KanjiGradeState() {}

displaySVG() async {

<g id="kvg:StrokePaths_080a1" style="fill:none;stroke:#000000;stroke- 
width:3;stroke-linecap:round;stroke-linejoin:round;">
<path id="kvg:080a1-s1" 
d="M20.22,18.74c0.77,0.77,0.79,2.14,0.8,3.05C21.38,62,20.62,75,11.5,89.39"/>
</g>
</svg>''';

final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);

final Picture picture = svgRoot.toPicture();

PictureRecorder picRec;
Canvas can = Canvas(picRec);

setState(() {
  can.drawPicture(picture);
});
}

Widget build(BuildContext context) {
    return new Scaffold(
      body: Container(
        child: displaySVG()
      ),
    );
 }
}

The error I receive is:

I/flutter ( 7791): type 'Future' is not a subtype of type 'Widget'

You can use FutureBuilder widget for display image.

class KanjiGradeGenerator extends StatefulWidget {
  @override
  _KanjiGradeGeneratorState createState() => _KanjiGradeGeneratorState();
}

class _KanjiGradeGeneratorState extends State<KanjiGradeGenerator> {
  final rawSvg = 'YOUR_PATH';

  displaySvg() async {
    final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);
    return await svgRoot.toPicture().toImage(500, 500);
  }
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: displaySvg(),
      builder: (context,snapshot){
        return Container(
          child: snapshot.data,
        );
      },
    );
  }
}

Despite the documentation suggesting to do this:

final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);

I found you can run in to difficulties converting this to a Widget .

If your image string is encoded/decoded from Base64 then you can do this .

Once you have a string you can do:

import 'package:flutter_svg/flutter_svg.dart';

String decoded; // Decoded image

@override
Widget build(BuildContext context) {
    return new Scaffold(
      body: Container(
        child: SvgPicture.string(decoded)
      ),
    );
 }

You can do it like this:

SvgPicture.string(
                '''<svg viewBox="...">...</svg>''' //Or your store it in a variable
            )

then complete(inside State) sample usage would be:

class _SampleSvgState extends State<_SampleSvg> {
late String rawSvg;
@override
void initState() {
  super.initState();
  rawSvg = '''<svg viewBox="...">...</svg>''';
}

@override
Widget build(BuildContext context) {
return Padding(
  padding: const EdgeInsets.only(right: Const.space15),
  child: InkWell(
    onTap: (){},
    borderRadius: BorderRadius.circular(Const.space12),
    child: SizedBox(
      width: 60,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 45,
            height: 45,
            padding: const EdgeInsets.all(Const.space12),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(Const.space12),
            ),
            child: SvgPicture.string(
                rawSvg
            ),
          ),
          const SizedBox(height: Const.space8),
         Expanded(
            child: Text(
              "I am Sample Text",
              maxLines: 1,
              textAlign: TextAlign.center,
              style: _theme.textTheme.subtitle2,
            ),
          )
        ],
      ),
    ),
  ),
);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM