Skip to content
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
3 changes: 2 additions & 1 deletion packages/vector_graphics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 1.1.20

* Updates minimum supported SDK version to Flutter 3.35/Dart 3.9.
* Respect BoxFit parameter when viewbox is specified.

## 1.1.19

Expand Down
2 changes: 1 addition & 1 deletion packages/vector_graphics/lib/src/listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'loader.dart';

const VectorGraphicsCodec _codec = VectorGraphicsCodec();

/// The deocded result of a vector graphics asset.
/// The decoded result of a vector graphics asset.
class PictureInfo {
/// Construct a new [PictureInfo].
PictureInfo._(this.picture, this.size);
Expand Down
57 changes: 27 additions & 30 deletions packages/vector_graphics/lib/src/vector_graphics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class _PictureKey {
}

class _VectorGraphicWidgetState extends State<VectorGraphic> {
_PictureData? _pictureInfo;
_PictureData? _pictureData;
Object? _error;
StackTrace? _stackTrace;
Locale? locale;
Expand Down Expand Up @@ -338,8 +338,8 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {

@override
void dispose() {
_maybeReleasePicture(_pictureInfo);
_pictureInfo = null;
_maybeReleasePicture(_pictureData);
_pictureData = null;
super.dispose();
}

Expand Down Expand Up @@ -407,8 +407,8 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
if (data != null) {
data.count += 1;
setState(() {
_maybeReleasePicture(_pictureInfo);
_pictureInfo = data;
_maybeReleasePicture(_pictureData);
_pictureData = data;
});
return;
}
Expand All @@ -431,8 +431,8 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
}

setState(() {
_maybeReleasePicture(_pictureInfo);
_pictureInfo = data;
_maybeReleasePicture(_pictureData);
_pictureData = data;
});
} catch (error, stackTrace) {
_handleError(error, stackTrace);
Expand All @@ -443,7 +443,7 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {

@override
Widget build(BuildContext context) {
final PictureInfo? pictureInfo = _pictureInfo?.pictureInfo;
final PictureInfo? pictureInfo = _pictureData?.pictureInfo;

Widget child;
if (pictureInfo != null) {
Expand All @@ -454,42 +454,39 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
double? width = widget.width;
double? height = widget.height;

if (width == null && height == null) {
width = pictureInfo.size.width;
height = pictureInfo.size.height;
} else if (height != null && !pictureInfo.size.isEmpty) {
if (height != null && !pictureInfo.size.isEmpty) {
width = height / pictureInfo.size.height * pictureInfo.size.width;
} else if (width != null && !pictureInfo.size.isEmpty) {
height = width / pictureInfo.size.width * pictureInfo.size.height;
}

assert(width != null && height != null);

var scale = 1.0;
scale = math.min(
pictureInfo.size.width / width!,
pictureInfo.size.height / height!,
);
if (width != null && height != null) {
scale = math.min(
pictureInfo.size.width / width,
pictureInfo.size.height / height,
);
}

if (_webRenderObject) {
child = _RawWebVectorGraphicWidget(
pictureInfo: pictureInfo,
assetKey: _pictureInfo!.key,
assetKey: _pictureData!.key,
colorFilter: widget.colorFilter,
opacity: widget.opacity,
);
} else if (widget.strategy == RenderingStrategy.raster) {
child = _RawVectorGraphicWidget(
pictureInfo: pictureInfo,
assetKey: _pictureInfo!.key,
assetKey: _pictureData!.key,
colorFilter: widget.colorFilter,
opacity: widget.opacity,
scale: scale,
);
} else {
child = _RawPictureVectorGraphicWidget(
pictureInfo: pictureInfo,
assetKey: _pictureInfo!.key,
assetKey: _pictureData!.key,
colorFilter: widget.colorFilter,
opacity: widget.opacity,
);
Expand All @@ -507,16 +504,16 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
}
}

child = SizedBox(
width: width,
height: height,
child: FittedBox(
fit: widget.fit,
alignment: widget.alignment,
clipBehavior: widget.clipBehavior,
child: SizedBox.fromSize(size: pictureInfo.size, child: child),
),
child = FittedBox(
fit: widget.fit,
alignment: widget.alignment,
clipBehavior: widget.clipBehavior,
child: SizedBox.fromSize(size: pictureInfo.size, child: child),
);

if (width != null && height != null) {
child = SizedBox(width: width, height: height, child: child);
}
} else if (_error != null && widget.errorBuilder != null) {
child = widget.errorBuilder!(
context,
Expand Down
2 changes: 1 addition & 1 deletion packages/vector_graphics/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: vector_graphics
description: A vector graphics rendering package for Flutter using a binary encoding.
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
version: 1.1.19
version: 1.1.20

environment:
sdk: ^3.9.0
Expand Down
95 changes: 90 additions & 5 deletions packages/vector_graphics/test/vector_graphics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,91 @@ void main() {
expect(fittedBox.clipBehavior, Clip.hardEdge);
});

group('BoxFit', () {
Future<(RenderBox, RenderBox)> setupBoxFitVectorGraphic(
WidgetTester tester, {
required BoxFit fit,
}) async {
final buffer = VectorGraphicsBuffer();
codec.writeSize(buffer, 100, 50);

await tester.pumpWidget(
RepaintBoundary(
child: Center(
child: SizedBox(
width: 200,
height: 200,
child: VectorGraphic(
fit: fit,
loader: TestBytesLoader(buffer.done()),
),
),
),
),
);
await tester.pumpAndSettle();

final RenderProxyBox outsideBox = tester.renderObject(
find.byType(VectorGraphic),
);
expect(outsideBox.size, const Size(200, 200));

final RenderBox insideBox = tester.renderObject(
find.byType(SizedBox).last,
);
expect(insideBox.size, const Size(100, 50));

return (outsideBox, insideBox);
}

testWidgets(
'Scale on BoxFit.contain without constraints, but with viewbox',
(WidgetTester tester) async {
final (RenderBox outsideBox, RenderBox insideBox) =
await setupBoxFitVectorGraphic(tester, fit: BoxFit.contain);

// Top left point as offset in child space
final Offset insidePoint = insideBox.localToGlobal(Offset.zero);
// Top left point as offset in parent space
final Offset outsidePoint = outsideBox.localToGlobal(
const Offset(0, 50),
);

expect(insidePoint, equals(outsidePoint));
},
);

testWidgets('Scale on BoxFit.cover without constraints, but with viewbox', (
WidgetTester tester,
) async {
final (RenderBox outsideBox, RenderBox insideBox) =
await setupBoxFitVectorGraphic(tester, fit: BoxFit.cover);

// Top left point as offset in child space
final Offset insidePoint = insideBox.localToGlobal(Offset.zero);
// Top left point as offset in parent space
final Offset outsidePoint = outsideBox.localToGlobal(
const Offset(-100, 0),
);

expect(insidePoint, equals(outsidePoint));
});

testWidgets('Scale on BoxFit.fill without constraints, but with viewbox', (
WidgetTester tester,
) async {
final (RenderBox outsideBox, RenderBox insideBox) =
await setupBoxFitVectorGraphic(tester, fit: BoxFit.fill);

// Top left point as offset in child space
final Offset insidePoint = insideBox.localToGlobal(Offset.zero);
// Top left point as offset in parent space
final Offset outsidePoint = outsideBox.localToGlobal(Offset.zero);

expect(insidePoint, equals(outsidePoint));
});
});

group('ClipBehavior', () {
testWidgets('Sets clipBehavior to hardEdge if not provided', (
WidgetTester tester,
Expand Down Expand Up @@ -209,9 +294,9 @@ void main() {
);
await tester.pumpAndSettle();

expect(find.byType(SizedBox), findsNWidgets(2));
expect(find.byType(SizedBox), findsNWidgets(1));

final sizedBox = find.byType(SizedBox).evaluate().last.widget as SizedBox;
final sizedBox = find.byType(SizedBox).evaluate().single.widget as SizedBox;

expect(sizedBox.width, 100);
expect(sizedBox.height, 200);
Expand Down Expand Up @@ -601,7 +686,7 @@ void main() {
// A blank image, because the image hasn't loaded yet.
await expectLater(
find.byKey(key),
matchesGoldenFile('vg_with_image_blank.png'),
matchesGoldenFile('goldens/vg_with_image_blank.png'),
);

expect(imageCache.currentSize, 1);
Expand All @@ -615,10 +700,10 @@ void main() {
expect(imageCache.statusForKey(imageKey).live, false);
expect(imageCache.statusForKey(imageKey).keepAlive, true);

// A blue square, becuase the image is available now.
// A blue square, because the image is available now.
await expectLater(
find.byKey(key),
matchesGoldenFile('vg_with_image_blue.png'),
matchesGoldenFile('goldens/vg_with_image_blue.png'),
);
}, skip: kIsWeb);

Expand Down
Loading