You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[flutter_svg] feat: Expose the colorMapper property in SvgPicture (#9043)
## Description
This pull request exposes the existing `colorMapper` functionality in the `flutter_svg` package, allowing developers to customize SVG colors during parsing.
## Related Issue
- flutter/flutter#158634
## Motivation
The `colorMapper` functionality was already present within the `flutter_svg` package but was not directly accessible through the `SvgPicture` constructors. By exposing this property, developers gain a powerful and flexible way to dynamically modify the colors of SVG assets based on custom logic. This can be useful for various scenarios, such as theming or branding.
## Example Usage
```dart
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
const String svgString = '''
<svg viewBox="0 0 100 100">
<rect width="50" height="50" fill="#FF0000" />
<circle cx="75" cy="75" r="25" fill="#00FF00" />
</svg>
''';
class MyColorMapper extends ColorMapper {
const MyColorMapper();
@OverRide
Color substitute(
String? id, String elementName, String attributeName, Color color) {
if (color == const Color(0xFFFF0000)) {
return Colors.blue;
}
if (color == const Color(0xFF00FF00)) {
return Colors.yellow;
}
return color;
}
}
void main() {
runApp(MaterialApp(
home: Scaffold(
body: Center(
child: SvgPicture.string(
svgString,
width: 200,
height: 200,
colorMapper: const MyColorMapper(),
),
),
),
));
}
```
In this example, all red colors in the SVG will be rendered as blue, and all green colors will be rendered as yellow. You can customize the `substitute` method to implement more complex color mapping logic based on your requirements.
82
+
36
83
The default placeholder is an empty box (`LimitedBox`) - although if a `height`
37
84
or `width` is specified on the `SvgPicture`, a `SizedBox` will be used instead
38
85
(which ensures better layout experience). There is currently no way to show an
@@ -67,6 +114,7 @@ If you'd like to render the SVG to some other canvas, you can do something like:
0 commit comments