-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpull_to_refresh.dart
150 lines (135 loc) · 4.89 KB
/
pull_to_refresh.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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/gestures.dart';
import 'package:webview_flutter/webview_flutter.dart';
// Fixed issue: https://github.com/flutter/flutter/issues/39389
class DragGesturePullToRefresh extends VerticalDragGestureRecognizer {
static const int exceedsLoadingTime = 3000;
late BuildContext _context;
late WebViewController _controller;
// loading
Completer<void> completer = Completer<void>();
late int msWaitToRestart;
int msLoading = 0;
bool isLoading = true;
// drag
int dragStartYDiff = 0;
double dragHeightEnd = 200;
bool dragStarted = false;
double dragDistance = 0;
@override
//override rejectGesture here
void rejectGesture(int pointer) {
acceptGesture(pointer);
}
void _clearDrag() {
dragStarted = false;
dragDistance = 0;
}
/// [context] RefreshIndicator
DragGesturePullToRefresh setContext(BuildContext context) { _context = context; return this; }
/// [controller] WebViewController
DragGesturePullToRefresh setController(WebViewController controller) { _controller = controller; return this; }
/// [dragHeightEnd] End height for starting the refresh
DragGesturePullToRefresh setDragHeightEnd(double value) { dragHeightEnd = value; return this; }
/// [msWaitToRestart] milliseconds to reallow pull to refresh if the website
/// didn't load in msWaitToRestart time
DragGesturePullToRefresh setWaitToRestart(int value) { msWaitToRestart = value; return this; }
/// [dragStartYDiff] add some offset as page top is not always obviously page top, e.g. 10
DragGesturePullToRefresh setDragStartYDiff(int value) { dragStartYDiff = value; return this; }
/// start refresh
Future<void> refresh() {
if (!completer.isCompleted) {
completer.complete();
}
completer = Completer<void>();
started();
_controller.reload();
return completer.future;
}
/// Loading started
void started() {
msLoading = DateTime.now().millisecondsSinceEpoch;
isLoading = true;
}
/// Loading finished
void finished() {
msLoading = 0;
isLoading = false;
// hide the RefreshIndicator
if (!completer.isCompleted) {
completer.complete();
}
}
FixedScrollMetrics _getMetrics(double minScrollExtent, double maxScrollExtent,
double pixels, double viewportDimension, AxisDirection axisDirection) {
return FixedScrollMetrics(
minScrollExtent: minScrollExtent,
maxScrollExtent: maxScrollExtent,
pixels: pixels,
viewportDimension: viewportDimension,
axisDirection: axisDirection, devicePixelRatio: 1
);
}
/// [msWaitToRestart] milliseconds to reallow pull to refresh if the website
/// didn't load in msWaitToRestart time
///
/// [dragStartYDiff] add some offset as page top is not always obviously page top, e.g. 10
DragGesturePullToRefresh([this.msWaitToRestart = exceedsLoadingTime, this.dragStartYDiff = 0]) {
onStart = (DragStartDetails dragDetails) async {
//debugPrint('DragGesturePullToRefresh(): $dragDetails');
if (!isLoading ||
// Reallow pull to refresh if the website didn't load in msWaitToRestart time
(msLoading > 0 && (DateTime.now().millisecondsSinceEpoch - msLoading) > msWaitToRestart)) {
Offset scrollPos = await _controller.getScrollPosition();
// Only allow RefreshIndicator if you are at the top of page!
if (scrollPos.dy <= dragStartYDiff) {
dragStarted = true;
dragDistance = 0;
ScrollStartNotification(
metrics: _getMetrics(0, dragHeightEnd, 0, dragHeightEnd, AxisDirection.down),
dragDetails: dragDetails,
context: _context
).dispatch(_context);
}
}
};
onUpdate = (DragUpdateDetails dragDetails) {
if (dragStarted) {
double dy = dragDetails.delta.dy;
dragDistance += dy;
ScrollUpdateNotification(
metrics: _getMetrics(
dy > 0 ? 0 : dragDistance, dragHeightEnd,
dy > 0 ? (-1) * dy : dragDistance, dragHeightEnd,
dragDistance < 0 ? AxisDirection.up : AxisDirection.down),
context: _context,
scrollDelta: (-1) * dy
).dispatch(_context);
if (dragDistance < 0) {
_clearDrag();
}
}
};
onEnd = (DragEndDetails dragDetails) {
if (dragStarted) {
ScrollEndNotification(
metrics: _getMetrics(0, dragHeightEnd, dragDistance, dragHeightEnd, AxisDirection.down),
context: _context
).dispatch(_context);
_clearDrag();
}
};
onCancel = () {
if (dragStarted) {
ScrollUpdateNotification(
metrics: _getMetrics(0, dragHeightEnd, 1, dragHeightEnd, AxisDirection.up),
context: _context,
scrollDelta: 0
).dispatch(_context);
_clearDrag();
}
};
}
}