feat: Expose paint from svgComponent to set opacity and have opacity effects (#2092)

This commit is contained in:
Dipak Prajapati
2022-10-21 17:58:40 +05:30
committed by GitHub
parent 8395126678
commit bedacd0c8c
2 changed files with 13 additions and 5 deletions

View File

@@ -41,14 +41,19 @@ class Svg {
} }
/// Renders the svg on the [canvas] using the dimensions provided by [size]. /// Renders the svg on the [canvas] using the dimensions provided by [size].
void render(Canvas canvas, Vector2 size) { void render(
Canvas canvas,
Vector2 size, {
Paint? overridePaint,
}) {
final _size = size.toSize(); final _size = size.toSize();
final image = _getImage(_size); final image = _getImage(_size);
if (image != null) { if (image != null) {
canvas.save(); canvas.save();
canvas.scale(1 / pixelRatio); canvas.scale(1 / pixelRatio);
canvas.drawImage(image, Offset.zero, _paint); final drawPaint = overridePaint ?? _paint;
canvas.drawImage(image, Offset.zero, drawPaint);
canvas.restore(); canvas.restore();
} else { } else {
_render(canvas, _size); _render(canvas, _size);

View File

@@ -4,7 +4,7 @@ import 'package:flame/components.dart';
import 'package:flame_svg/svg.dart'; import 'package:flame_svg/svg.dart';
/// Wraps [Svg] in a Flame component. /// Wraps [Svg] in a Flame component.
class SvgComponent extends PositionComponent { class SvgComponent extends PositionComponent with HasPaint {
/// The wrapped instance of [Svg]. /// The wrapped instance of [Svg].
Svg? _svg; Svg? _svg;
@@ -18,7 +18,10 @@ class SvgComponent extends PositionComponent {
super.anchor, super.anchor,
super.children, super.children,
super.priority, super.priority,
}) : _svg = svg; Paint? paint,
}) : _svg = svg {
this.paint = paint ?? this.paint;
}
set svg(Svg? svg) { set svg(Svg? svg) {
_svg?.dispose(); _svg?.dispose();
@@ -30,7 +33,7 @@ class SvgComponent extends PositionComponent {
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
_svg?.render(canvas, size); _svg?.render(canvas, size, overridePaint: paint);
} }
@override @override