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
| class ScrollableState {
void _handleDragDown(DragDownDetails details) { assert(_drag == null); assert(_hold == null); _hold = position.hold(_disposeHold); }
void _handleDragStart(DragStartDetails details) { assert(_drag == null); _drag = position.drag(details, _disposeDrag); assert(_drag != null); assert(_hold == null); }
void _handleDragUpdate(DragUpdateDetails details) { assert(_hold == null || _drag == null); _drag?.update(details); }
void _handleDragEnd(DragEndDetails details) { assert(_hold == null || _drag == null); _drag?.end(details); assert(_drag == null); } }
class ScrollPositionWithSingleContext {
@override ScrollHoldController hold(VoidCallback holdCancelCallback) { final double previousVelocity = activity!.velocity; final HoldScrollActivity holdActivity = HoldScrollActivity( delegate: this, onHoldCanceled: holdCancelCallback, ); beginActivity(holdActivity); _heldPreviousVelocity = previousVelocity; return holdActivity; }
ScrollDragController? _currentDrag;
@override Drag drag(DragStartDetails details, VoidCallback dragCancelCallback) { final ScrollDragController drag = ScrollDragController( delegate: this, details: details, onDragCanceled: dragCancelCallback, carriedVelocity: physics.carriedMomentum(_heldPreviousVelocity), motionStartDistanceThreshold: physics.dragStartDistanceMotionThreshold, ); beginActivity(DragScrollActivity(this, drag)); assert(_currentDrag == null); _currentDrag = drag; return drag; }
@override void beginActivity(ScrollActivity? newActivity) { _heldPreviousVelocity = 0.0; if (newActivity == null) { return; } assert(newActivity.delegate == this); super.beginActivity(newActivity); _currentDrag?.dispose(); _currentDrag = null; if (!activity!.isScrolling) { updateUserScrollDirection(ScrollDirection.idle); } }
@override void goBallistic(double velocity) { assert(hasPixels); final Simulation? simulation = physics.createBallisticSimulation(this, velocity); if (simulation != null) { beginActivity(BallisticScrollActivity(this, simulation, context.vsync)); } else { goIdle(); } }
}
class ScrollDragController {
@override void update(DragUpdateDetails details) { assert(details.primaryDelta != null); _lastDetails = details; double offset = details.primaryDelta!; if (offset != 0.0) { _lastNonStationaryTimestamp = details.sourceTimeStamp; } _maybeLoseMomentum(offset, details.sourceTimeStamp); offset = _adjustForScrollStartThreshold(offset, details.sourceTimeStamp); if (offset == 0.0) { return; } if (_reversed) { offset = -offset; } delegate.applyUserOffset(offset); }
void _maybeLoseMomentum(double offset, Duration? timestamp) { if (_retainMomentum && offset == 0.0 && (timestamp == null || timestamp - _lastNonStationaryTimestamp! > momentumRetainStationaryDurationThreshold)) { _retainMomentum = false; } }
@override void end(DragEndDetails details) { assert(details.primaryVelocity != null); double velocity = -details.primaryVelocity!; if (_reversed) { velocity = -velocity; } _lastDetails = details;
if (_retainMomentum) { final bool isFlingingInSameDirection = velocity.sign == carriedVelocity!.sign; final bool isVelocityNotSubstantiallyLessThanCarriedMomentum = velocity.abs() > carriedVelocity!.abs() * momentumRetainVelocityThresholdFactor; if(isFlingingInSameDirection && isVelocityNotSubstantiallyLessThanCarriedMomentum) { velocity += carriedVelocity!; } } delegate.goBallistic(velocity); }
}
|