-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
lottie_integration.dart
131 lines (115 loc) · 3.91 KB
/
lottie_integration.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import 'package:pub_semver/pub_semver.dart';
import '../../settings/asset_type.dart';
import 'integration.dart';
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as p;
class LottieIntegration extends Integration {
LottieIntegration(String packageParameterLiteral)
: super(packageParameterLiteral);
// These are required keys for this integration.
static const lottieKeys = [
'w', // width
'h', // height
'ip', // The frame at which the Lottie animation starts at
'op', // The frame at which the Lottie animation ends at
'fr', // frame rate
'v', // // Must include version
'layers', // Must include layers
];
String get packageExpression => packageParameterLiteral.isNotEmpty
? ' = \'$packageParameterLiteral\''
: '';
@override
List<String> get requiredImports => [
'package:lottie/lottie.dart',
];
@override
String get classOutput => _classDefinition;
String get _classDefinition => '''class LottieGenImage {
const LottieGenImage(this._assetName);
final String _assetName;
LottieBuilder lottie({
Animation<double>? controller,
bool? animate,
FrameRate? frameRate,
bool? repeat,
bool? reverse,
LottieDelegates? delegates,
LottieOptions? options,
void Function(LottieComposition)? onLoaded,
LottieImageProviderFactory? imageProviderFactory,
Key? key,
AssetBundle? bundle,
Widget Function(BuildContext, Widget, LottieComposition?)? frameBuilder,
ImageErrorWidgetBuilder? errorBuilder,
double? width,
double? height,
BoxFit? fit,
AlignmentGeometry? alignment,
String? package$packageExpression,
bool? addRepaintBoundary,
FilterQuality? filterQuality,
void Function(String)? onWarning,
}) {
return Lottie.asset(
_assetName,
controller: controller,
animate: animate,
frameRate: frameRate,
repeat: repeat,
reverse: reverse,
delegates: delegates,
options: options,
onLoaded: onLoaded,
imageProviderFactory: imageProviderFactory,
key: key,
bundle: bundle,
frameBuilder: frameBuilder,
errorBuilder: errorBuilder,
width: width,
height: height,
fit: fit,
alignment: alignment,
package: package,
addRepaintBoundary: addRepaintBoundary,
filterQuality: filterQuality,
onWarning: onWarning,
);
}
String get path => _assetName;
String get keyName => ${packageParameterLiteral.isEmpty ? '_assetName' : '\'packages/$packageParameterLiteral/\$_assetName\''};
}''';
@override
String get className => 'LottieGenImage';
@override
String classInstantiate(String path) => 'LottieGenImage(\'$path\')';
@override
bool isSupport(AssetType type) => isLottieFile(type);
@override
bool get isConstConstructor => true;
bool isLottieFile(AssetType type) {
if (type.mime != 'application/json') {
return false;
}
try {
final absolutePath = p.join(type.rootPath, type.path);
String input = File(absolutePath).readAsStringSync();
final fileKeys = jsonDecode(input) as Map<String, dynamic>;
if (lottieKeys.every((key) => fileKeys.containsKey(key)) &&
fileKeys['v'] != null) {
var version = Version.parse(fileKeys['v']);
// Lottie version 4.4.0 is the first version that supports BodyMovin.
// https://github.com/xvrh/lottie-flutter/blob/0e7499d82ea1370b6acf023af570395bbb59b42f/lib/src/parser/lottie_composition_parser.dart#L60
return version >= Version(4, 4, 0);
}
} on FormatException catch (e) {
// Catches bad/corrupted json and reports it to user.
stderr.writeln(e.message);
} on TypeError catch (e) {
// Catches bad/corrupted json and reports it to user.
stderr.writeln(e);
}
return false;
}
}