搜索分类、插件与代码仓

插件详情v2.0.0

cached_network_svg_image

一个用于缓存 SVG 图像的 Flutter 包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2.6万
喜欢数
58
Pub 分数
120

安装插件

flutter pub add cached_network_svg_image

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个用于从网络高效加载和缓存 SVG 图像的 Flutter 包。它通过处理网络请求、缓存和渲染,简化了在 Flutter 应用中显示可缩放矢量图形的过程,只需最少的样板代码。该包支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台,确保在各种设备上行为一致。通过本地缓存减少重复下载,从而提升性能并加快图像加载速度。

适合场景

  • 从URL显示可缩放的图标
  • 缓存远程 SVG 资产以供离线使用
  • 通过缓存矢量图形提升应用性能
  • 在跨平台 Flutter 应用中加载 SVG
  • 减少重复图像加载的带宽使用

主要特点

  • 自动缓存已下载的 SVG
  • 支持所有主要的 Flutter 平台
  • 高效的内存和磁盘使用
  • 与现有 Flutter 代码的简单集成
  • 内置错误处理和重试逻辑

适合对象

  • 需要频繁加载SVG图像的应用
  • 寻求优化图像性能的开发者
  • 跨平台项目使用矢量图形
  • 面向离线功能的应用程序
  • 优先考虑干净、可重用的图像组件的团队

使用建议

  • 初始下载需要互联网连接
  • 缓存管理可能需要手动清除
  • 大型 SVG 文件可能会影响初始加载时间
  • SVG 渲染选项的自定义有限
  • 取决于底层SVG解析库的稳定性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Cached Network SVG Image

cached_network_svg_image is a Flutter package that allows you to easily load and cache SVG images from the network. It provides a simple and efficient way to display SVG images with caching, placeholders, and error handling. 🌐🖼️ Now supports Web! 🌍

Features ✨

  • Load SVG images from the network. 🌍
  • Cache images for offline use. 📦
  • Display a placeholder while the image is loading. ⏳
  • Show an error widget if the image fails to load. ❌
  • Customize the fade-in duration for the image. ⏱️

Installation 🛠️

Add the following to your pubspec.yaml file:

dependencies:
  cached_network_svg_image: any

Then, run flutter pub get to install the package. 🚀

Usage 📖

Import the package in your Dart file:

import 'package:cached_network_svg_image/cached_network_svg_image.dart';

Use the CachedNetworkSVGImage widget to display an SVG image from the network:

CachedNetworkSVGImage(
    imageUrl: 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg',
    placeholder: const CircularProgressIndicator(color: Colors.green),
    errorWidget: const Icon(Icons.error, color: Colors.red),
    width: 250.0,
    height: 250.0,
    fadeDuration: const Duration(milliseconds: 500),
),
Parameters 📋
  • imageUrl: The URL of the SVG image. 🌐
  • cacheKey: A custom cache key to use for the image. 🔑
  • placeholder: A widget to display while the image is loading. ⏳
  • errorWidget: A widget to display if the image fails to load. ❌
  • width: The width of the image. 📏
  • height: The height of the image. 📏
  • fadeDuration: The duration of the fade-in animation when the image loads. ⏱️
  • headers: A map of headers to pass with the network request. 🗂️
  • cacheManager: A custom cache manager to use for caching the image, Default is DefaultCacheManager(). 📦
  • fit: How the image should be inscribed into the space allocated for it. 🔲
  • alignment: How the image should be aligned within its bounds. 📐
  • matchTextDirection: Whether to paint the image in the direction of the TextDirection. ↔️
  • allowDrawingOutsideViewBox: Whether to allow the image to draw outside its view box. 📏
  • color: The color to apply to the image. 🎨
  • colorBlendMode: The blend mode to apply to the image color. 🎨
  • semanticsLabel: A description of the image for accessibility tools. 🏷️
  • excludeFromSemantics: Whether the image should be excluded from semantics. 🚫
  • colorFilter: A color filter to apply to the image. 🎨
  • placeholderBuilder: A builder function to create the placeholder widget. 🛠️
  • theme: The theme to apply to the image. 🎨
Functions 🔧
  • preCache: A function to pre-cache the image. 📦
  • clearCacheForUrl: A function to clear the cache for a specific URL. 🗑️
  • clearCache: A function to clear the entire cache. 🗑️

Example 💡

Here is a complete example of how to use the cached_network_svg_image package:

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

void main() {
    runApp(MyApp());
}

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
            home: Scaffold(
                appBar: AppBar(
                    title: Text('Cached Network SVG Image Example'),
                ),
                body: Center(
                    child: CachedNetworkSVGImage(
                        imageUrl: 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg',
                        placeholder: const CircularProgressIndicator(color: Colors.green),
                        errorWidget: const Icon(Icons.error, color: Colors.red),
                        width: 250.0,
                        height: 250.0,
                        fadeDuration: const Duration(milliseconds: 500),
                    ),
                ),
            ),
        );
    }
}

License 📄

This project is licensed under the MIT License. See the LICENSE file for more details.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目