-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmain_detect_scroll.dart
More file actions
184 lines (169 loc) · 5.01 KB
/
main_detect_scroll.dart
File metadata and controls
184 lines (169 loc) · 5.01 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
// ignore_for_file: deprecated_member_use
import 'package:assorted_layout_widgets/assorted_layout_widgets.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
const HomePage(),
);
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(scrollbarTheme: _scrollbarTheme()),
home: const Demo(),
);
}
/// This theme makes the scrollbar always visible, with a thickness of 20 px.
ScrollbarThemeData _scrollbarTheme() {
return ScrollbarThemeData(
thumbVisibility: MaterialStateProperty.all(true),
trackVisibility: MaterialStateProperty.all(true),
thickness: MaterialStateProperty.all(20.0),
interactive: true,
radius: const Radius.circular(8),
minThumbLength: 32,
trackColor: MaterialStateProperty.all(Colors.blue[100]),
thumbColor: MaterialStateProperty.all(Colors.blue[900]),
crossAxisMargin: 0,
mainAxisMargin: 0,
);
}
}
class Demo extends StatefulWidget {
const Demo({super.key});
@override
State<Demo> createState() => _DemoState();
}
class _DemoState extends State<Demo> {
int lines = 10;
bool canScroll = false;
double scrollbarWidth = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('DetectScroll Example')),
body: Column(
children: [
//
Expanded(
child: DetectScroll(
onChange: _onChange,
child: LinesOfText(lines: lines),
),
),
//
const Box.gap(20),
Text('onChange gets: canScroll=$canScroll, scrollbarWidth=$scrollbarWidth'),
const Box.gap(20),
Padding(
padding: const Pad(horizontal: 20),
child: Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () {
setState(() {
lines += 10;
});
},
child: const Text('Add lines'),
),
),
const Box.gap(20),
Expanded(
child: ElevatedButton(
onPressed: () {
setState(() {
lines -= 10;
if (lines < 0) lines = 0;
});
},
child: const Text('Remove lines'),
),
),
],
),
),
const Box.gap(40),
],
),
);
}
/// This callback is called whenever the scroll state changes.
void _onChange({
required bool canScroll,
required double scrollbarWidth,
}) {
setState(() {
this.canScroll = canScroll;
this.scrollbarWidth = scrollbarWidth;
});
}
}
class LinesOfText extends StatelessWidget {
final int lines;
const LinesOfText({
super.key,
required this.lines,
});
@override
Widget build(BuildContext context) {
final ScrollController scrollController = ScrollController();
return Box(
color: Colors.blue[200],
width: double.infinity,
child: Stack(
children: [
Scrollbar(
child: SingleChildScrollView(
controller: scrollController,
child: Padding(
padding: const Pad(horizontal: 12, vertical: 6),
child: Column(
mainAxisSize: MainAxisSize.max,
children: List.generate(
lines,
(index) => SizedBox(
width: double.infinity,
child: Text('$lines lines of text'),
),
),
),
),
),
),
Positioned(
top: 0,
right: DetectScroll.of(context).canScroll
? DetectScroll.of(context).scrollbarWidth
: 0,
child: IconButton(
icon: const Icon(Icons.help_outline),
onPressed: () {
bool canScroll = DetectScroll.of(context).canScroll;
double scrollbarWidth = DetectScroll.of(context).scrollbarWidth;
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text(
'DetectScroll.of(context).canScroll = $canScroll'
'\n\n'
'DetectScroll.of(context).scrollbarWidth = $scrollbarWidth'
'\n\n'
'Note the help button will move '
'when the scrollbar is shown and removed.',
),
);
},
);
},
),
),
],
),
);
}
}