-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrive_animation_service.dart
42 lines (36 loc) · 1.29 KB
/
rive_animation_service.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Flutter imports:
import 'package:flutter/services.dart';
// Package imports:
import 'package:rive/rive.dart';
// Project imports:
import 'package:notredame/core/utils/animation_exception.dart';
/// Manage the rive animation for the application
class RiveAnimationService {
Future<Artboard> loadRiveFile({required String riveFileName}) async {
final bytes = await rootBundle.load("assets/animations/$riveFileName.riv");
try {
final file = RiveFile.import(bytes);
// get the main artboard
return file.mainArtboard;
} on Exception {
throw const AnimationException(
prefix: "loadRiveFile",
message:
"Impossible to load the rive file. The file is most likely not found or corrupted. Check if the file is located in /assets/animations/*.riv");
}
}
void addControllerToAnimation(
{required Artboard artboard,
RiveAnimationController<dynamic>? controller}) {
try {
controller ??= SimpleAnimation(artboard.animations[0].name);
artboard.addController(controller);
} catch (e) {
throw AnimationException(
prefix: "addControllerToAnimation",
message:
"The animation controller was not able to attach to the artboard.",
nestedException: e as Exception);
}
}
}