Flutter 可随主题变化的SVG组件,简直太完美啦!无敌啦哥们

所需插件

flutter_svg: ^2.0.9 #svg
flutter_cache_manager: ^3.4.0 #网络图像缓存

核心代码 

ColorFilter.matrix 语句会对Svg 的颜色取反色。0~255反转。即使多色也不怕

SvgPicture.asset(
      path,
      colorFilter: Get.isDarkMode && autoColor
          ? ColorFilter.matrix([
              -1, 0, 0, 0, 255, // Red
              0, -1, 0, 0, 255, // Green
              0, 0, -1, 0, 255, // Blue
              0, 0, 0, 1, 0, // Alpha
            ])
          : null,
      width: size,
      height: size,
    );

svg.dart

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_svg/flutter_svg.dart';

///可随主题变化的Svg,简直太完美啦!无敌啦哥们
class ThemedSvgIcon extends StatelessWidget {
  final String path;
  final double size;
  final bool isNetwork;
  final bool autoColor; //因为不需要随主题变化的网络svg也有缓存的需求,故留此bool,如果你没有该需求,可以移除

  ///图标加载失败时 显示的展位符中的字 默认“牛”
  final String errorText;

  const ThemedSvgIcon(
      {required this.path,
      required this.size,
      this.isNetwork = false,
      this.autoColor = true,
      this.errorText = "牛"});

  Future<Uint8List?> _getSvgData(String url) async {
    try {
      // 获取缓存管理器实例
      final fileInfo = await DefaultCacheManager().getSingleFile(url);
      return fileInfo.readAsBytes();
    } catch (e) {
      print("Error fetching SVG: $e");
      return null;
    }
  }
  @override
  Widget build(BuildContext context) {
    if (isNetwork) return networkSvg();
    return SvgPicture.asset(
      path,
      colorFilter: Theme.of(context).colorScheme.brightness == Brightness.dark && autoColor
          ? ColorFilter.matrix([
              -1, 0, 0, 0, 255, // Red
              0, -1, 0, 0, 255, // Green
              0, 0, -1, 0, 255, // Blue
              0, 0, 0, 1, 0, // Alpha
            ])
          : null,
      width: size,
      height: size,
    );
  }

  Widget networkSvg() {
    return FutureBuilder<Uint8List?>(
      future: _getSvgData(path),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasData) {
            return SvgPicture.memory(
              snapshot.data!,
              width: size,
              height: size,
              colorFilter: Theme.of(context).colorScheme.brightness == Brightness.dark && autoColor
                  ? ColorFilter.matrix([
                      -1, 0, 0, 0, 255, // Red
                      0, -1, 0, 0, 255, // Green
                      0, 0, -1, 0, 255, // Blue
                      0, 0, 0, 1, 0, // Alpha
                    ])
                  : null,
            );
          }
        }
        return Container(
          width: size,
          height: size,
          decoration: BoxDecoration(
              color: Colors.grey.withAlpha(50),
              borderRadius: BorderRadius.all(Radius.circular(size / 6))),
          child: Center(
              child: Text(errorText, style: TextStyle(fontSize: size / 2))),
        );
      },
    );
  }
}

使用

Column(
     children: [
    //网络
    ThemedSvgIcon(path:"https://cdn.hugeicons.com/icons/algorithm-stroke-rounded.svg",isNetwork: true,size: 32),
    SizedBox(height: 4),
    //本地
    ThemedSvgIcon(path:AssetsSvgs.consoleDoublecolorMenuSvg, size: 32),
    ],
)

效果 ,太完美啦哥们!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值