Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use SVG's on Flutter Web?

The flutter_web docos say to that dart:svg is ported, but how do you use that with the flutter asset / widget system?

like image 882
dazza5000 Avatar asked Jul 24 '19 02:07

dazza5000


People also ask

How do I use SVG in Flutter Web?

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. On the web in debug mode, I get the following error. So I don't think Image.network supports SVG.

How do I show SVG files in Flutter?

First, go and create the folder called images in the root structure and add any SVG image which you like. Then open the pubspec. yaml and specify your file name under the assets. Next you can load the file from asset as mentioned in below.

Is SVG good for Flutter?

Although Flutter doesn't support SVG natively, the Dart-native flutter_svg package has excellent performant and fast support for SVG files.


1 Answers

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.

I've created a CrossPlatformSvg widget using this library: https://pub.dev/packages/flutter_svg, something like:

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

like image 55
Jack Avatar answered Sep 18 '22 06:09

Jack