Skip to content

Add support for vector graphics (vec files) in SvgGenImage #493

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

Merged
merged 10 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
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
29 changes: 20 additions & 9 deletions examples/example/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use

import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:vector_graphics/vector_graphics.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
import 'package:rive/rive.dart';
Expand Down Expand Up @@ -277,11 +278,20 @@ class AssetGenImage {
}

class SvgGenImage {
const SvgGenImage(this._assetName, {this.size = null});
const SvgGenImage(
this._assetName, {
this.size = null,
}) : _isVecFormat = false;

const SvgGenImage.vec(
this._assetName, {
this.size = null,
}) : _isVecFormat = true;

final String _assetName;

final Size? size;
final bool _isVecFormat;

SvgPicture svg({
Key? key,
Expand All @@ -303,12 +313,14 @@ class SvgGenImage {
@deprecated BlendMode colorBlendMode = BlendMode.srcIn,
@deprecated bool cacheColorFilter = false,
}) {
return SvgPicture.asset(
_assetName,
return SvgPicture(
_isVecFormat
? AssetBytesLoader(_assetName,
assetBundle: bundle, packageName: package)
: SvgAssetLoader(_assetName,
assetBundle: bundle, packageName: package),
key: key,
matchTextDirection: matchTextDirection,
bundle: bundle,
package: package,
width: width,
height: height,
fit: fit,
Expand All @@ -318,9 +330,8 @@ class SvgGenImage {
semanticsLabel: semanticsLabel,
excludeFromSemantics: excludeFromSemantics,
theme: theme,
colorFilter: colorFilter,
color: color,
colorBlendMode: colorBlendMode,
colorFilter: colorFilter ??
(color == null ? null : ColorFilter.mode(color, colorBlendMode)),
clipBehavior: clipBehavior,
cacheColorFilter: cacheColorFilter,
);
Expand Down
4 changes: 2 additions & 2 deletions examples/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ description: A sample project using FlutterGen.
publish_to: 'none'

environment:
sdk: '>=2.16.0 <4.0.0'
flutter: '>=3.0.0'
sdk: '>=3.2.0 <4.0.0'
flutter: '>=3.16.0'

dependencies:
flutter:
Expand Down
57 changes: 44 additions & 13 deletions examples/example_resources/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use

import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:vector_graphics/vector_graphics.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
import 'package:rive/rive.dart';
Expand Down Expand Up @@ -55,6 +56,8 @@ class $AssetsUnknownGen {
class ResAssets {
ResAssets._();

static const String package = 'example_resources';

static const $AssetsImagesGen images = $AssetsImagesGen();
static const $AssetsUnknownGen unknown = $AssetsUnknownGen();
}
Expand All @@ -64,6 +67,8 @@ class AssetGenImage {

final String _assetName;

static const String package = 'example_resources';

final Size? size;

Image image({
Expand All @@ -86,7 +91,8 @@ class AssetGenImage {
bool matchTextDirection = false,
bool gaplessPlayback = false,
bool isAntiAlias = false,
String? package = 'example_resources',
@Deprecated('Do not specify package for a generated library asset')
String? package = package,
FilterQuality filterQuality = FilterQuality.low,
int? cacheWidth,
int? cacheHeight,
Expand Down Expand Up @@ -121,7 +127,8 @@ class AssetGenImage {

ImageProvider provider({
AssetBundle? bundle,
String? package = 'example_resources',
@Deprecated('Do not specify package for a generated library asset')
String? package = package,
}) {
return AssetImage(
_assetName,
Expand All @@ -136,15 +143,29 @@ class AssetGenImage {
}

class SvgGenImage {
const SvgGenImage(this._assetName);
const SvgGenImage(
this._assetName, {
this.size = null,
}) : _isVecFormat = false;

const SvgGenImage.vec(
this._assetName, {
this.size = null,
}) : _isVecFormat = true;

final String _assetName;

static const String package = 'example_resources';

final Size? size;
final bool _isVecFormat;

SvgPicture svg({
Key? key,
bool matchTextDirection = false,
AssetBundle? bundle,
String? package = 'example_resources',
@Deprecated('Do not specify package for a generated library asset')
String? package = package,
double? width,
double? height,
BoxFit fit = BoxFit.contain,
Expand All @@ -160,12 +181,14 @@ class SvgGenImage {
@deprecated BlendMode colorBlendMode = BlendMode.srcIn,
@deprecated bool cacheColorFilter = false,
}) {
return SvgPicture.asset(
_assetName,
return SvgPicture(
_isVecFormat
? AssetBytesLoader(_assetName,
assetBundle: bundle, packageName: package)
: SvgAssetLoader(_assetName,
assetBundle: bundle, packageName: package),
key: key,
matchTextDirection: matchTextDirection,
bundle: bundle,
package: package,
width: width,
height: height,
fit: fit,
Expand All @@ -175,9 +198,8 @@ class SvgGenImage {
semanticsLabel: semanticsLabel,
excludeFromSemantics: excludeFromSemantics,
theme: theme,
colorFilter: colorFilter,
color: color,
colorBlendMode: colorBlendMode,
colorFilter: colorFilter ??
(color == null ? null : ColorFilter.mode(color, colorBlendMode)),
clipBehavior: clipBehavior,
cacheColorFilter: cacheColorFilter,
);
Expand All @@ -193,6 +215,8 @@ class FlareGenImage {

final String _assetName;

static const String package = 'example_resources';

FlareActor flare({
String? boundsNode,
String? animation,
Expand Down Expand Up @@ -236,6 +260,8 @@ class RiveGenImage {

final String _assetName;

static const String package = 'example_resources';

RiveAnimation rive({
String? artboard,
List<String> animations = const [],
Expand All @@ -244,6 +270,7 @@ class RiveGenImage {
Alignment? alignment,
Widget? placeHolder,
bool antialiasing = true,
bool useArtboardSize = false,
List<RiveAnimationController> controllers = const [],
OnInitCallback? onInit,
}) {
Expand All @@ -256,6 +283,7 @@ class RiveGenImage {
alignment: alignment,
placeHolder: placeHolder,
antialiasing: antialiasing,
useArtboardSize: useArtboardSize,
controllers: controllers,
onInit: onInit,
);
Expand All @@ -271,6 +299,8 @@ class LottieGenImage {

final String _assetName;

static const String package = 'example_resources';

LottieBuilder lottie({
Animation<double>? controller,
bool? animate,
Expand All @@ -289,7 +319,8 @@ class LottieGenImage {
double? height,
BoxFit? fit,
AlignmentGeometry? alignment,
String? package = 'example_resources',
@Deprecated('Do not specify package for a generated library asset')
String? package = package,
bool? addRepaintBoundary,
FilterQuality? filterQuality,
void Function(String)? onWarning,
Expand Down
4 changes: 2 additions & 2 deletions examples/example_resources/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ description: A sample project using FlutterGen.
publish_to: 'none'

environment:
sdk: '>=2.16.0 <4.0.0'
flutter: '>=3.0.0'
sdk: '>=3.2.0 <4.0.0'
flutter: '>=3.16.0'

dependencies:
flutter:
Expand Down
2 changes: 1 addition & 1 deletion packages/command/example/lib/gen/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/example/lib/gen/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions packages/core/lib/generators/integrations/svg_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ class SvgIntegration extends Integration {
@override
List<String> get requiredImports => [
'package:flutter/widgets.dart',
'package:flutter_svg/flutter_svg.dart',
'package:flutter/services.dart',
'package:flutter_svg/flutter_svg.dart',
'package:vector_graphics/vector_graphics.dart',
];

@override
String get classOutput => _classDefinition;

String get _classDefinition => '''class SvgGenImage {
const SvgGenImage(this._assetName, {this.size = null});
const SvgGenImage(
this._assetName, {
this.size = null,
}) : _isVecFormat = false;

const SvgGenImage.vec(
this._assetName, {
this.size = null,
}) : _isVecFormat = true;

final String _assetName;
${isPackage ? "\n static const String package = '$packageName';" : ''}

final Size? size;
final bool _isVecFormat;

SvgPicture svg({
Key? key,
Expand All @@ -49,12 +59,12 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''}
@deprecated BlendMode colorBlendMode = BlendMode.srcIn,
@deprecated bool cacheColorFilter = false,
}) {
return SvgPicture.asset(
_assetName,
return SvgPicture(
_isVecFormat ?
AssetBytesLoader(_assetName, assetBundle: bundle, packageName: package) :
SvgAssetLoader(_assetName, assetBundle: bundle, packageName: package),
key: key,
matchTextDirection: matchTextDirection,
bundle: bundle,
package: package,
width: width,
height: height,
fit: fit,
Expand All @@ -64,9 +74,7 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''}
semanticsLabel: semanticsLabel,
excludeFromSemantics: excludeFromSemantics,
theme: theme,
colorFilter: colorFilter,
color: color,
colorBlendMode: colorBlendMode,
colorFilter: colorFilter ?? (color == null ? null : ColorFilter.mode(color, colorBlendMode)),
clipBehavior: clipBehavior,
cacheColorFilter: cacheColorFilter,
);
Expand All @@ -85,8 +93,11 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''}
// Query extra information about the SVG
ImageMetadata? info = parseMetadata ? _getMetadata(asset) : null;

return 'SvgGenImage(\'${asset.posixStylePath}\''
'${(info != null) ? ', size: Size(${info.width}, ${info.height})' : ''}'
final String constructorName =
asset.extension == '.vec' ? 'SvgGenImage.vec' : 'SvgGenImage';

return "$constructorName('${asset.posixStylePath}'"
"${(info != null) ? ', size: Size(${info.width}, ${info.height})' : ''}"
')';
}

Expand All @@ -107,7 +118,8 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''}
}

@override
bool isSupport(AssetType asset) => asset.mime == 'image/svg+xml';
bool isSupport(AssetType asset) =>
asset.mime == 'image/svg+xml' || asset.extension == '.vec';

@override
bool get isConstConstructor => true;
Expand Down
Loading
Loading