forked from flutter/assets-for-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_drag_scroll_activity.dart
83 lines (71 loc) · 2.35 KB
/
fake_drag_scroll_activity.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
import 'dart:async';
import 'package:flutter/widgets.dart';
/// A [ScrollActivity] that simulates a user dragging their finger over the
/// screen to scroll.
///
/// We cannot use [ScrollController.animateTo] for this animation because it
/// sets [SliverConstraints.userScrollDirection] to [ScrollDirection.idle] to
/// indicate that there is no real user scrolling. However, [SliverAppBar]s use
/// this property to show or hide the app bar when the user changes scrolling
/// direction and we want to show that in the animations.
///
/// This implementation is inspired by [DrivenScrollActivity], but instead
/// of using [ScrollActivityDelegate.setPixels] it uses
/// [ScrollActivityDelegate.applyUserOffset] to simulate real scrolling and
/// to trigger all scroll-related effects in [SliverAppBar]s.
class FakeDragScrollActivity extends ScrollActivity {
FakeDragScrollActivity(
ScrollActivityDelegate delegate, {
required double from,
required double to,
required Duration duration,
required Curve curve,
required TickerProvider vsync,
}) : _completer = Completer<void>(),
_controller = AnimationController.unbounded(
value: from,
debugLabel: '$FakeDragScrollActivity',
vsync: vsync,
),
_lastValue = from,
super(delegate) {
_controller
..addListener(_tick)
..animateTo(to, duration: duration, curve: curve).whenComplete(_end);
}
final Completer<void> _completer;
final AnimationController _controller;
Future<void> get done => _completer.future;
@override
double get velocity => _controller.velocity;
double _lastValue;
void _tick() {
if (_lastValue != _controller.value) {
delegate.applyUserOffset(_lastValue - _controller.value);
_lastValue = _controller.value;
}
}
void _end() {
delegate.goBallistic(velocity);
}
@override
void dispatchOverscrollNotification(
ScrollMetrics metrics, BuildContext context, double overscroll) {
OverscrollNotification(
metrics: metrics,
context: context,
overscroll: overscroll,
velocity: velocity)
.dispatch(context);
}
@override
bool get shouldIgnorePointer => true;
@override
bool get isScrolling => true;
@override
void dispose() {
_completer.complete();
_controller.dispose();
super.dispose();
}
}