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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import '../rendering/mock_canvas.dart';
final Matcher doesNotOverscroll = isNot(paints..circle());
Future<void> slowDrag(WidgetTester tester, Offset start, Offset offset) async {
final TestGesture gesture = await tester.startGesture(start);
for (int index = 0; index < 10; index += 1) {
await gesture.moveBy(offset);
await tester.pump(const Duration(milliseconds: 20));
}
await gesture.up();
}
void main() {
testWidgets('Overscroll indicator color', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 2000.0)),
],
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
expect(painter, doesNotOverscroll);
// the scroll gesture from tester.scroll happens in zero time, so nothing should appear:
await tester.drag(find.byType(Scrollable), const Offset(0.0, 100.0));
expect(painter, doesNotOverscroll);
await tester.pump(); // allow the ticker to register itself
expect(painter, doesNotOverscroll);
await tester.pump(const Duration(milliseconds: 100)); // animate
expect(painter, doesNotOverscroll);
final TestGesture gesture = await tester.startGesture(const Offset(200.0, 200.0));
await tester.pump(const Duration(milliseconds: 100)); // animate
expect(painter, doesNotOverscroll);
await gesture.up();
expect(painter, doesNotOverscroll);
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0));
expect(painter, paints..circle(color: const Color(0x0DFFFFFF)));
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(painter, doesNotOverscroll);
});
testWidgets('Nested scrollable', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GlowingOverscrollIndicator(
axisDirection: AxisDirection.down,
color: const Color(0x0DFFFFFF),
notificationPredicate: (ScrollNotification notification) => notification.depth == 1,
child: const SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SizedBox(
width: 600.0,
child: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 2000.0)),
],
),
),
),
),
),
);
final RenderObject outerPainter = tester.renderObject(find.byType(CustomPaint).first);
final RenderObject innerPainter = tester.renderObject(find.byType(CustomPaint).last);
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0));
expect(outerPainter, paints..circle());
expect(innerPainter, paints..circle());
});
testWidgets('Overscroll indicator changes side when you drag on the other side', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 2000.0)),
],
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(400.0, 200.0), const Offset(0.0, 10.0));
expect(painter, paints..circle(x: 400.0));
await slowDrag(tester, const Offset(100.0, 200.0), const Offset(0.0, 10.0));
expect(painter, paints..something((Symbol method, List<dynamic> arguments) {
if (method != #drawCircle)
return false;
final Offset center = arguments[0] as Offset;
if (center.dx < 400.0)
return true;
throw 'Dragging on left hand side did not overscroll on left hand side.';
}));
await slowDrag(tester, const Offset(700.0, 200.0), const Offset(0.0, 10.0));
expect(painter, paints..something((Symbol method, List<dynamic> arguments) {
if (method != #drawCircle)
return false;
final Offset center = arguments[0] as Offset;
if (center.dx > 400.0)
return true;
throw 'Dragging on right hand side did not overscroll on right hand side.';
}));
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(painter, doesNotOverscroll);
});
testWidgets('Overscroll indicator changes side when you shift sides', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 2000.0)),
],
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
final TestGesture gesture = await tester.startGesture(const Offset(300.0, 200.0));
await gesture.moveBy(const Offset(0.0, 10.0));
await tester.pump(const Duration(milliseconds: 20));
double oldX = 0.0;
for (int index = 0; index < 10; index += 1) {
await gesture.moveBy(const Offset(50.0, 50.0));
await tester.pump(const Duration(milliseconds: 20));
expect(painter, paints..something((Symbol method, List<dynamic> arguments) {
if (method != #drawCircle)
return false;
final Offset center = arguments[0] as Offset;
if (center.dx <= oldX)
throw 'Sliding to the right did not make the center of the radius slide to the right.';
oldX = center.dx;
return true;
}));
}
await gesture.up();
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(painter, doesNotOverscroll);
});
group("Flipping direction of scrollable doesn't change overscroll behavior", () {
testWidgets('down', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
physics: AlwaysScrollableScrollPhysics(),
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 20.0)),
],
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0));
expect(painter, paints..save()..circle()..restore()..save()..scale(y: -1.0)..restore()..restore());
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(painter, doesNotOverscroll);
});
testWidgets('up', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
reverse: true,
physics: AlwaysScrollableScrollPhysics(),
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 20.0)),
],
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0));
expect(painter, paints..save()..scale(y: -1.0)..restore()..save()..circle()..restore()..restore());
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(painter, doesNotOverscroll);
});
});
testWidgets('Overscroll in both directions', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
physics: AlwaysScrollableScrollPhysics(),
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 20.0)),
],
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0));
expect(painter, paints..circle());
expect(painter, isNot(paints..circle()..circle()));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, -5.0));
expect(painter, paints..circle()..circle());
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(painter, doesNotOverscroll);
});
testWidgets('Overscroll horizontally', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: CustomScrollView(
scrollDirection: Axis.horizontal,
physics: AlwaysScrollableScrollPhysics(),
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 20.0)),
],
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(5.0, 0.0));
expect(painter, paints..rotate(angle: math.pi / 2.0)..circle()..saveRestore());
expect(painter, isNot(paints..circle()..circle()));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(-5.0, 0.0));
expect(
painter,
paints
..rotate(angle: math.pi / 2.0)
..circle()
..rotate(angle: math.pi / 2.0)
..circle(),
);
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(painter, doesNotOverscroll);
});
testWidgets('Nested overscrolls do not throw exceptions', (WidgetTester tester) async {
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: PageView(
children: <Widget>[
ListView(
children: <Widget>[
Container(
width: 2000.0,
height: 2000.0,
color: const Color(0xFF00FF00),
),
],
),
],
),
));
await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 2000.0));
await tester.pumpAndSettle();
});
testWidgets('Changing settings', (WidgetTester tester) async {
RenderObject painter;
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: ScrollConfiguration(
behavior: TestScrollBehavior1(),
child: CustomScrollView(
scrollDirection: Axis.horizontal,
physics: AlwaysScrollableScrollPhysics(),
reverse: true,
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 20.0)),
],
),
),
),
);
painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(5.0, 0.0));
expect(painter, paints..rotate(angle: math.pi / 2.0)..circle(color: const Color(0x0A00FF00)));
expect(painter, isNot(paints..circle()..circle()));
await tester.pumpAndSettle(const Duration(seconds: 1));
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: ScrollConfiguration(
behavior: TestScrollBehavior2(),
child: CustomScrollView(
scrollDirection: Axis.horizontal,
physics: AlwaysScrollableScrollPhysics(),
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 20.0)),
],
),
),
),
);
painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(5.0, 0.0));
expect(painter, paints..rotate(angle: math.pi / 2.0)..circle(color: const Color(0x0A0000FF))..saveRestore());
expect(painter, isNot(paints..circle()..circle()));
});
testWidgets('CustomScrollView overscroll indicator works if there is sliver before center', (WidgetTester tester) async {
final Key centerKey = UniqueKey();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ScrollConfiguration(
behavior: const TestScrollBehavior2(),
child: CustomScrollView(
center: centerKey,
physics: const AlwaysScrollableScrollPhysics(),
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => Text('First sliver $index'),
childCount: 2,
),
),
SliverList(
key: centerKey,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => Text('Second sliver $index'),
childCount: 5,
),
),
],
),
),
),
);
expect(find.text('First sliver 1'), findsNothing);
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 300.0));
expect(find.text('First sliver 1'), findsOneWidget);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
// The scroll offset and paint extend should cancel out each other.
expect(painter, paints..save()..translate(y: 0.0)..scale()..circle());
});
testWidgets('CustomScrollView overscroll indicator works well with [CustomScrollView.center] and [OverscrollIndicatorNotification.paintOffset]', (WidgetTester tester) async {
final Key centerKey = UniqueKey();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: ScrollConfiguration(
behavior: const TestScrollBehavior2(),
child: NotificationListener<OverscrollIndicatorNotification>(
onNotification: (OverscrollIndicatorNotification notification) {
if (notification.leading) {
notification.paintOffset = 50.0;
}
return false;
},
child: CustomScrollView(
center: centerKey,
physics: const AlwaysScrollableScrollPhysics(),
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => Text('First sliver $index'),
childCount: 2,
),
),
SliverList(
key: centerKey,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) => Text('Second sliver $index'),
childCount: 5,
),
),
],
),
),
),
),
);
expect(find.text('First sliver 1'), findsNothing);
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0)); // offset will be magnified ten times
expect(find.text('First sliver 1'), findsOneWidget);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
// The OverscrollIndicator should respect the [OverscrollIndicatorNotification.paintOffset] setting.
expect(painter, paints..save()..translate(y: 50.0)..scale()..circle());
});
testWidgets('The OverscrollIndicator should not overflow the scrollable view edge', (WidgetTester tester) async {
// Regressing test for https://github.com/flutter/flutter/issues/64149
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: NotificationListener<OverscrollIndicatorNotification>(
onNotification: (OverscrollIndicatorNotification notification) {
notification.paintOffset = 50.0; // both the leading and trailing indicator have a 50.0 pixels offset.
return false;
},
child: const CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 2000.0)),
],
),
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0));
expect(painter, paints..save()..translate(y: 50.0)..scale()..circle());
// Reverse scroll (30 pixels), and the offset < notification.paintOffset.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, -30.0));
await tester.pump();
// The OverscrollIndicator should move with the CustomScrollView.
expect(painter, paints..save()..translate(y: 50.0 - 30.0)..scale()..circle());
// Reverse scroll (30+20 pixels) and offset == notification.paintOffset.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, -20.0));
await tester.pump();
expect(painter, paints..save()..translate(y: 50.0 - 50.0)..scale()..circle());
// Reverse scroll (30+20+10 pixels) and offset > notification.paintOffset.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, -10.0));
await tester.pump();
// The OverscrollIndicator should not overflow the CustomScrollView's edge.
expect(painter, paints..save()..translate(y: 50.0 - 50.0)..scale()..circle());
await tester.pumpAndSettle(); // Finish the leading indicator.
// trigger the trailing indicator
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, -200.0));
expect(painter, paints..scale(y: -1.0)..save()..translate(y: 50.0)..scale()..circle());
// Reverse scroll (30 pixels), and the offset < notification.paintOffset.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, 30.0));
await tester.pump();
// The OverscrollIndicator should move with the CustomScrollView.
expect(painter, paints..scale(y: -1.0)..save()..translate(y: 50.0 - 30.0)..scale()..circle());
// Reverse scroll (30+20 pixels) and offset == notification.paintOffset.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, 20.0));
await tester.pump();
expect(painter, paints..scale(y: -1.0)..save()..translate(y: 50.0 - 50.0)..scale()..circle());
// Reverse scroll (30+20+10 pixels) and offset > notification.paintOffset.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, 10.0));
await tester.pump();
// The OverscrollIndicator should not overflow the CustomScrollView's edge.
expect(painter, paints..scale(y: -1.0)..save()..translate(y: 50.0 - 50.0)..scale()..circle());
});
group('[OverscrollIndicatorNotification.paintOffset] test', () {
testWidgets('Leading', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: NotificationListener<OverscrollIndicatorNotification>(
onNotification: (OverscrollIndicatorNotification notification) {
if (notification.leading) {
notification.paintOffset = 50.0;
}
return false;
},
child: const CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 2000.0)),
],
),
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, 5.0));
// The OverscrollIndicator should respect the [OverscrollIndicatorNotification.paintOffset] setting.
expect(painter, paints..save()..translate(y: 50.0)..scale()..circle());
// Reverse scroll direction.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, -30.0));
await tester.pump();
// The OverscrollIndicator should move with the CustomScrollView.
expect(painter, paints..save()..translate(y: 50.0 - 30.0)..scale()..circle());
});
testWidgets('Trailing', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: NotificationListener<OverscrollIndicatorNotification>(
onNotification: (OverscrollIndicatorNotification notification) {
if (!notification.leading) {
notification.paintOffset = 50.0;
}
return false;
},
child: const CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 2000.0)),
],
),
),
),
);
final RenderObject painter = tester.renderObject(find.byType(CustomPaint));
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(200.0, -10000.0));
await tester.pump();
await slowDrag(tester, const Offset(200.0, 200.0), const Offset(0.0, -5.0));
// The OverscrollIndicator should respect the [OverscrollIndicatorNotification.paintOffset] setting.
expect(painter, paints..scale(y: -1.0)..save()..translate(y: 50.0)..scale()..circle());
// Reverse scroll direction.
await tester.dragFrom(const Offset(200.0, 200.0), const Offset(0.0, 30.0));
await tester.pump();
// The OverscrollIndicator should move with the CustomScrollView.
expect(painter, paints..scale(y: -1.0)..save()..translate(y: 50.0 - 30.0)..scale()..circle());
});
});
}
class TestScrollBehavior1 extends ScrollBehavior {
const TestScrollBehavior1();
@override
Widget buildOverscrollIndicator(BuildContext context, Widget child, ScrollableDetails details) {
return GlowingOverscrollIndicator(
axisDirection: details.direction,
color: const Color(0xFF00FF00),
child: child,
);
}
}
class TestScrollBehavior2 extends ScrollBehavior {
const TestScrollBehavior2();
@override
Widget buildOverscrollIndicator(BuildContext context, Widget child, ScrollableDetails details) {
return GlowingOverscrollIndicator(
axisDirection: details.direction,
color: const Color(0xFF0000FF),
child: child,
);
}
}