【问题标题】:SvgPicture can't be assigned to ImageProvider无法将 SvgPicture 分配给 ImageProvider
【发布时间】:2020-03-31 16:47:55
【问题描述】:

我正在尝试在 Container 中使用 svg 图像作为背景,我使用这个包“flutter_svg”我找不到提供者方法来在装饰中设置它

    Container(
              margin: EdgeInsets.symmetric(horizontal: 5),
              width: 60,
              height: 20,
              decoration: BoxDecoration(
                color: currentColor.medium,
                borderRadius: BorderRadius.circular(4),
                image: DecorationImage(
                  image: SvgPicture.asset('assets/images/home.png'),
                ),
              ),
);

参数类型'SvgPicture'不能分配给参数类型'ImageProvider'.dart(argument_type_not_assignable)

所以我不知道如何在这里使用 svg 作为 ImageProvider,任何想法

【问题讨论】:

  • 为什么不使用其他格式?
  • @Guilherme 因为它比 png 小

标签: flutter dart


【解决方案1】:

有两种选择:

第一个,为此编写一个函数,如flutter_svghere 中所述:

final String rawSvg = '''<svg viewBox="...">...</svg>''';
final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);

final Picture picture = svgRoot.toPicture();
final Image image = await picture.toImage(width, height);
return RawImage(image, 1.0);

第二个,在svg_flutter 包之上使用另一个package

import 'package:flutter_svg_provider/flutter_svg_provider.dart';

Image(
  width: 32,
  height: 32,
  image: Svg('assets/my_icon.svg'),
)

【讨论】:

    【解决方案2】:

    Flutter 开发人员创建了一个库,您可以在此处找到 https://www.developerlibs.com/2019/06/flutter-draw-svg-vector-drawables.html

    【讨论】:

    • 我知道我已经在使用该库,但问题是我不能将它用作 ImageProvider
    猜你喜欢
    • 2019-11-28
    • 2021-11-20
    • 2023-02-10
    • 2021-11-03
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多