-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathquick_scrollbar.dart
More file actions
169 lines (154 loc) · 4.98 KB
/
quick_scrollbar.dart
File metadata and controls
169 lines (154 loc) · 4.98 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
import 'dart:async';
import 'package:flutter/material.dart';
/// A quick scrollbar not only indicates which portion of a [Scrollable]
/// widget is actually visible, but also control the scroll position of
/// [Scrollable] widget by [ScrollController].
///
/// Quick scrollbar support the drag gesture to scroll the [Scrollable]
/// widget quickly.
/// for example:
/// ```dart
/// QuickScrollbar(
/// child: ListView.builder(
/// itemCount: 1000,
/// // Specifying an [itemExtent] or [prototypeItem] is more efficient
/// // than letting the children determine their own extent when use QuickScrollbar.
/// //itemExtent: 56,
/// prototypeItem: const ListTile(title: Text("1")),
/// itemBuilder: (ctx, index) => ListTile(
/// title: Text("$index"),
/// onTap: () => debugPrint('$index'),
/// ),
/// ),
/// );
/// ```dart
class QuickScrollbar extends StatefulWidget {
const QuickScrollbar({
Key? key,
this.controller,
this.velocity = 10,
required this.child,
}) : super(key: key);
final Widget child;
/// The [Scrollable] widget's Controller, by which quick scrollbar
/// will control the scroll position of [Scrollable] widget.
final ScrollController? controller;
/// Critical value that determine to show [QuickScrollbar].
/// If the scroll delta offset greater than [velocity], the
/// quick scrollbar will show.
final int velocity;
@override
_QuickScrollBarState createState() => _QuickScrollBarState();
}
class _QuickScrollBarState extends State<QuickScrollbar>
with SingleTickerProviderStateMixin {
double _offsetTop = 0.0;
final double _barHeight = 35.0;
// Animation controller for show/hide bar .
late AnimationController _animationController;
late Animation<double> _animation;
Timer? _timer;
late ScrollController _scrollController;
@override
void initState() {
super.initState();
_scrollController = widget.controller!;
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 200),
);
_animation = Tween(begin: 1.0, end: 0.0).animate(_animationController);
_animationController.value = 1.0;
}
@override
void dispose() {
_animationController.dispose();
_scrollController.dispose();
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
Widget stack = Stack(
children: <Widget>[
RepaintBoundary(
child: widget.child,
),
Positioned(
top: _offsetTop,
right: 0.0,
child: RepaintBoundary(
child: GestureDetector(
child: Padding(
padding: const EdgeInsets.only(right: 5.0),
child: FadeTransition(
child: Material(
color: const Color(0xffe8e8e8),
elevation: .8,
child: SizedBox(
height: _barHeight,
child: Icon(
Icons.unfold_more,
color: Colors.grey[600],
size: 24.0,
),
),
),
opacity: _animation,
),
),
onVerticalDragStart: (details) => _timer?.cancel(),
onVerticalDragUpdate: (DragUpdateDetails details) {
final position = _scrollController.position;
double pixels = (position.extentBefore + position.extentAfter) *
details.delta.dy /
(position.extentInside - _barHeight);
pixels += position.pixels;
_scrollController.jumpTo(pixels.clamp(
0.0,
position.maxScrollExtent,
));
},
onVerticalDragEnd: (details) {
_fadeBar();
},
),
),
)
],
);
return NotificationListener<ScrollNotification>(
onNotification: _handleNotification,
child: stack,
);
}
void _fadeBar() {
if (_animationController.value == 1.0) return;
_timer?.cancel();
_timer = Timer(
const Duration(seconds: 1),
() {
_animationController.animateTo(1.0);
_animationController.forward();
},
);
}
bool _handleNotification(ScrollNotification notification) {
if (notification is ScrollUpdateNotification) {
if (notification.scrollDelta != null &&
notification.scrollDelta!.abs() > widget.velocity &&
notification.metrics.maxScrollExtent != double.infinity) {
_animationController.value = 0.0;
}
}
setState(() {
double total =
notification.metrics.extentBefore + notification.metrics.extentAfter;
_offsetTop = notification.metrics.extentBefore /
total *
(notification.metrics.extentInside - _barHeight);
_fadeBar();
});
return true;
}
}