-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrop.dart
98 lines (83 loc) · 2.08 KB
/
drop.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
import 'package:liquify/liquify.dart';
void main() {
// Create an instance of ProductDrop
final product = ProductDrop(
name: 'Smartphone',
price: 599.99,
manufacturer: ManufacturerDrop(
name: 'TechCorp',
country: 'Japan',
),
);
FilterRegistry.register('discounted_price', (input, args, namedArgs) {
if (input is! num || args.isEmpty || args[0] is! num) {
return input;
}
num price = input;
num discountPercentage = args[0];
if (discountPercentage < 0 || discountPercentage > 100) {
return price;
}
return price - (price * discountPercentage / 100);
});
// Define and render templates
final templates = [
'{{ product.name }} costs \${{ product.price }}',
'Manufacturer: {{ product.manufacturer.name }}',
'Country of origin: {{ product.manufacturer.country }}',
'Discounted price: {{ product.price | discounted_price: 10 }}',
'Is expensive? {{ product.is_expensive }}',
];
for (final template in templates) {
final result = Template.parse(
template,
data: {
'product': product,
},
);
print(result.render());
}
}
class ProductDrop extends Drop {
final String name;
final double price;
final ManufacturerDrop manufacturer;
ProductDrop({
required this.name,
required this.price,
required this.manufacturer,
});
@override
Map<String, dynamic> get attrs => {
'name': name,
'price': price,
'manufacturer': manufacturer,
};
@override
List<Symbol> get invokable => [
...super.invokable,
#is_expensive,
];
@override
invoke(Symbol symbol, [List<dynamic>? args]) {
switch (symbol) {
case #is_expensive:
return price > 500 ? 'Yes' : 'No';
default:
return liquidMethodMissing(symbol);
}
}
}
class ManufacturerDrop extends Drop {
final String name;
final String country;
ManufacturerDrop({
required this.name,
required this.country,
});
@override
Map<String, dynamic> get attrs => {
'name': name,
'country': country,
};
}