-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathice_cream_indicator.dart
175 lines (160 loc) · 4.92 KB
/
ice_cream_indicator.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import 'dart:async';
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
import 'package:flutter/material.dart';
class ParallaxConfig {
final int level;
final AssetImage image;
const ParallaxConfig({
required this.level,
required this.image,
});
}
class IceCreamIndicator extends StatefulWidget {
final Widget child;
final IndicatorController? controller;
const IceCreamIndicator({
super.key,
required this.child,
this.controller,
});
@override
State<IceCreamIndicator> createState() => _IceCreamIndicatorState();
}
class _IceCreamIndicatorState extends State<IceCreamIndicator>
with SingleTickerProviderStateMixin {
static const _assets = <ParallaxConfig>[
ParallaxConfig(
image: AssetImage("assets/ice_cream_indicator/cup2.png"),
level: 0,
),
ParallaxConfig(
image: AssetImage("assets/ice_cream_indicator/spoon.png"),
level: 1,
),
ParallaxConfig(
image: AssetImage("assets/ice_cream_indicator/ice1.png"),
level: 3,
),
ParallaxConfig(
image: AssetImage("assets/ice_cream_indicator/ice3.png"),
level: 4,
),
ParallaxConfig(
image: AssetImage("assets/ice_cream_indicator/ice2.png"),
level: 2,
),
ParallaxConfig(
image: AssetImage("assets/ice_cream_indicator/cup.png"),
level: 0,
),
ParallaxConfig(
image: AssetImage("assets/ice_cream_indicator/mint.png"),
level: 5,
),
];
static const _indicatorSize = 150.0;
static const _imageSize = 140.0;
late AnimationController _spoonController;
static final _spoonTween = CurveTween(curve: Curves.easeInOut);
@override
void initState() {
_spoonController =
AnimationController(vsync: this, duration: const Duration(seconds: 1));
WidgetsBinding.instance.addPostFrameCallback((_) => _precacheImages());
super.initState();
}
void _precacheImages() {
for (final config in _assets) {
unawaited(precacheImage(config.image, context));
}
}
Widget _buildImage(IndicatorController controller, ParallaxConfig asset) {
return Transform.translate(
offset: Offset(
0,
-(asset.level * (controller.value.clamp(1.0, 1.5) - 1.0) * 20) + 10,
),
child: OverflowBox(
maxHeight: _imageSize,
minHeight: _imageSize,
child: Image(
image: asset.image,
fit: BoxFit.contain,
height: _imageSize,
),
),
);
}
@override
Widget build(BuildContext context) {
return CustomRefreshIndicator(
controller: widget.controller,
offsetToArmed: _indicatorSize,
onRefresh: () => Future.delayed(const Duration(seconds: 4)),
autoRebuild: false,
child: widget.child,
onStateChanged: (change) {
if (change.didChange(to: IndicatorState.loading)) {
_spoonController.repeat(reverse: true);
} else if (change.didChange(from: IndicatorState.loading)) {
_spoonController.stop();
} else if (change.didChange(to: IndicatorState.idle)) {
_spoonController.value = 0.0;
}
},
builder: (
BuildContext context,
Widget child,
IndicatorController controller,
) {
return Stack(
children: <Widget>[
AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget? _) {
return SizedBox(
height: controller.value * _indicatorSize,
child: Stack(
children: <Widget>[
for (int i = 0; i < _assets.length; i++)
/// checking for spoon build animation and attaching the spoon controller
if (i == 1)
AnimatedBuilder(
animation: _spoonController,
child: _buildImage(controller, _assets[i]),
builder: (context, child) {
return Transform.rotate(
angle: (-_spoonTween
.transform(_spoonController.value)) *
1.25,
child: child,
);
},
)
else
_buildImage(controller, _assets[i])
],
),
);
},
),
AnimatedBuilder(
builder: (context, _) {
return Transform.translate(
offset: Offset(0.0, controller.value * _indicatorSize),
child: child,
);
},
animation: controller,
),
],
);
},
);
}
@override
void dispose() {
_spoonController.dispose();
super.dispose();
}
}