-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[flutter_svg] Add SvgPicture.precompiled constructor
#10255
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
base: main
Are you sure you want to change the base?
Changes from all commits
670c8d6
26a42a9
1fceebb
5a01ae3
543f6f2
a41d8f1
f785151
5840fe2
25c85ab
2edbed6
a1f68e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ import 'src/loaders.dart'; | |
| import 'src/utilities/file.dart'; | ||
|
|
||
| export 'package:vector_graphics/vector_graphics.dart' | ||
| show BytesLoader, PictureInfo, VectorGraphicUtilities, vg; | ||
| show AssetBytesLoader, BytesLoader, PictureInfo, VectorGraphicUtilities, vg; | ||
|
|
||
| export 'src/cache.dart'; | ||
| export 'src/default_theme.dart'; | ||
|
|
@@ -446,6 +446,117 @@ class SvgPicture extends StatelessWidget { | |
| ), | ||
| colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode); | ||
|
|
||
| /// Creates a widget that renders precompiled vector graphics bytes from an | ||
| /// [AssetBundle]. | ||
| /// | ||
| /// This is a convenience wrapper around `SvgPicture(AssetBytesLoader(...))`, | ||
| /// allowing callers to pass the path to a `.vec` asset without importing | ||
| /// `package:vector_graphics/vector_graphics.dart` or manually constructing an | ||
| /// [AssetBytesLoader]. | ||
| /// | ||
| /// The asset should be generated by `package:vector_graphics_compiler`. See | ||
| /// https://pub.dev/packages/flutter_svg#precompiling-and-optimizing-svgs for | ||
| /// more details on precompiling SVGs. | ||
| /// | ||
| /// The key for caching will be derived from the `assetName`, `package`, and | ||
| /// `bundle` arguments. When providing assets from another package, the | ||
| /// `package` argument must be non-null. | ||
| /// | ||
| /// Either the [width] and [height] arguments should be specified, or the | ||
| /// widget should be placed in a context that sets tight layout constraints. | ||
| /// Otherwise, the image dimensions will change as the image is loaded, which | ||
| /// will result in ugly layout changes. | ||
| /// | ||
| /// If `matchTextDirection` is set to true, the picture will be flipped | ||
| /// horizontally in [TextDirection.rtl] contexts. | ||
| /// | ||
| /// The `allowDrawingOutsideViewBox` parameter should be used with caution - | ||
| /// if set to true, it will not clip the canvas used internally to the view box, | ||
| /// meaning the picture may draw beyond the intended area and lead to undefined | ||
| /// behavior or additional memory overhead. | ||
| /// | ||
| /// A custom `placeholderBuilder` can be specified for cases where decoding or | ||
| /// acquiring data may take a noticeably long time. | ||
| /// | ||
| /// The `color` and `colorBlendMode` arguments, if specified, will be used to set a | ||
| /// [ColorFilter] on any [Paint]s created for this drawing. | ||
| /// | ||
| /// ## Assets in packages | ||
| /// | ||
| /// To create the widget with an asset from a package, the [package] argument | ||
| /// must be provided. For instance, suppose a package called `my_icons` has | ||
| /// `icons/heart.svg.vec`. | ||
| /// | ||
| /// Then to display the image, use: | ||
| /// | ||
| /// ```dart | ||
| /// SvgPicture.precompiled('icons/heart.svg.vec', package: 'my_icons') | ||
| /// ``` | ||
| /// | ||
| /// Assets used by the package itself should also be displayed using the | ||
| /// [package] argument as above. | ||
| /// | ||
| /// If the desired asset is specified in the `pubspec.yaml` of the package, it | ||
| /// is bundled automatically with the app. In particular, assets used by the | ||
| /// package itself must be specified in its `pubspec.yaml`. | ||
| /// | ||
| /// A package can also choose to have assets in its 'lib/' folder that are not | ||
| /// specified in its `pubspec.yaml`. In this case for those images to be | ||
| /// bundled, the app has to specify which ones to include. For instance a | ||
| /// package named `fancy_backgrounds` could have: | ||
| /// | ||
| /// ```none | ||
| /// lib/backgrounds/background1.svg.vec | ||
| /// lib/backgrounds/background2.svg.vec | ||
| /// lib/backgrounds/background3.svg.vec | ||
| ///``` | ||
| /// | ||
| /// To include, say the first image, the `pubspec.yaml` of the app should | ||
| /// specify it in the assets section: | ||
| /// | ||
| /// ```yaml | ||
| /// assets: | ||
| /// - packages/fancy_backgrounds/backgrounds/background1.svg.vec | ||
| /// ``` | ||
| /// | ||
| /// The `lib/` is implied, so it should not be included in the asset path. | ||
| /// | ||
| /// | ||
| /// See also: | ||
| /// | ||
| /// * <https://flutter.io/assets-and-images/>, an introduction to assets in | ||
| /// Flutter. | ||
| /// | ||
| /// If [excludeFromSemantics] is true, then [semanticsLabel] will be ignored. | ||
|
Comment on lines
+465
to
+530
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: Copied this part from |
||
| SvgPicture.precompiled( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you fine with the name |
||
| String assetName, { | ||
| super.key, | ||
| this.matchTextDirection = false, | ||
| AssetBundle? bundle, | ||
| String? package, | ||
| this.width, | ||
| this.height, | ||
| this.fit = BoxFit.contain, | ||
| this.alignment = Alignment.center, | ||
| this.allowDrawingOutsideViewBox = false, | ||
| this.placeholderBuilder, | ||
| this.semanticsLabel, | ||
| this.excludeFromSemantics = false, | ||
| this.clipBehavior = Clip.hardEdge, | ||
| this.errorBuilder, | ||
| ui.ColorFilter? colorFilter, | ||
| @Deprecated('Use colorFilter instead.') ui.Color? color, | ||
| @Deprecated('Use colorFilter instead.') | ||
|
Comment on lines
+548
to
+549
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add the |
||
| ui.BlendMode colorBlendMode = ui.BlendMode.srcIn, | ||
| @Deprecated('This no longer does anything.') bool cacheColorFilter = false, | ||
| this.renderingStrategy = RenderingStrategy.picture, | ||
| }) : bytesLoader = AssetBytesLoader( | ||
| assetName, | ||
| packageName: package, | ||
| assetBundle: bundle, | ||
| ), | ||
| colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode); | ||
|
|
||
| static ColorFilter? _getColorFilter( | ||
| ui.Color? color, | ||
| ui.BlendMode colorBlendMode, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: Not needed anymore