Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield committed Nov 5, 2022
1 parent 37c8e31 commit 0f5f9a3
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 16 deletions.
66 changes: 58 additions & 8 deletions lib/src/loaders.dart
@@ -1,6 +1,6 @@
import 'dart:convert' show utf8;
import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/src/utilities/http.dart';
import 'package:vector_graphics/vector_graphics.dart';
Expand All @@ -25,7 +25,7 @@ class SvgStringLoader extends BytesLoader {
final SvgTheme theme;

@override
Future<ByteData> loadBytes(BuildContext context) async {
Future<ByteData> loadBytes(BuildContext? context) async {
return await compute((String svg) async {
final Uint8List compiledBytes = await encodeSvg(
xml: svg,
Expand Down Expand Up @@ -57,7 +57,7 @@ class SvgBytesLoader extends BytesLoader {
final SvgTheme theme;

@override
Future<ByteData> loadBytes(BuildContext context) async {
Future<ByteData> loadBytes(BuildContext? context) async {
return await compute((_) async {
final Uint8List compiledBytes = await encodeSvg(
xml: utf8.decode(svg),
Expand Down Expand Up @@ -88,7 +88,7 @@ class SvgFileLoader extends BytesLoader {
final SvgTheme theme;

@override
Future<ByteData> loadBytes(BuildContext context) async {
Future<ByteData> loadBytes(BuildContext? context) async {
return await compute((File file) async {
final Uint8List bytes = file.readAsBytesSync();
final Uint8List compiledBytes = await encodeSvg(
Expand All @@ -104,6 +104,38 @@ class SvgFileLoader extends BytesLoader {
}
}

// Replaces the cache key for [AssetBytesLoader] to account for the fact that
// different widgets may select a different asset bundle based on the return
// value of `DefaultAssetBundle.of(context)`.
@immutable
class _AssetByteLoaderCacheKey {
const _AssetByteLoaderCacheKey(
this.assetName,
this.packageName,
this.assetBundle,
);

final String assetName;
final String? packageName;

final AssetBundle assetBundle;

@override
int get hashCode => Object.hash(assetName, packageName, assetBundle);

@override
bool operator ==(Object other) {
return other is _AssetByteLoaderCacheKey &&
other.assetName == assetName &&
other.assetBundle == assetBundle &&
other.packageName == packageName;
}

@override
String toString() =>
'VectorGraphicAsset(${packageName != null ? '$packageName/' : ''}$assetName)';
}

/// A [BytesLoader] that decodes and parses an SVG asset in an isolate and
/// creates a vector_graphics binary representation.
class SvgAssetLoader extends BytesLoader {
Expand All @@ -127,10 +159,19 @@ class SvgAssetLoader extends BytesLoader {
/// The theme to determine currentColor and font sizing attributes.
final SvgTheme theme;

AssetBundle _resolveBundle(BuildContext? context) {
if (assetBundle != null) {
return assetBundle!;
}
if (context != null) {
return DefaultAssetBundle.of(context);
}
return rootBundle;
}

@override
Future<ByteData> loadBytes(BuildContext context) async {
final ByteData bytes =
await (assetBundle ?? DefaultAssetBundle.of(context)).load(assetName);
Future<ByteData> loadBytes(BuildContext? context) async {
final ByteData bytes = await _resolveBundle(context).load(assetName);

return await compute((_) async {
final Uint8List compiledBytes = await encodeSvg(
Expand All @@ -145,6 +186,15 @@ class SvgAssetLoader extends BytesLoader {
}, null, debugLabel: 'Load Bytes');
}

@override
Object cacheKey(BuildContext? context) {
return _AssetByteLoaderCacheKey(
assetName,
packageName,
_resolveBundle(context),
);
}

@override
int get hashCode => Object.hash(assetName, packageName, assetBundle);

Expand Down Expand Up @@ -177,7 +227,7 @@ class SvgNetworkLoader extends BytesLoader {
final SvgTheme theme;

@override
Future<ByteData> loadBytes(BuildContext context) async {
Future<ByteData> loadBytes(BuildContext? context) async {
return await compute((String svgUrl) async {
final Uint8List bytes = await httpGet(svgUrl, headers: headers);
final Uint8List compiledBytes = await encodeSvg(
Expand Down
92 changes: 87 additions & 5 deletions lib/svg.dart
@@ -1,3 +1,5 @@
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:vector_graphics/vector_graphics_compat.dart';
Expand All @@ -10,6 +12,55 @@ export 'package:vector_graphics_compiler/vector_graphics_compiler.dart'
show SvgTheme;
export 'src/loaders.dart';

/// Instance for [Svg]'s utility methods, which can produce a [DrawableRoot]
/// or [PictureInfo] from [String] or [Uint8List].
final Svg svg = Svg._();

/// A utility class for decoding SVG data to a [DrawableRoot] or a [PictureInfo].
///
/// These methods are used by [SvgPicture], but can also be directly used e.g.
/// to create a [DrawableRoot] you manipulate or render to your own [Canvas].
/// Access to this class is provided by the exported [svg] member.
class Svg {
Svg._();

/// A global override flag for [SvgPicture.cacheColorFilter].
///
/// If this is null, the value in [SvgPicture.cacheColorFilter] is used. If it
/// is not null, it will override that value.
@Deprecated('This no longer does anything.')
bool? cacheColorFilterOverride;

/// The cache instance for decoded SVGs.
final Cache cache = Cache();
}

/// The cache for decoded SVGs.
class Cache {
int _maximumSize = 100;

/// The maximum number of decoded SVGs to keep in memory.
int get maximumSize => _maximumSize;
set maximumSize(int value) {
if (value == _maximumSize) {
return;
}
_maximumSize = value;
}

/// Clears the cache.
void clear() {}
}

// ignore: avoid_classes_with_only_static_members
/// Deprecated class, will be removed, does not do anything.
@Deprecated('This feature does not do anything anymore.')
class PictureProvider {
/// Deprecated, use [svg.cache] instead.
@Deprecated('Use svg.cache instead.')
static Cache get cache => svg.cache;
}

/// A widget that will parse SVG data for rendering on screen.
class SvgPicture extends StatelessWidget {
/// Instantiates a widget that renders an SVG picture using the `pictureProvider`.
Expand Down Expand Up @@ -48,6 +99,8 @@ class SvgPicture extends StatelessWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.theme = const SvgTheme(),
@deprecated Clip? clipBehavior,
@deprecated bool cacheColorFilter = false,
}) : super(key: key);

/// Instantiates a widget that renders an SVG picture from an [AssetBundle].
Expand Down Expand Up @@ -142,16 +195,21 @@ class SvgPicture extends StatelessWidget {
this.alignment = Alignment.center,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.colorFilter,
this.semanticsLabel,
this.excludeFromSemantics = false,
this.theme = const SvgTheme(),
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
@deprecated Clip? clipBehavior,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgAssetLoader(
assetName,
packageName: package,
assetBundle: bundle,
theme: theme,
),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays a [PictureStream] obtained from the network.
Expand Down Expand Up @@ -197,11 +255,16 @@ class SvgPicture extends StatelessWidget {
this.matchTextDirection = false,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.colorFilter,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
this.semanticsLabel,
this.excludeFromSemantics = false,
@deprecated Clip? clipBehavior,
@deprecated bool cacheColorFilter = false,
this.theme = const SvgTheme(),
}) : bytesLoader = SvgNetworkLoader(url, headers: headers, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays a [PictureStream] obtained from a [File].
Expand Down Expand Up @@ -244,11 +307,16 @@ class SvgPicture extends StatelessWidget {
this.matchTextDirection = false,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.colorFilter,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
this.semanticsLabel,
this.excludeFromSemantics = false,
this.theme = const SvgTheme(),
@deprecated Clip? clipBehavior,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgFileLoader(file, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays a [PictureStream] obtained from a [Uint8List].
Expand Down Expand Up @@ -288,11 +356,16 @@ class SvgPicture extends StatelessWidget {
this.matchTextDirection = false,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.colorFilter,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
this.semanticsLabel,
this.excludeFromSemantics = false,
this.theme = const SvgTheme(),
@deprecated Clip? clipBehavior,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgBytesLoader(bytes, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
super(key: key);

/// Creates a widget that displays a [PictureStream] obtained from a [String].
Expand Down Expand Up @@ -332,13 +405,22 @@ class SvgPicture extends StatelessWidget {
this.matchTextDirection = false,
this.allowDrawingOutsideViewBox = false,
this.placeholderBuilder,
this.colorFilter,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
this.semanticsLabel,
this.excludeFromSemantics = false,
this.theme = const SvgTheme(),
@deprecated Clip? clipBehavior,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgStringLoader(string, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
super(key: key);

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

/// 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
6 changes: 3 additions & 3 deletions pubspec.yaml
Expand Up @@ -9,9 +9,9 @@ dependencies:
sdk: flutter
meta: ^1.7.0
path_drawing: ^1.0.1
vector_graphics: ^0.0.3
vector_graphics_codec: ^0.0.3
vector_graphics_compiler: ^0.0.3
vector_graphics: ^0.0.5
vector_graphics_codec: ^0.0.5
vector_graphics_compiler: ^0.0.5
vector_math: ^2.1.2

dev_dependencies:
Expand Down

0 comments on commit 0f5f9a3

Please sign in to comment.