[英]How do you use SVG's on Flutter Web?
flutter_web 文档说 dart:svg 已移植,但是您如何将其与 flutter 资产/小部件系统一起使用?
Honestly the best solution I've found is to load the image over a network for the web.老实说,我发现的最佳解决方案是通过网络加载网络图像。 Store the asset on a CDN somewhere, get it from the CDN on the web version, and retrieve it from your assets locally on iOS/Android.
将资产存储在 CDN 上的某处,从 Web 版本的 CDN 中获取它,然后在 iOS/Android 上从本地的资产中检索它。
I've created a CrossPlatformSvg
widget using this library: https://pub.dev/packages/flutter_svg , something like:我使用这个库创建了一个
CrossPlatformSvg
小部件: https : CrossPlatformSvg
,类似于:
class CrossPlatformSvg {
static Widget asset(
String assetPath, {
double width,
double height,
BoxFit fit = BoxFit.contain,
Color color,
alignment = Alignment.center,
String semanticsLabel,
}) {
// `kIsWeb` is a special Flutter variable that just exists
// Returns true if we're on web, false for mobile
if (kIsWeb) {
return Image.network(
assetPath,
width: width,
height: height,
fit: fit,
color: color,
alignment: alignment,
);
} else {
return SvgPicture.network(
assetPath,
width: width,
height: height,
fit: fit,
color: color,
alignment: alignment,
placeholderBuilder: (_) => Container(
width: 30,
height: 30,
padding: EdgeInsets.all(30),
child: CircularIndicator(),
),
);
}
}
}
@aterenshkov's approach worked for me (see comments under Jack's answer). @aterenshkov 的方法对我有用(请参阅杰克回答下的评论)。 Here are the details to implement...
以下是实施的详细信息...
iOS / Android iOS / 安卓
child: SvgPicture.asset('assets/yourimage.svg')
Web网络
// remove assets folder reference
child: SvgPicture.asset('yourimage.svg')
Combine these 2 together...将这两个组合在一起......
import 'package:flutter/foundation.dart'; // provides kIsWeb property
...
child: kIsWeb ? SvgPicture.asset('yourimage.svg') : SvgPicture.asset('assets/yourimage.svg')
My Flutter web app would render SVGs in a desktop web browser but not a mobile web browser using flutter_svg .我的 Flutter Web 应用程序会在桌面 Web 浏览器中呈现 SVG,而不是使用flutter_svg的移动 Web 浏览器。 I found it necessary to build with canvaskit support in order for SVGs to render on mobile:
我发现有必要使用 canvaskit 支持进行构建,以便 SVG 在移动设备上呈现:
flutter build web --web-renderer canvaskit
However, the docs state that canvaskit adds about 2MB in download size, which was a dealbreaker for me.然而,文档指出 canvaskit 增加了大约 2MB 的下载大小,这对我来说是一个破坏者。 Sad to say, but I will be using raster images until this is resolved.
很遗憾,但在解决此问题之前,我将使用光栅图像。
@RumbleFish has nicely but using ternary operation at many can make the code messy. @RumbleFish 很好,但在许多情况下使用三元运算会使代码变得混乱。
My approach to this is:我对此的做法是:
assets/
from the path.assets/
。extension ForWeb on String { String forWeb({required bool web}) => web ? this.replaceFirst('assets/','') : this; }
SvgPicture.asset("assets/images/logo.png".forWeb(web: kIsWeb));
flutter_svg version 1.0.0 is out since Dec 2, 2021 including web support. flutter_svg 1.0.0 版自 2021 年 12 月 2 日起发布,包括网络支持。 So there is no longer a blocker to use svg images inside a Flutter project.
因此,在 Flutter 项目中不再有使用 svg 图像的阻止程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.