-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
sticky_test.dart
341 lines (290 loc) · 9.96 KB
/
sticky_test.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import 'package:flutter/material.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:sliver_tools/sliver_tools.dart';
void main() {
setUp(() {
WidgetsBinding.instance.renderView.configuration =
TestViewConfiguration(size: Size(400, 800));
});
testWidgets('Mix sticky and not sticky headers', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: CustomScrollView(
cacheExtent: 0,
slivers: <Widget>[
SliverStickyHeader(
header: _Header(index: 0),
sliver: const _Sliver(),
),
SliverStickyHeader(
header: _Header(index: 1),
sticky: false,
sliver: const _Sliver(),
),
SliverStickyHeader(
header: _Header(index: 2),
sliver: const _Sliver(),
),
],
),
),
),
);
final header00Finder = find.text('Header #0');
final header01Finder = find.text('Header #1');
final header02Finder = find.text('Header #2');
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsNothing);
expect(header02Finder, findsNothing);
final gesture = await tester.startGesture(Offset(200, 100));
// We scroll just before the Header #1.
await gesture.moveBy(Offset(0, -80));
await tester.pump();
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsNothing);
expect(header02Finder, findsNothing);
// We scroll just after the Header #1 so that it is visible.
await gesture.moveBy(Offset(0, -80));
await tester.pump();
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsOneWidget);
expect(header02Finder, findsNothing);
// We scroll in a way that Headers 0 and 1 are side by side.
await gesture.moveBy(Offset(0, -640));
await tester.pump();
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsOneWidget);
expect(header02Finder, findsNothing);
// We scroll in a way that Header #1 is at the top of the screen.
await gesture.moveBy(Offset(0, -80));
await tester.pump();
expect(header00Finder, findsNothing);
expect(header01Finder, findsOneWidget);
expect(header02Finder, findsNothing);
// We scroll in a way that Header #1 is not visible.
await gesture.moveBy(Offset(0, -80));
await tester.pump();
expect(header00Finder, findsNothing);
// Header #1 is in the tree (because the sliver is onstage).
expect(tester.getRect(header01Finder), const Rect.fromLTRB(0, -80, 400, 0));
expect(header02Finder, findsNothing);
});
testWidgets('Mix sticky and not sticky headers - reverse',
(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: CustomScrollView(
cacheExtent: 0,
reverse: true,
slivers: <Widget>[
SliverStickyHeader(
header: _Header(index: 0),
sliver: const _Sliver(),
),
SliverStickyHeader(
header: _Header(index: 1),
sticky: false,
sliver: const _Sliver(),
),
SliverStickyHeader(
header: _Header(index: 2),
sliver: const _Sliver(),
),
],
),
),
),
);
final header00Finder = find.text('Header #0');
final header01Finder = find.text('Header #1');
final header02Finder = find.text('Header #2');
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsNothing);
expect(header02Finder, findsNothing);
final gesture = await tester.startGesture(Offset(200, 100));
// We scroll just before the Header #1.
await gesture.moveBy(Offset(0, 80));
await tester.pump();
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsNothing);
expect(header02Finder, findsNothing);
// We scroll just after the Header #1 so that it is visible.
await gesture.moveBy(Offset(0, 80));
await tester.pump();
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsOneWidget);
expect(header02Finder, findsNothing);
// We scroll in a way that Headers 0 and 1 are side by side.
await gesture.moveBy(Offset(0, 640));
await tester.pump();
expect(header00Finder, findsOneWidget);
expect(header01Finder, findsOneWidget);
expect(header02Finder, findsNothing);
// We scroll in a way that Header #1 is at the top of the screen.
await gesture.moveBy(Offset(0, 80));
await tester.pump();
expect(header00Finder, findsNothing);
expect(header01Finder, findsOneWidget);
expect(header02Finder, findsNothing);
// We scroll in a way that Header #1 is no longer visible.
await gesture.moveBy(Offset(0, 80));
await tester.pump();
expect(header00Finder, findsNothing);
// Header #1 is in the tree (because the sliver is onstage).
expect(
tester.getRect(header01Finder), const Rect.fromLTRB(0, 800, 400, 880));
expect(header02Finder, findsNothing);
});
testWidgets('Testing multi-depth sticky headers', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: CustomScrollView(
cacheExtent: 0,
slivers: <Widget>[
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '1'),
sliver: MultiSliver(
children: [
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '1.1'),
sliver: const _Sliver100(),
),
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '1.2'),
sliver: MultiSliver(
children: [
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '1.2.1'),
sliver: const _Sliver100(),
),
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '1.2.2'),
sliver: const _Sliver100(),
),
],
),
),
],
),
),
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '2'),
sliver: const _Sliver100(),
),
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '3'),
sliver: MultiSliver(
children: [
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '3.1'),
sliver: const _Sliver100(),
),
SliverStickyHeader(
header: _HierarchyHeader(hierarchy: '3.2'),
sliver: const _Sliver100(),
),
],
),
),
],
),
),
),
);
final header001Finder = find.text('Header 1');
final header011Finder = find.text('Header 1.1');
final header012Finder = find.text('Header 1.2');
final header121Finder = find.text('Header 1.2.1');
expect(header001Finder, findsOneWidget);
expect(header011Finder, findsOneWidget);
expect(header012Finder, findsOneWidget);
expect(header121Finder, findsOneWidget);
expect(tester.getTopLeft(header011Finder).dy, 50);
expect(tester.getTopLeft(header012Finder).dy, 200);
expect(tester.getTopLeft(header121Finder).dy, 250);
// We scroll a little and expect that header 1 is sticky.
final gesture = await tester.startGesture(Offset(200, 100));
await gesture.moveBy(Offset(0, -25));
await tester.pump();
expect(tester.getTopLeft(header011Finder).dy, 50);
expect(tester.getTopLeft(header012Finder).dy, 175);
expect(tester.getTopLeft(header121Finder).dy, 225);
await gesture.moveBy(Offset(0, -125));
await tester.pump();
expect(tester.getTopLeft(header011Finder).dy, 0);
expect(tester.getTopLeft(header012Finder).dy, 50);
expect(tester.getTopLeft(header121Finder).dy, 100);
await gesture.moveBy(Offset(0, -25));
await tester.pump();
expect(tester.getTopLeft(header012Finder).dy, 50);
expect(tester.getTopLeft(header121Finder).dy, 100);
});
}
class _HierarchyHeader extends StatelessWidget {
const _HierarchyHeader({
Key? key,
required this.hierarchy,
}) : super(key: key);
final String hierarchy;
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Text('Header $hierarchy'),
height: 50,
);
}
}
class _Sliver100 extends StatelessWidget {
const _Sliver100({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SliverToBoxAdapter(
child: SizedBox(height: 100),
);
}
}
class _Header extends StatelessWidget {
const _Header({
Key? key,
required this.index,
}) : super(key: key);
final int index;
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Text('Header #$index'),
height: 80,
);
}
}
class _Sliver extends StatelessWidget {
const _Sliver({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) => const _SliverItem(),
childCount: 20,
),
);
}
}
class _SliverItem extends StatelessWidget {
const _SliverItem({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(height: 40);
}
}