forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_refresh_indicator.dart
More file actions
284 lines (246 loc) · 8.7 KB
/
Copy pathcustom_refresh_indicator.dart
File metadata and controls
284 lines (246 loc) · 8.7 KB
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
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class CustomRefreshIndicator extends StatefulWidget {
final Widget child;
final Future<void> Function() onRefresh;
final double triggerDistance;
final double minDragStartThreshold;
const CustomRefreshIndicator({
Key? key,
required this.child,
required this.onRefresh,
this.triggerDistance = 120.0,
this.minDragStartThreshold = 60.0,
}) : super(key: key);
@override
State<CustomRefreshIndicator> createState() => _CustomRefreshIndicatorState();
}
class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator> with TickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
double _dragOffset = 0.0;
double _totalDragDistance = 0.0;
bool _isRefreshing = false;
bool _canRefresh = false;
int _previousFilledDots = 0;
@override
void initState() {
super.initState();
_controller = AnimationController(duration: const Duration(milliseconds: 1200), vsync: this);
_animation = Tween<double>(
begin: 0.0,
end: 1.0,
).animate(CurvedAnimation(parent: _controller, curve: Curves.linear));
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _handleScrollNotification(ScrollNotification notification) {
if (_isRefreshing) return;
if (notification is ScrollUpdateNotification) {
final ScrollMetrics metrics = notification.metrics;
// Check if we're at the top and pulling down
if (metrics.pixels <= 0 && notification.scrollDelta! < 0) {
setState(() {
_totalDragDistance = math.min(
widget.triggerDistance + widget.minDragStartThreshold,
_totalDragDistance + (-notification.scrollDelta!),
);
// Only show visual feedback after minimum threshold is exceeded
if (_totalDragDistance > widget.minDragStartThreshold) {
_dragOffset = _totalDragDistance - widget.minDragStartThreshold;
_canRefresh = _dragOffset >= widget.triggerDistance;
} else {
_dragOffset = 0.0;
_canRefresh = false;
}
});
if (_dragOffset > 0) {
_checkForHapticFeedback();
}
} else if (metrics.pixels > 0 && (_dragOffset > 0 || _totalDragDistance > 0)) {
// Reset if user scrolls back up
_resetDrag();
}
} else if (notification is ScrollEndNotification) {
if (_canRefresh && !_isRefreshing) {
_triggerRefresh();
} else if (!_isRefreshing) {
_resetDrag();
}
} else if (notification is OverscrollNotification) {
if (notification.overscroll < 0 && notification.metrics.pixels <= 0) {
setState(() {
final overscrollAmount = -notification.overscroll;
_totalDragDistance = math.min(
widget.triggerDistance + widget.minDragStartThreshold,
_totalDragDistance + overscrollAmount,
);
if (_totalDragDistance > widget.minDragStartThreshold) {
_dragOffset = _totalDragDistance - widget.minDragStartThreshold;
_canRefresh = _dragOffset >= widget.triggerDistance;
} else {
_dragOffset = 0.0;
_canRefresh = false;
}
});
if (_dragOffset > 0) {
_checkForHapticFeedback();
}
}
}
}
void _checkForHapticFeedback() {
final progress = _dragOffset / widget.triggerDistance;
final currentFilledDots = (progress * 8).round().clamp(0, 8);
if (currentFilledDots > _previousFilledDots) {
// Stronger haptic feedback when all dots are filled
if (currentFilledDots == 8) {
HapticFeedback.mediumImpact();
} else {
HapticFeedback.lightImpact();
}
_previousFilledDots = currentFilledDots;
} else if (currentFilledDots < _previousFilledDots) {
_previousFilledDots = currentFilledDots;
}
}
void _triggerRefresh() async {
setState(() {
_isRefreshing = true;
});
// Haptic feedback when refresh is triggered
HapticFeedback.heavyImpact();
// Start continuous spinning animation
_controller.repeat();
try {
await widget.onRefresh();
} finally {
if (mounted) {
_controller.stop();
_controller.reset();
_resetDrag();
}
}
}
void _resetDrag() {
setState(() {
_dragOffset = 0.0;
_totalDragDistance = 0.0;
_canRefresh = false;
_isRefreshing = false;
_previousFilledDots = 0;
});
}
@override
Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification.depth == 0) {
_handleScrollNotification(notification);
}
return false;
},
child: Stack(
children: [
widget.child,
if (_dragOffset > 0)
Positioned(
top: 0,
left: 0,
right: 0,
child: Container(
height: math.min(_dragOffset, 100),
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.black.withValues(alpha: 0.3), Colors.transparent],
),
),
child: Padding(
padding: const EdgeInsets.only(bottom: 20),
child: CustomPaint(
size: const Size(60, 60),
painter: CircularDotsIndicator(
progress: _dragOffset / widget.triggerDistance,
isRefreshing: _isRefreshing,
animation: _animation,
),
),
),
),
),
],
),
);
}
}
class CircularDotsIndicator extends CustomPainter {
final double progress;
final bool isRefreshing;
final Animation<double> animation;
CircularDotsIndicator({required this.progress, required this.isRefreshing, required this.animation})
: super(repaint: animation);
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final radius = size.width / 2 - 12;
const dotRadius = 3.0;
// Calculate how many dots should be filled
const totalDots = 8;
final filledDots = isRefreshing ? totalDots : (progress * totalDots).round().clamp(0, totalDots);
// Add rotation when refreshing
if (isRefreshing) {
canvas.save();
canvas.translate(center.dx, center.dy);
canvas.rotate(animation.value * 2 * math.pi);
canvas.translate(-center.dx, -center.dy);
}
for (int i = 0; i < totalDots; i++) {
final angle = (i * 2 * math.pi / totalDots) - math.pi / 2;
final dotCenter = Offset(center.dx + radius * math.cos(angle), center.dy + radius * math.sin(angle));
final isFilled = i < filledDots;
// Enhanced animation when refreshing
if (isRefreshing) {
// Create a wave effect with varying opacity and size
final wavePhase = (animation.value * 2 * math.pi) + (i * math.pi / 4);
final opacity = 0.4 + (0.6 * (math.sin(wavePhase) + 1) / 2);
final sizeFactor = 0.8 + (0.4 * (math.cos(wavePhase) + 1) / 2);
final paint = Paint()
..style = PaintingStyle.fill
..color = Colors.white.withValues(alpha: opacity);
// Add enhanced shadow for spinning dots
final shadowPaint = Paint()
..style = PaintingStyle.fill
..color = Colors.white.withValues(alpha: opacity * 0.3)
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 3.0);
canvas.drawCircle(dotCenter, (dotRadius + 1) * sizeFactor, shadowPaint);
canvas.drawCircle(dotCenter, dotRadius * sizeFactor, paint);
} else {
// Static dots during pull-down
final paint = Paint()
..style = PaintingStyle.fill
..color = isFilled ? Colors.white : Colors.white.withValues(alpha: 0.3);
// Add shadow for filled dots
if (isFilled) {
final shadowPaint = Paint()
..style = PaintingStyle.fill
..color = Colors.white.withValues(alpha: 0.3)
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 2.0);
canvas.drawCircle(dotCenter, dotRadius + 1, shadowPaint);
}
canvas.drawCircle(dotCenter, dotRadius, paint);
}
}
if (isRefreshing) {
canvas.restore();
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}