Skip to content

Commit e6ce02c

Browse files
[vector_graphics] Allow transition between placeholder and loaded image to have an animation (#8195)
*Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue.* [Issue #158857](flutter/flutter#158857)
1 parent 5018fd3 commit e6ce02c

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

packages/vector_graphics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.18
2+
3+
* Allow transition between placeholder and loaded image to have an animation.
4+
15
## 1.1.17
26

37
* Reverts leaker tracker changes that caused runtime exceptions.

packages/vector_graphics/lib/src/vector_graphics.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ VectorGraphic createCompatVectorGraphic({
6161
String? semanticsLabel,
6262
bool excludeFromSemantics = false,
6363
Clip clipBehavior = Clip.hardEdge,
64+
Duration? transitionDuration,
6465
WidgetBuilder? placeholderBuilder,
6566
VectorGraphicsErrorWidget? errorBuilder,
6667
ColorFilter? colorFilter,
@@ -79,6 +80,7 @@ VectorGraphic createCompatVectorGraphic({
7980
semanticsLabel: semanticsLabel,
8081
excludeFromSemantics: excludeFromSemantics,
8182
clipBehavior: clipBehavior,
83+
transitionDuration: transitionDuration,
8284
placeholderBuilder: placeholderBuilder,
8385
errorBuilder: errorBuilder,
8486
colorFilter: colorFilter,
@@ -118,6 +120,7 @@ class VectorGraphic extends StatefulWidget {
118120
this.semanticsLabel,
119121
this.excludeFromSemantics = false,
120122
this.clipBehavior = Clip.hardEdge,
123+
this.transitionDuration,
121124
this.placeholderBuilder,
122125
this.errorBuilder,
123126
this.colorFilter,
@@ -137,6 +140,7 @@ class VectorGraphic extends StatefulWidget {
137140
this.semanticsLabel,
138141
this.excludeFromSemantics = false,
139142
this.clipBehavior = Clip.hardEdge,
143+
this.transitionDuration,
140144
this.placeholderBuilder,
141145
this.errorBuilder,
142146
this.colorFilter,
@@ -218,6 +222,9 @@ class VectorGraphic extends StatefulWidget {
218222
/// A callback that fires if some exception happens during data acquisition or decoding.
219223
final VectorGraphicsErrorWidget? errorBuilder;
220224

225+
/// Set transition duration while switching from placeholder to url image
226+
final Duration? transitionDuration;
227+
221228
/// If provided, a color filter to apply to the vector graphic when painting.
222229
///
223230
/// For example, `ColorFilter.mode(Colors.red, BlendMode.srcIn)` to give the vector
@@ -517,6 +524,19 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
517524
);
518525
}
519526

527+
if (widget.transitionDuration != null) {
528+
child = AnimatedSwitcher(
529+
duration: widget.transitionDuration!,
530+
child: child,
531+
transitionBuilder: (Widget child, Animation<double> animation) {
532+
return FadeTransition(
533+
opacity: animation,
534+
child: child,
535+
);
536+
},
537+
);
538+
}
539+
520540
if (!widget.excludeFromSemantics) {
521541
child = Semantics(
522542
container: widget.semanticsLabel != null,

packages/vector_graphics/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics
22
description: A vector graphics rendering package for Flutter using a binary encoding.
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
5-
version: 1.1.17
5+
version: 1.1.18
66

77
environment:
88
sdk: ^3.4.0

packages/vector_graphics/test/vector_graphics_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,32 @@ void main() {
314314
expect(debugLastTextDirection, TextDirection.ltr);
315315
});
316316

317+
testWidgets('Test animated switch between placeholder and image',
318+
(WidgetTester tester) async {
319+
final TestAssetBundle testBundle = TestAssetBundle();
320+
const Text placeholderWidget = Text('Placeholder');
321+
322+
await tester.pumpWidget(DefaultAssetBundle(
323+
bundle: testBundle,
324+
child: Directionality(
325+
textDirection: TextDirection.rtl,
326+
child: VectorGraphic(
327+
loader: const AssetBytesLoader('bar.svg'),
328+
placeholderBuilder: (BuildContext context) => placeholderWidget,
329+
transitionDuration: const Duration(microseconds: 500),
330+
),
331+
),
332+
));
333+
334+
expect(find.text('Placeholder'), findsOneWidget);
335+
expect(find.byType(Container), findsNothing); // No image yet
336+
337+
await tester.pumpAndSettle(const Duration(microseconds: 500));
338+
339+
expect(find.text('Placeholder'), findsNothing);
340+
expect(testBundle.loadKeys, <String>['bar.svg']);
341+
});
342+
317343
testWidgets('Can exclude from semantics', (WidgetTester tester) async {
318344
final TestAssetBundle testBundle = TestAssetBundle();
319345

0 commit comments

Comments
 (0)