Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions packages/vector_graphics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.16

* Allow transition between placeholder and loaded image to have an animation.

## 1.1.15

* Updates error handling in VectorGraphicWidget to handle errors when the bytes of the graphic cannot be loaded.
Expand Down
30 changes: 25 additions & 5 deletions packages/vector_graphics/lib/src/vector_graphics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ VectorGraphic createCompatVectorGraphic({
String? semanticsLabel,
bool excludeFromSemantics = false,
Clip clipBehavior = Clip.hardEdge,
Duration? transitionDuration,
WidgetBuilder? placeholderBuilder,
VectorGraphicsErrorWidget? errorBuilder,
ColorFilter? colorFilter,
Expand All @@ -79,6 +80,7 @@ VectorGraphic createCompatVectorGraphic({
semanticsLabel: semanticsLabel,
excludeFromSemantics: excludeFromSemantics,
clipBehavior: clipBehavior,
transitionDuration: transitionDuration,
placeholderBuilder: placeholderBuilder,
errorBuilder: errorBuilder,
colorFilter: colorFilter,
Expand Down Expand Up @@ -118,6 +120,7 @@ class VectorGraphic extends StatefulWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.transitionDuration,
this.placeholderBuilder,
this.errorBuilder,
this.colorFilter,
Expand All @@ -137,6 +140,7 @@ class VectorGraphic extends StatefulWidget {
this.semanticsLabel,
this.excludeFromSemantics = false,
this.clipBehavior = Clip.hardEdge,
this.transitionDuration,
this.placeholderBuilder,
this.errorBuilder,
this.colorFilter,
Expand Down Expand Up @@ -218,6 +222,9 @@ class VectorGraphic extends StatefulWidget {
/// A callback that fires if some exception happens during data acquisition or decoding.
final VectorGraphicsErrorWidget? errorBuilder;

/// Set transition duration while switching from placeholder to url image
final Duration? transitionDuration;

/// If provided, a color filter to apply to the vector graphic when painting.
///
/// For example, `ColorFilter.mode(Colors.red, BlendMode.srcIn)` to give the vector
Expand Down Expand Up @@ -510,11 +517,24 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
_stackTrace ?? StackTrace.empty,
);
} else {
child = widget.placeholderBuilder?.call(context) ??
SizedBox(
width: widget.width,
height: widget.height,
);
child = widget.placeholderBuilder != null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the double nested ternary please right a helper function and/or some if statements.

? widget.transitionDuration != null
? AnimatedSwitcher(
duration: widget.transitionDuration!,
child: widget.placeholderBuilder!.call(context),
transitionBuilder:
(Widget child, Animation<double> animation) {
return FadeTransition(
opacity: animation,
child: child,
);
},
)
: widget.placeholderBuilder!.call(context)
: SizedBox(
width: widget.width,
height: widget.height,
);
}

if (!widget.excludeFromSemantics) {
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.15
version: 1.1.16

environment:
sdk: ^3.4.0
Expand Down
26 changes: 26 additions & 0 deletions packages/vector_graphics/test/vector_graphics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,32 @@ void main() {
expect(debugLastTextDirection, TextDirection.ltr);
});

testWidgets('Test animated switch between placeholder and image',
(WidgetTester tester) async {
final TestAssetBundle testBundle = TestAssetBundle();
const Text placeholderWidget = Text('Placeholder');

await tester.pumpWidget(DefaultAssetBundle(
bundle: testBundle,
child: Directionality(
textDirection: TextDirection.rtl,
child: VectorGraphic(
loader: const AssetBytesLoader('bar.svg'),
placeholderBuilder: (BuildContext context) => placeholderWidget,
transitionDuration: const Duration(microseconds: 500),
),
),
));

expect(find.text('Placeholder'), findsOneWidget);
expect(find.byType(Container), findsNothing); // No image yet

await tester.pumpAndSettle(const Duration(microseconds: 500));

expect(find.text('Placeholder'), findsNothing);
expect(testBundle.loadKeys, <String>['bar.svg']);
});

testWidgets('Can exclude from semantics', (WidgetTester tester) async {
final TestAssetBundle testBundle = TestAssetBundle();

Expand Down
Loading