/
scroll_view_gesture_getx.dart
74 lines (66 loc) · 2.02 KB
/
scroll_view_gesture_getx.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
import 'dart:math';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
class ScrollViewGestureGetx extends GetxController
with GetSingleTickerProviderStateMixin {
late Animation<double> scrollAnimation;
late AnimationController scrollController;
RxDouble topPosition = (0.0).obs;
Offset dragStartOffset = const Offset(0, 0);
double dragStartValue = 0.0;
double clamping<T extends num>(T number, T low, T high) =>
max(low * 1.0, min(number * 1.0, high * 1.0));
void dragStarted(DragStartDetails details) {
dragStartOffset = details.globalPosition;
dragStartValue = topPosition.value;
}
void dragUpdated({
required DragUpdateDetails details,
required double min,
required double max,
}) {
var newValue = clamping(
dragStartValue - (details.globalPosition - dragStartOffset).dy / 1.0,
min,
max);
topPosition.value = newValue;
}
void dragEnded({
required DragEndDetails details,
required double min,
required double max,
}) {
double velocity = details.primaryVelocity!;
double originalValue = topPosition.value;
void Function() currentFlingListener =
_flingListener(originalValue: originalValue, min: min, max: max);
scrollController.duration = Duration(milliseconds: velocity.abs().toInt());
scrollAnimation =
Tween(begin: 0.0, end: velocity / 2).animate(CurvedAnimation(
curve: Curves.fastLinearToSlowEaseIn,
parent: scrollController,
))
..addListener(currentFlingListener);
scrollController
..reset()
..forward();
}
void Function() _flingListener({
required double originalValue,
required double min,
required double max,
}) {
return () {
double newValue =
clamping(originalValue - scrollAnimation.value, min, max);
if (newValue != topPosition.value) {
topPosition.value = newValue;
}
};
}
@override
void onInit() {
scrollController = AnimationController(vsync: this);
super.onInit();
}
}