Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solidColor parameter to handle colorFilter internally #1065

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions packages/flutter_svg/lib/svg.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'package:vector_graphics/vector_graphics_compat.dart';
Expand Down Expand Up @@ -83,6 +84,7 @@ class SvgPicture extends StatelessWidget {
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.colorFilter,
this.solidColor,
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
Expand Down Expand Up @@ -184,6 +186,7 @@ class SvgPicture extends StatelessWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.solidColor,
SvgTheme? theme,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
Expand All @@ -195,7 +198,9 @@ class SvgPicture extends StatelessWidget {
assetBundle: bundle,
theme: theme,
),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
colorFilter = colorFilter ??
_getSolidColor(solidColor) ??
_getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays an SVG obtained from the network.
Expand Down Expand Up @@ -241,6 +246,7 @@ class SvgPicture extends StatelessWidget {
this.matchTextDirection = false,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.solidColor,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
Expand All @@ -256,7 +262,9 @@ class SvgPicture extends StatelessWidget {
theme: theme,
httpClient: httpClient,
),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
colorFilter = colorFilter ??
_getSolidColor(solidColor) ??
_getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays an SVG obtained from a [File].
Expand Down Expand Up @@ -299,6 +307,7 @@ class SvgPicture extends StatelessWidget {
this.matchTextDirection = false,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.solidColor,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
Expand All @@ -308,7 +317,9 @@ class SvgPicture extends StatelessWidget {
SvgTheme? theme,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgFileLoader(file, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
colorFilter = colorFilter ??
_getSolidColor(solidColor) ??
_getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays an SVG obtained from a [Uint8List].
Expand Down Expand Up @@ -348,6 +359,7 @@ class SvgPicture extends StatelessWidget {
this.matchTextDirection = false,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.solidColor,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
Expand All @@ -357,7 +369,9 @@ class SvgPicture extends StatelessWidget {
SvgTheme? theme,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgBytesLoader(bytes, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
colorFilter = colorFilter ??
_getSolidColor(solidColor) ??
_getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays an SVG obtained from a [String].
Expand Down Expand Up @@ -398,6 +412,7 @@ class SvgPicture extends StatelessWidget {
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
ui.ColorFilter? colorFilter,
this.solidColor,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
this.semanticsLabel,
Expand All @@ -406,13 +421,18 @@ class SvgPicture extends StatelessWidget {
SvgTheme? theme,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgStringLoader(string, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
colorFilter = colorFilter ??
_getSolidColor(solidColor) ??
_getColorFilter(color, colorBlendMode),
super(key: key);

static ColorFilter? _getColorFilter(
ui.Color? color, ui.BlendMode colorBlendMode) =>
color == null ? null : ui.ColorFilter.mode(color, colorBlendMode);

static ColorFilter? _getSolidColor(Color? color) =>
color == null ? null : ui.ColorFilter.mode(color, ui.BlendMode.srcIn);

/// The default placeholder for a SVG that may take time to parse or
/// retrieve, e.g. from a network location.
static WidgetBuilder defaultPlaceholderBuilder =
Expand Down Expand Up @@ -460,6 +480,9 @@ class SvgPicture extends StatelessWidget {
/// The placeholder to use while fetching, decoding, and parsing the SVG data.
final WidgetBuilder? placeholderBuilder;

/// Pass color object to color the svg, it is a replacement of old color object
final Color? solidColor;

/// If true, will horizontally flip the picture in [TextDirection.rtl] contexts.
final bool matchTextDirection;

Expand Down