如何在 flutter 中将颜色应用于多色 svg?

How do I apply colors to a multi color svg in flutter?

我正在寻找在 flutter 中更改插图 svg 的颜色。我试过 flutter_svg 包,但它支持只对 svg 应用一种颜色,如果我这样做,svg 将显示为单一颜色 svg,个别颜色会丢失。有没有什么方法可以在运行时为多色 svg 更改颜色?

你的意思是这样的吗?我称之为 SVG 着色。

我将尝试将这个概念压缩到一个简短的摘要中。在 SVG 文件中,您需要使用 fill 属性 来定义十六进制颜色代码。

在编程方面,你会:

  1. 将 SVG 文件数据提取为 String 变量 svgCode

  1. previousColor 中分配前一个十六进制颜色代码 & 在 newColor 中分配当前选择的十六进制颜色代码。

  1. svgCode 上应用 String.replaceAll 方法来替换颜色。

  1. 更新previousColor的值。

更简短的阐述是

/// Initially without any color selection.
SVGPicture.string('''<svg code with fill #f7ebcb>''');
/// After the user selects the red color.
SVGPicture.string('''<svg code with fill #FF0000>''');

This tutorial can help to solve your issue. Not only does this app 在运行时更改颜色,但它也允许用户下载经过处理的 SVG 代码。