diff --git a/example/lib/custom_asset_loading.dart b/example/lib/custom_asset_loading.dart index 7569b1d0..560df9bb 100644 --- a/example/lib/custom_asset_loading.dart +++ b/example/lib/custom_asset_loading.dart @@ -89,6 +89,7 @@ class _RiveRandomImageState extends State<_RiveRandomImage> { return true; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() { @@ -149,6 +150,7 @@ class _RiveRandomFontState extends State<_RiveRandomFont> { return true; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() { diff --git a/example/lib/custom_cached_asset_loading.dart b/example/lib/custom_cached_asset_loading.dart index 406f46d0..98a0a092 100644 --- a/example/lib/custom_cached_asset_loading.dart +++ b/example/lib/custom_cached_asset_loading.dart @@ -174,6 +174,7 @@ class __RiveRandomCachedImageState extends State<_RiveRandomCachedImage> { return false; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() => _riveImageSampleFile = imageFile); @@ -248,6 +249,7 @@ class __RiveRandomCachedFontState extends State<_RiveRandomCachedFont> { return false; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() { diff --git a/lib/src/rive_file.dart b/lib/src/rive_file.dart index c3928dc4..d5abbfda 100644 --- a/lib/src/rive_file.dart +++ b/lib/src/rive_file.dart @@ -309,14 +309,12 @@ class RiveFile { /// file - as set in the editor. static Future 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, diff --git a/lib/src/widgets/rive_animation.dart b/lib/src/widgets/rive_animation.dart index cffc2606..83a31571 100644 --- a/lib/src/widgets/rive_animation.dart +++ b/lib/src/widgets/rive_animation.dart @@ -173,7 +173,7 @@ class RiveAnimationState extends State { @override void initState() { super.initState(); - _configure(); + WidgetsBinding.instance.addPostFrameCallback((_) => _configure()); } /// Loads [RiveFile] and calls [_init] @@ -189,6 +189,7 @@ class RiveAnimationState extends State { case _Source.asset: return RiveFile.asset( widget.name!, + bundle: DefaultAssetBundle.of(context), ); case _Source.network: return RiveFile.network( diff --git a/test/asset_test.dart b/test/asset_test.dart index ef401495..c558eb74 100644 --- a/test/asset_test.dart +++ b/test/asset_test.dart @@ -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); + }); }); } diff --git a/test/mocks/mocks.dart b/test/mocks/mocks.dart index 64a8b8de..7778b326 100644 --- a/test/mocks/mocks.dart +++ b/test/mocks/mocks.dart @@ -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'; @@ -8,3 +9,5 @@ abstract class _OnInitFunction { } class OnInitCallbackMock extends Mock implements _OnInitFunction {} + +class MockAssetBundle extends Mock implements AssetBundle {}