Skip to content

Commit

Permalink
Adds DefaultSvgTheme (#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
GP4cK committed Jul 13, 2023
1 parent d7b5c23 commit 3148f72
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/flutter_svg/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGES

## 2.0.8

- Adds back `DefaultSvgTheme`.

## 2.0.7

- Fix broken `matchTextDirection`.
Expand Down
42 changes: 42 additions & 0 deletions packages/flutter_svg/lib/src/default_theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/widgets.dart';

import 'loaders.dart';

/// The SVG theme to apply to descendant [SvgPicture] widgets
/// which don't have explicit theme values.
class DefaultSvgTheme extends InheritedTheme {
/// Creates a default SVG theme for the given subtree
/// using the provided [theme].
const DefaultSvgTheme({
Key? key,
required Widget child,
required this.theme,
}) : super(key: key, child: child);

/// The SVG theme to apply.
final SvgTheme theme;

/// The closest instance of this class that encloses the given context.
///
/// Typical usage is as follows:
///
/// ```dart
/// DefaultSvgTheme theme = DefaultSvgTheme.of(context);
/// ```
static DefaultSvgTheme? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<DefaultSvgTheme>();
}

@override
bool updateShouldNotify(DefaultSvgTheme oldWidget) {
return theme != oldWidget.theme;
}

@override
Widget wrap(BuildContext context, Widget child) {
return DefaultSvgTheme(
theme: theme,
child: child,
);
}
}
30 changes: 25 additions & 5 deletions packages/flutter_svg/lib/src/loaders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import 'dart:convert' show utf8;
import 'package:flutter/foundation.dart' hide compute;
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';
import 'package:vector_graphics_compiler/vector_graphics_compiler.dart' as vg;

import '../svg.dart' show svg;
import 'default_theme.dart';
import 'utilities/compute.dart';
import 'utilities/file.dart';
import 'utilities/http.dart';

/// A theme used when decoding an SVG picture.
@immutable
Expand Down Expand Up @@ -111,12 +112,12 @@ class _DelegateVgColorMapper extends vg.ColorMapper {
abstract class SvgLoader<T> extends BytesLoader {
/// See class doc.
const SvgLoader({
this.theme = const SvgTheme(),
this.theme,
this.colorMapper,
});

/// The theme to determine currentColor and font sizing attributes.
final SvgTheme theme;
final SvgTheme? theme;

/// The [ColorMapper] used to transform colors from the SVG, if any.
final ColorMapper? colorMapper;
Expand All @@ -130,7 +131,24 @@ abstract class SvgLoader<T> extends BytesLoader {
Future<T?> prepareMessage(BuildContext? context) =>
SynchronousFuture<T?>(null);

/// Returns the svg theme.
@visibleForTesting
@protected
SvgTheme getTheme(BuildContext? context) {
if (theme != null) {
return theme!;
}
if (context != null) {
final SvgTheme? defaultTheme = DefaultSvgTheme.of(context)?.theme;
if (defaultTheme != null) {
return defaultTheme;
}
}
return const SvgTheme();
}

Future<ByteData> _load(BuildContext? context) {
final SvgTheme theme = getTheme(context);
return prepareMessage(context).then((T? message) {
return compute((T? message) {
return vg
Expand Down Expand Up @@ -160,6 +178,7 @@ abstract class SvgLoader<T> extends BytesLoader {

@override
SvgCacheKey cacheKey(BuildContext? context) {
final SvgTheme theme = getTheme(context);
return SvgCacheKey(keyData: this, theme: theme, colorMapper: colorMapper);
}
}
Expand All @@ -172,13 +191,13 @@ abstract class SvgLoader<T> extends BytesLoader {
class SvgCacheKey {
/// See [SvgCacheKey].
const SvgCacheKey({
required this.theme,
required this.keyData,
required this.colorMapper,
this.theme,
});

/// The theme for this cached SVG.
final SvgTheme theme;
final SvgTheme? theme;

/// The other key data for the SVG.
///
Expand Down Expand Up @@ -365,6 +384,7 @@ class SvgAssetLoader extends SvgLoader<ByteData> {

@override
SvgCacheKey cacheKey(BuildContext? context) {
final SvgTheme theme = getTheme(context);
return SvgCacheKey(
theme: theme,
colorMapper: colorMapper,
Expand Down
15 changes: 6 additions & 9 deletions packages/flutter_svg/lib/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export 'package:vector_graphics/vector_graphics.dart'
show BytesLoader, vg, VectorGraphicUtilities, PictureInfo;

export 'src/cache.dart';
export 'src/default_theme.dart';
export 'src/loaders.dart';

/// Instance for [Svg]'s utility methods, which can produce a [DrawableRoot]
Expand Down Expand Up @@ -84,7 +85,6 @@ class SvgPicture extends StatelessWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.theme = const SvgTheme(),
@deprecated bool cacheColorFilter = false,
}) : super(key: key);

Expand Down Expand Up @@ -179,7 +179,7 @@ class SvgPicture extends StatelessWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.theme = const SvgTheme(),
SvgTheme? theme,
ui.ColorFilter? colorFilter,
@deprecated ui.Color? color,
@deprecated ui.BlendMode colorBlendMode = ui.BlendMode.srcIn,
Expand Down Expand Up @@ -243,7 +243,7 @@ class SvgPicture extends StatelessWidget {
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
@deprecated bool cacheColorFilter = false,
this.theme = const SvgTheme(),
SvgTheme? theme,
}) : bytesLoader = SvgNetworkLoader(url, headers: headers, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
super(key: key);
Expand Down Expand Up @@ -294,7 +294,7 @@ class SvgPicture extends StatelessWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.theme = const SvgTheme(),
SvgTheme? theme,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgFileLoader(file, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
Expand Down Expand Up @@ -343,7 +343,7 @@ class SvgPicture extends StatelessWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.theme = const SvgTheme(),
SvgTheme? theme,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgBytesLoader(bytes, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
Expand Down Expand Up @@ -392,7 +392,7 @@ class SvgPicture extends StatelessWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.theme = const SvgTheme(),
SvgTheme? theme,
@deprecated bool cacheColorFilter = false,
}) : bytesLoader = SvgStringLoader(string, theme: theme),
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
Expand Down Expand Up @@ -479,9 +479,6 @@ class SvgPicture extends StatelessWidget {
/// The color filter, if any, to apply to this widget.
final ColorFilter? colorFilter;

/// The theme used when parsing SVG elements.
final SvgTheme theme;

@override
Widget build(BuildContext context) {
return createCompatVectorGraphic(
Expand Down
Loading

0 comments on commit 3148f72

Please sign in to comment.