I am looking for changing colors of an illustration svg in flutter. I tried flutter_svg package but it support applying only one color to the svg and if I do that svg will be displayed as a single color svg, individual colors are lost. Is there any way to change colors at runtime in flutter for a multi color svg?
Asked
Active
Viewed 1,838 times
3
-
You got any solution? – fajar ainul Nov 25 '20 at 08:09
-
https://stackoverflow.com/questions/60946115/interacting-with-svg-in-flutter This is somewhat a solution but I haven't figured it out how to make the color stick because on this example the color goes back to normal when clicked outside – Donki Jan 30 '21 at 21:39
1 Answers
2
Do you mean something like this? I call it SVG colorization.
I'll try to squeeze the concept into a short summary. In an SVG file, you need to play around with the fill
property as it defines the hex color code.
In terms of programming, you would:
- Extract the SVG file data as a
String
variablesvgCode
.
- Assign the previous hex color code in
previousColor
& the currently selected hex color code innewColor
.
- Apply the
String.replaceAll
method onsvgCode
to replace the colors.
- Update the value of the
previousColor
.
A more brief elaboration would be
/// 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 changes the color on runtime, but it also allows the user to download the manipulated SVG code.

Zujaj Misbah Khan
- 655
- 2
- 11
- 31