-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathwarp_indicator.dart
311 lines (270 loc) · 8.44 KB
/
warp_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
enum WarpAnimationState {
stopped,
playing,
}
typedef StarColorGetter = Color Function(int index);
class WarpIndicator extends StatefulWidget {
final Widget child;
final int starsCount;
final AsyncCallback onRefresh;
final IndicatorController? controller;
final Color skyColor;
final StarColorGetter starColorGetter;
final Key? indicatorKey;
const WarpIndicator({
super.key,
this.indicatorKey,
this.controller,
required this.onRefresh,
required this.child,
this.starsCount = 30,
this.skyColor = Colors.black,
this.starColorGetter = _defaultStarColorGetter,
});
static Color _defaultStarColorGetter(int index) =>
HSLColor.fromAHSL(1, Random().nextDouble() * 360, 1, 0.98).toColor();
@override
State<WarpIndicator> createState() => _WarpIndicatorState();
}
class _WarpIndicatorState extends State<WarpIndicator>
with SingleTickerProviderStateMixin {
static const _indicatorSize = 150.0;
final _random = Random();
WarpAnimationState _state = WarpAnimationState.stopped;
List<Star> stars = [];
final _offsetTween = Tween<Offset>(
begin: Offset.zero,
end: Offset.zero,
);
final _angleTween = Tween<double>(
begin: 0,
end: 0,
);
late AnimationController shakeController;
static final _scaleTween = Tween(begin: 1.0, end: 0.75);
static final _radiusTween = Tween(begin: 0.0, end: 16.0);
@override
void initState() {
shakeController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 100),
);
super.initState();
}
Offset _getRandomOffset() => Offset(
_random.nextInt(10) - 5,
_random.nextInt(10) - 5,
);
double _getRandomAngle() {
final degrees = ((_random.nextDouble() * 2) - 1);
final radians = degrees == 0 ? 0.0 : degrees / 360.0;
return radians;
}
void _shiftAndGenerateRandomShakeTransform() {
_offsetTween.begin = _offsetTween.end;
_offsetTween.end = _getRandomOffset();
_angleTween.begin = _angleTween.end;
_angleTween.end = _getRandomAngle();
}
void _startShakeAnimation() {
_shiftAndGenerateRandomShakeTransform();
shakeController.animateTo(1.0);
_state = WarpAnimationState.playing;
stars = List.generate(
widget.starsCount,
(index) => Star(initialColor: widget.starColorGetter(index)),
);
}
void _resetShakeAnimation() {
_shiftAndGenerateRandomShakeTransform();
shakeController.value = 0.0;
shakeController.animateTo(1.0);
}
void _stopShakeAnimation() {
_offsetTween.end = Offset.zero;
_angleTween.end = 0.0;
_state = WarpAnimationState.stopped;
_shiftAndGenerateRandomShakeTransform();
shakeController.stop();
shakeController.value = 0.0;
stars = [];
}
@override
Widget build(BuildContext context) {
return CustomRefreshIndicator(
key: widget.indicatorKey,
controller: widget.controller,
offsetToArmed: _indicatorSize,
leadingScrollIndicatorVisible: false,
trailingScrollIndicatorVisible: false,
onRefresh: widget.onRefresh,
autoRebuild: false,
onStateChanged: (change) {
if (change.didChange(to: IndicatorState.loading)) {
_startShakeAnimation();
} else if (change.didChange(to: IndicatorState.idle)) {
_stopShakeAnimation();
}
},
child: widget.child,
builder: (
BuildContext context,
Widget child,
IndicatorController controller,
) {
final animation = Listenable.merge([controller, shakeController]);
return Stack(
children: <Widget>[
AnimatedBuilder(
animation: shakeController,
builder: (_, __) {
return LayoutBuilder(
builder:
(BuildContext context, BoxConstraints constraints) {
return CustomPaint(
painter: Sky(
stars: stars,
color: widget.skyColor,
),
child: const SizedBox.expand(),
);
},
);
}),
AnimatedBuilder(
animation: animation,
builder: (context, _) {
return Transform.scale(
scale: _scaleTween.transform(controller.value),
child: Builder(builder: (context) {
if (shakeController.value == 1.0 &&
_state == WarpAnimationState.playing) {
SchedulerBinding.instance
.addPostFrameCallback((_) => _resetShakeAnimation());
}
return Transform.rotate(
angle: _angleTween.transform(shakeController.value),
child: Transform.translate(
offset: _offsetTween.transform(shakeController.value),
child: ClipRRect(
borderRadius: BorderRadius.circular(
_radiusTween.transform(controller.value),
),
child: child,
),
),
);
}),
);
},
),
],
);
},
);
}
@override
void dispose() {
shakeController.dispose();
super.dispose();
}
}
class Star {
Offset? position;
Color? color;
double value;
late Offset speed;
final Color initialColor;
late double angle;
Star({
required this.initialColor,
}) : value = 0.0;
static const _minOpacity = 0.1;
static const _maxOpacity = 1.0;
void _init(Rect rect) {
position = rect.center;
value = 0.0;
final random = Random();
angle = random.nextDouble() * pi * 3;
speed = Offset(cos(angle), sin(angle));
const minSpeedScale = 20;
const maxSpeedScale = 35;
final speedScale = minSpeedScale +
random.nextInt(maxSpeedScale - minSpeedScale).toDouble();
speed = speed.scale(
speedScale,
speedScale,
);
final t = speedScale / maxSpeedScale;
final opacity = _minOpacity + (_maxOpacity - _minOpacity) * t;
color = initialColor.withOpacity(opacity);
}
draw(Canvas canvas, Rect rect) {
if (position == null) {
_init(rect);
}
value++;
final startPosition = Offset(position!.dx, position!.dy);
final endPosition = position! + (speed * (value * 0.3));
position = speed + position!;
final paint = Paint()..color = color!;
final startShiftAngle = angle + (pi / 2);
final startShift = Offset(cos(startShiftAngle), sin(startShiftAngle));
final shiftedStartPosition =
startPosition + (startShift * (0.75 + value * 0.01));
final endShiftAngle = angle + (pi / 2);
final endShift = Offset(cos(endShiftAngle), sin(endShiftAngle));
final shiftedEndPosition = endPosition + (endShift * (1.5 + value * 0.01));
final path = Path()
..moveTo(startPosition.dx, startPosition.dy)
..lineTo(startPosition.dx, startPosition.dy)
..lineTo(shiftedStartPosition.dx, shiftedStartPosition.dy)
..lineTo(shiftedEndPosition.dx, shiftedEndPosition.dy)
..lineTo(endPosition.dx, endPosition.dy);
if (!rect.contains(startPosition)) {
_init(rect);
}
canvas.drawPath(path, paint);
}
}
class Sky extends CustomPainter {
final List<Star> stars;
final Color color;
Sky({
required this.stars,
required this.color,
});
@override
void paint(Canvas canvas, Size size) {
var rect = Offset.zero & size;
canvas.drawRect(rect, Paint()..color = color);
for (final star in stars) {
star.draw(canvas, rect);
}
}
@override
SemanticsBuilderCallback get semanticsBuilder {
return (Size size) {
var rect = Offset.zero & size;
return [
CustomPainterSemantics(
rect: rect,
properties: const SemanticsProperties(
label: 'light speed animation.',
textDirection: TextDirection.ltr,
),
),
];
};
}
@override
bool shouldRepaint(Sky oldDelegate) => true;
@override
bool shouldRebuildSemantics(Sky oldDelegate) => false;
}