scroll_controller.dart
5.37 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
import 'package:flutter/widgets.dart';
import 'package:sheet/sheet.dart';
/// A [ScrollController] suitable for use in a [SheetPrimaryScrollPosition] created
/// by a [Sheet].
///
/// If a [Sheet] contains content that is exceeds the height
/// of its container, this controller will allow the sheet to both be dragged to
/// fill the container and then scroll the child content.
///
/// See also:
///
/// * [SheetPrimaryScrollPosition], which manages the positioning logic for
/// this controller.
/// * [PrimarySheetController], which can be used to establish a
/// [_SheetScrollController] as the primary controller for
/// descendants.
class SheetPrimaryScrollController extends ScrollController {
SheetPrimaryScrollController({
double initialScrollOffset = 0.0,
String? debugLabel,
required this.sheetContext,
}) : super(
debugLabel: debugLabel,
initialScrollOffset: initialScrollOffset,
);
final SheetContext sheetContext;
@override
SheetPrimaryScrollPosition createScrollPosition(
ScrollPhysics physics,
ScrollContext context,
ScrollPosition? oldPosition,
) {
return SheetPrimaryScrollPosition(
physics: physics,
context: context,
oldPosition: oldPosition,
sheetContext: sheetContext,
);
}
}
class _SheetScrollActivity extends ScrollActivity {
_SheetScrollActivity(SheetPosition delegate) : super(delegate);
@override
bool get isScrolling => true;
@override
bool get shouldIgnorePointer => false;
@override
double get velocity => 0;
}
/// A scroll position that manages scroll activities for
/// [_SheetScrollController].
///
/// This class is a concrete subclass of [ScrollPosition] logic that handles a
/// single [ScrollContext], such as a [Scrollable]. An instance of this class
/// manages [ScrollActivity] instances, which changes the
/// [SheetPrimaryScrollPosition.currentExtent] or visible content offset in the
/// [Scrollable]'s [Viewport]
///
/// See also:
///
/// * [_SheetScrollController], which uses this as its [ScrollPosition].
class SheetPrimaryScrollPosition extends ScrollPositionWithSingleContext {
SheetPrimaryScrollPosition({
required ScrollPhysics physics,
required ScrollContext context,
double initialPixels = 0.0,
bool keepScrollOffset = true,
required this.sheetContext,
ScrollPosition? oldPosition,
String? debugLabel,
}) : super(
physics: physics,
context: context,
initialPixels: initialPixels,
keepScrollOffset: keepScrollOffset,
oldPosition: oldPosition,
debugLabel: debugLabel,
);
final SheetContext sheetContext;
SheetPosition get sheetPosition => sheetContext.position;
bool sheetShouldSheetAcceptUserOffser(double delta) {
// Can drag down if list already on the top
final bool canDragForward = delta >= 0 && pixels <= minScrollExtent;
// Can drag up if sheet is not yet on top and list is already on top
final bool canDragBackwards = delta < 0 &&
sheetPosition.pixels < sheetPosition.maxScrollExtent &&
pixels <= minScrollExtent;
return sheetPosition.physics.shouldAcceptUserOffset(sheetPosition) &&
(canDragForward || canDragBackwards);
}
@override
void applyUserOffset(double delta) {
if (sheetPosition.preventingDrag) {
return;
}
if (sheetShouldSheetAcceptUserOffser(delta)) {
final double pixels = sheetPosition.pixels -
sheetPosition.physics.applyPhysicsToUserOffset(sheetPosition, delta);
sheetPosition.forcePixels(
pixels.clamp(
sheetPosition.minScrollExtent, sheetPosition.maxScrollExtent),
);
sheetPosition.beginActivity(_SheetScrollActivity(sheetPosition));
return;
} else {
super.applyUserOffset(delta);
sheetPosition.goIdle();
}
}
@override
void goBallistic(double velocity) {
if (sheetPosition.preventingDrag) {
beginActivity(
BallisticScrollActivity(
this,
ScrollSpringSimulation(
SpringDescription.withDampingRatio(
mass: 0.5,
stiffness: 100.0,
ratio: 1.1,
),
pixels,
0,
velocity,
),
context.vsync,
true,
),
);
return;
}
final bool sheetDragging = sheetPosition.activity!.isScrolling;
if (sheetDragging &&
sheetPosition.hasContentDimensions &&
!sheetPosition.preventingDrag &&
sheetPosition.activity!.isScrolling) {
sheetPosition.goBallistic(velocity);
} else {
sheetPosition.goIdle();
}
if (!sheetDragging) {
super.goBallistic(velocity);
return;
} else if (velocity > 0.0 &&
sheetPosition.pixels >= sheetPosition.maxScrollExtent ||
(velocity < 0.0 && pixels > 0)) {
// super.goBallistic(velocity);
return;
} else if (outOfRange) {
beginActivity(
BallisticScrollActivity(
this,
ScrollSpringSimulation(
SpringDescription.withDampingRatio(
mass: 0.5,
stiffness: 100.0,
ratio: 1.1,
),
pixels,
0,
velocity,
),
context.vsync,
true,
),
);
return;
}
goIdle();
}
//@override
//double get pixels => super.pixels + viewportDimension;
}