在Flutter/Dart中显示字符串的svg

4c8rllxm  于 2022-11-17  发布在  Flutter
关注(0)|答案(3)|浏览(209)

我正在尝试将svg字符串中的图像显示到屏幕上。
我使用:https://pub.dartlang.org/packages/flutter_svg来支持svg。

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>''';

我在将其渲染到图像小部件或绘制到画布上时遇到了麻烦。我尝试了两种方法,但没有任何效果。
完整的代码我将粘贴如下:我不确定我的思路是否正确。

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()
      ),
    );
 }
}

我收到的错误是:
I/扑动(7791):类型'Future'不是类型'Widget'的子类型

qnakjoqk

qnakjoqk1#

尽管documentation暗示要这样做:
final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);
我发现你可能会遇到困难,转换成一个Widget
如果您的图像字符串是从Base64编码/解码的,则可以执行this
一旦你有了一个字符串,你可以做:

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)
      ),
    );
 }
cuxqih21

cuxqih212#

您可以使用FutureBuilder小部件来显示图像。

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,
        );
      },
    );
  }
}
gz5pxeao

gz5pxeao3#

您可以这样做:

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

则完整(内部状态)示例用法为:

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,
            ),
          )
        ],
      ),
    ),
  ),
);
}
}

相关问题