Skip to content
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

use asset bundle of inherited widget instead of root #330

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions example/lib/custom_asset_loading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class _RiveRandomImageState extends State<_RiveRandomImage> {
return true;
},
),
bundle: DefaultAssetBundle.of(context),
);

setState(() {
Expand Down Expand Up @@ -149,6 +150,7 @@ class _RiveRandomFontState extends State<_RiveRandomFont> {
return true;
},
),
bundle: DefaultAssetBundle.of(context),
);

setState(() {
Expand Down
2 changes: 2 additions & 0 deletions example/lib/custom_cached_asset_loading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class __RiveRandomCachedImageState extends State<_RiveRandomCachedImage> {
return false;
},
),
bundle: DefaultAssetBundle.of(context),
);

setState(() => _riveImageSampleFile = imageFile);
Expand Down Expand Up @@ -248,6 +249,7 @@ class __RiveRandomCachedFontState extends State<_RiveRandomCachedFont> {
return false;
},
),
bundle: DefaultAssetBundle.of(context),
);

setState(() {
Expand Down
6 changes: 2 additions & 4 deletions lib/src/rive_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,12 @@ class RiveFile {
/// file - as set in the editor.
static Future<RiveFile> asset(
String bundleKey, {
AssetBundle? bundle,
required AssetBundle bundle,
FileAssetLoader? assetLoader,
bool loadCdnAssets = true,
bool loadEmbeddedAssets = true,
}) async {
final bytes = await (bundle ?? rootBundle).load(
bundleKey,
);
final bytes = await bundle.load(bundleKey);

return RiveFile.import(
bytes,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/widgets/rive_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class RiveAnimationState extends State<RiveAnimation> {
@override
void initState() {
super.initState();
_configure();
WidgetsBinding.instance.addPostFrameCallback((_) => _configure());
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is the main concern with this PR.

Presumably this pushes the first frame of the animation back a render cycle? I am guessing this is to ensure that the context has been loaded before loadRiveFile is executed?

}

/// Loads [RiveFile] and calls [_init]
Expand All @@ -189,6 +189,7 @@ class RiveAnimationState extends State<RiveAnimation> {
case _Source.asset:
return RiveFile.asset(
widget.name!,
bundle: DefaultAssetBundle.of(context),
);
case _Source.network:
return RiveFile.network(
Expand Down
19 changes: 19 additions & 0 deletions test/asset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,24 @@ void main() {
'https://public.uat.rive.app/cdn/uuid/69a03ce3-83f0-4fcb-94a5-0d401b8c030e'),
)).called(1);
});

testWidgets('Uses AssetBundle of context instead of rootBundle',
(WidgetTester tester) async {
await HttpOverrides.runZoned(() async {
final assetBundle = MockAssetBundle();
final riveBytes = loadFile('assets/image_asset_uat.riv');

when(() => assetBundle.load(any())).thenAnswer((_) async => riveBytes);

await tester.pumpWidget(
DefaultAssetBundle(
bundle: assetBundle,
child: const RiveAnimation.asset('assets/image_asset_uat.riv'),
),
);

verify(() => assetBundle.load(any())).called(1);
}, createHttpClient: (_) => mockHttpClient);
});
});
}
3 changes: 3 additions & 0 deletions test/mocks/mocks.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/widgets.dart';
import 'package:mocktail/mocktail.dart';
import 'package:rive/src/rive_core/artboard.dart';
export 'fakes.dart';
Expand All @@ -8,3 +9,5 @@ abstract class _OnInitFunction {
}

class OnInitCallbackMock extends Mock implements _OnInitFunction {}

class MockAssetBundle extends Mock implements AssetBundle {}