nested_scroll_view_test.dart 30.1 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium 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 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

9 10
import '../rendering/mock_canvas.dart';

11 12 13 14 15
class _CustomPhysics extends ClampingScrollPhysics {
  const _CustomPhysics({ ScrollPhysics parent }) : super(parent: parent);

  @override
  _CustomPhysics applyTo(ScrollPhysics ancestor) {
16
    return _CustomPhysics(parent: buildParent(ancestor));
17 18 19 20
  }

  @override
  Simulation createBallisticSimulation(ScrollMetrics position, double dragVelocity) {
21
    return ScrollSpringSimulation(spring, 1000.0, 1000.0, 1000.0);
22 23 24
  }
}

25
Widget buildTest({ ScrollController controller, String title ='TTTTTTTT' }) {
26
  return Localizations(
27
    locale: const Locale('en', 'US'),
28
    delegates: const <LocalizationsDelegate<dynamic>>[
29 30 31
      DefaultMaterialLocalizations.delegate,
      DefaultWidgetsLocalizations.delegate,
    ],
32
    child: Directionality(
33
      textDirection: TextDirection.ltr,
34
      child: MediaQuery(
35
        data: const MediaQueryData(),
36 37
        child: Scaffold(
          body: DefaultTabController(
38
            length: 4,
39
            child: NestedScrollView(
40 41 42
              controller: controller,
              headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                return <Widget>[
43 44
                  SliverAppBar(
                    title: Text(title),
45 46 47 48
                    pinned: true,
                    expandedHeight: 200.0,
                    forceElevated: innerBoxIsScrolled,
                    bottom: const TabBar(
49 50 51 52 53
                      tabs: <Tab>[
                        Tab(text: 'AA'),
                        Tab(text: 'BB'),
                        Tab(text: 'CC'),
                        Tab(text: 'DD'),
54 55 56 57 58
                      ],
                    ),
                  ),
                ];
              },
59
              body: TabBarView(
60
                children: <Widget>[
61
                  ListView(
62
                    children: <Widget>[
63
                      Container(
64 65 66
                        height: 300.0,
                        child: const Text('aaa1'),
                      ),
67
                      Container(
68 69 70
                        height: 200.0,
                        child: const Text('aaa2'),
                      ),
71
                      Container(
72 73 74
                        height: 100.0,
                        child: const Text('aaa3'),
                      ),
75
                      Container(
76 77 78
                        height: 50.0,
                        child: const Text('aaa4'),
                      ),
79 80
                    ],
                  ),
81
                  ListView(
82
                    children: <Widget>[
83
                      Container(
84 85 86 87 88
                        height: 100.0,
                        child: const Text('bbb1'),
                      ),
                    ],
                  ),
89
                  Container(
90
                    child: const Center(child: Text('ccc1')),
91
                  ),
92
                  ListView(
93
                    children: <Widget>[
94
                      Container(
95 96 97 98 99 100 101
                        height: 10000.0,
                        child: const Text('ddd1'),
                      ),
                    ],
                  ),
                ],
              ),
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
          ),
        ),
      ),
    ),
  );
}

void main() {
  testWidgets('NestedScrollView overscroll and release and hold', (WidgetTester tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
    await tester.pumpWidget(buildTest());
    expect(find.text('aaa2'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 250));
    final Offset point1 = tester.getCenter(find.text('aaa1'));
    await tester.dragFrom(point1, const Offset(0.0, 200.0));
    await tester.pump();
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);
    await tester.flingFrom(point1, const Offset(0.0, -80.0), 50000.0);
    await tester.pump(const Duration(milliseconds: 20));
    final Offset point2 = tester.getCenter(find.text('aaa1'));
    expect(point2.dy, greaterThan(point1.dy));
    // TODO(ianh): Once we improve how we handle scrolling down from overscroll,
    // the following expectation should switch to 200.0.
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 120.0);
    debugDefaultTargetPlatformOverride = null;
  });
  testWidgets('NestedScrollView overscroll and release and hold', (WidgetTester tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
    await tester.pumpWidget(buildTest());
    expect(find.text('aaa2'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 250));
    final Offset point = tester.getCenter(find.text('aaa1'));
    await tester.flingFrom(point, const Offset(0.0, 200.0), 5000.0);
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    expect(find.text('aaa2'), findsNothing);
    final TestGesture gesture1 = await tester.startGesture(point);
    await tester.pump(const Duration(milliseconds: 5000));
    expect(find.text('aaa2'), findsNothing);
    await gesture1.moveBy(const Offset(0.0, 50.0));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    expect(find.text('aaa2'), findsNothing);
    await tester.pump(const Duration(milliseconds: 1000));
    debugDefaultTargetPlatformOverride = null;
  });
  testWidgets('NestedScrollView overscroll and release', (WidgetTester tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
    await tester.pumpWidget(buildTest());
    expect(find.text('aaa2'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 500));
    final TestGesture gesture1 = await tester.startGesture(tester.getCenter(find.text('aaa1')));
    await gesture1.moveBy(const Offset(0.0, 200.0));
    await tester.pumpAndSettle();
    expect(find.text('aaa2'), findsNothing);
    await tester.pump(const Duration(seconds: 1));
    await gesture1.up();
    await tester.pumpAndSettle();
    expect(find.text('aaa2'), findsOneWidget);
    debugDefaultTargetPlatformOverride = null;
  }, skip: true); // https://github.com/flutter/flutter/issues/9040
  testWidgets('NestedScrollView', (WidgetTester tester) async {
    await tester.pumpWidget(buildTest());
    expect(find.text('aaa2'), findsOneWidget);
    expect(find.text('aaa3'), findsNothing);
    expect(find.text('bbb1'), findsNothing);
    await tester.pump(const Duration(milliseconds: 250));
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);

    await tester.drag(find.text('AA'), const Offset(0.0, -20.0));
    await tester.pump(const Duration(milliseconds: 250));
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 180.0);

    await tester.drag(find.text('AA'), const Offset(0.0, -20.0));
    await tester.pump(const Duration(milliseconds: 250));
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 160.0);

    await tester.drag(find.text('AA'), const Offset(0.0, -20.0));
    await tester.pump(const Duration(milliseconds: 250));
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 140.0);

    expect(find.text('aaa4'), findsNothing);
    await tester.pump(const Duration(milliseconds: 250));
    await tester.fling(find.text('AA'), const Offset(0.0, -50.0), 10000.0);
    await tester.pumpAndSettle(const Duration(milliseconds: 250));
    expect(find.text('aaa4'), findsOneWidget);

    final double minHeight = tester.renderObject<RenderBox>(find.byType(AppBar)).size.height;
    expect(minHeight, lessThan(140.0));

    await tester.pump(const Duration(milliseconds: 250));
    await tester.tap(find.text('BB'));
    await tester.pumpAndSettle(const Duration(milliseconds: 250));
    expect(find.text('aaa4'), findsNothing);
    expect(find.text('bbb1'), findsOneWidget);

    await tester.pump(const Duration(milliseconds: 250));
    await tester.tap(find.text('CC'));
    await tester.pumpAndSettle(const Duration(milliseconds: 250));
    expect(find.text('bbb1'), findsNothing);
    expect(find.text('ccc1'), findsOneWidget);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, minHeight);

    await tester.pump(const Duration(milliseconds: 250));
    await tester.fling(find.text('AA'), const Offset(0.0, 50.0), 10000.0);
    await tester.pumpAndSettle(const Duration(milliseconds: 250));
    expect(find.text('ccc1'), findsOneWidget);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);
  });
213 214

  testWidgets('NestedScrollView with a ScrollController', (WidgetTester tester) async {
215
    final ScrollController controller = ScrollController(initialScrollOffset: 50.0);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

    double scrollOffset;
    controller.addListener(() {
      scrollOffset = controller.offset;
    });

    await tester.pumpWidget(buildTest(controller: controller));
    expect(controller.position.minScrollExtent, 0.0);
    expect(controller.position.pixels, 50.0);
    expect(controller.position.maxScrollExtent, 200.0);

    // The appbar's expandedHeight - initialScrollOffset = 150.
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 150.0);

    // Fully expand the appbar by scrolling (no animation) to 0.0.
    controller.jumpTo(0.0);
232
    await tester.pumpAndSettle();
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
    expect(scrollOffset, 0.0);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);

    // Scroll back to 50.0 animating over 100ms.
    controller.animateTo(50.0, duration: const Duration(milliseconds: 100), curve: Curves.linear);
    await tester.pump();
    await tester.pump();
    expect(scrollOffset, 0.0);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);
    await tester.pump(const Duration(milliseconds: 50)); // 50ms - halfway to scroll offset = 50.0.
    expect(scrollOffset, 25.0);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 175.0);
    await tester.pump(const Duration(milliseconds: 50)); // 100ms - all the way to scroll offset = 50.0.
    expect(scrollOffset, 50.0);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 150.0);

    // Scroll to the end, (we're not scrolling to the end of the list that contains aaa1,
    // just to the end of the outer scrollview). Verify that the first item in each tab
    // is still visible.
    controller.jumpTo(controller.position.maxScrollExtent);
    await tester.pumpAndSettle();
    expect(scrollOffset, 200.0);
    expect(find.text('aaa1'), findsOneWidget);

    await tester.tap(find.text('BB'));
    await tester.pumpAndSettle();
    expect(find.text('bbb1'), findsOneWidget);

    await tester.tap(find.text('CC'));
    await tester.pumpAndSettle();
    expect(find.text('ccc1'), findsOneWidget);

    await tester.tap(find.text('DD'));
    await tester.pumpAndSettle();
    expect(find.text('ddd1'), findsOneWidget);
  });

  testWidgets('Three NestedScrollViews with one ScrollController', (WidgetTester tester) async {
271
    final TrackingScrollController controller = TrackingScrollController();
272 273 274
    expect(controller.mostRecentlyUpdatedPosition, isNull);
    expect(controller.initialScrollOffset, 0.0);

275
    await tester.pumpWidget(Directionality(
276
      textDirection: TextDirection.ltr,
277
      child: PageView(
278 279 280 281 282 283
        children: <Widget>[
          buildTest(controller: controller, title: 'Page0'),
          buildTest(controller: controller, title: 'Page1'),
          buildTest(controller: controller, title: 'Page2'),
        ],
      ),
284
    ));
285

286
    // Initially Page0 is visible and Page0's appbar is fully expanded (height = 200.0).
287 288 289 290 291 292 293
    expect(find.text('Page0'), findsOneWidget);
    expect(find.text('Page1'), findsNothing);
    expect(find.text('Page2'), findsNothing);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);

    // A scroll collapses Page0's appbar to 150.0.
    controller.jumpTo(50.0);
294
    await tester.pumpAndSettle();
295 296 297 298
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 150.0);

    // Fling to Page1. Page1's appbar height is the same as the appbar for Page0.
    await tester.fling(find.text('Page0'), const Offset(-100.0, 0.0), 10000.0);
299
    await tester.pumpAndSettle();
300 301 302 303 304
    expect(find.text('Page0'), findsNothing);
    expect(find.text('Page1'), findsOneWidget);
    expect(find.text('Page2'), findsNothing);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 150.0);

305
    // Expand Page1's appbar and then fling to Page2. Page2's appbar appears
306 307
    // fully expanded.
    controller.jumpTo(0.0);
308
    await tester.pumpAndSettle();
309 310
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);
    await tester.fling(find.text('Page1'), const Offset(-100.0, 0.0), 10000.0);
311
    await tester.pumpAndSettle();
312 313 314 315 316 317
    expect(find.text('Page0'), findsNothing);
    expect(find.text('Page1'), findsNothing);
    expect(find.text('Page2'), findsOneWidget);
    expect(tester.renderObject<RenderBox>(find.byType(AppBar)).size.height, 200.0);
  });

318
  testWidgets('NestedScrollViews with custom physics', (WidgetTester tester) async {
319
    await tester.pumpWidget(Directionality(
320
      textDirection: TextDirection.ltr,
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
      child: Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: MediaQuery(
          data: const MediaQueryData(),
          child: NestedScrollView(
            physics: const _CustomPhysics(),
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                const SliverAppBar(
                  floating: true,
                  title: Text('AA'),
                ),
              ];
            },
            body: Container(),
          ),
341 342 343
        ),
      ),
    ));
344 345 346 347 348
    expect(find.text('AA'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 500));
    final Offset point1 = tester.getCenter(find.text('AA'));
    await tester.dragFrom(point1, const Offset(0.0, 200.0));
    await tester.pump(const Duration(milliseconds: 20));
349
    final Offset point2 = tester.getCenter(find.text('AA', skipOffstage: false));
350 351 352
    expect(point1.dy, greaterThan(point2.dy));
  });

353
  testWidgets('NestedScrollView and internal scrolling', (WidgetTester tester) async {
354
    debugDisableShadows = false;
355
    const List<String> _tabs = <String>['Hello', 'World'];
356
    int buildCount = 0;
357
    await tester.pumpWidget(
358
      MaterialApp(home: Material(child:
359
        // THE FOLLOWING SECTION IS FROM THE NestedScrollView DOCUMENTATION
360
        // (EXCEPT FOR THE CHANGES TO THE buildCount COUNTER)
361
        DefaultTabController(
362
          length: _tabs.length, // This is the number of tabs.
363
          child: NestedScrollView(
364
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
365
              buildCount += 1; // THIS LINE IS NOT IN THE ORIGINAL -- ADDED FOR TEST
366 367
              // These are the slivers that show up in the "outer" scroll view.
              return <Widget>[
368
                SliverOverlapAbsorber(
369 370 371 372 373 374 375 376
                  // This widget takes the overlapping behavior of the SliverAppBar,
                  // and redirects it to the SliverOverlapInjector below. If it is
                  // missing, then it is possible for the nested "inner" scroll view
                  // below to end up under the SliverAppBar even when the inner
                  // scroll view thinks it has not been scrolled.
                  // This is not necessary if the "headerSliverBuilder" only builds
                  // widgets that do not overlap the next sliver.
                  handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
377
                  child: SliverAppBar(
378 379 380 381 382 383 384 385 386 387 388 389
                    title: const Text('Books'), // This is the title in the app bar.
                    pinned: true,
                    expandedHeight: 150.0,
                    // The "forceElevated" property causes the SliverAppBar to show
                    // a shadow. The "innerBoxIsScrolled" parameter is true when the
                    // inner scroll view is scrolled beyond its "zero" point, i.e.
                    // when it appears to be scrolled below the SliverAppBar.
                    // Without this, there are cases where the shadow would appear
                    // or not appear inappropriately, because the SliverAppBar is
                    // not actually aware of the precise position of the inner
                    // scroll views.
                    forceElevated: innerBoxIsScrolled,
390
                    bottom: TabBar(
391
                      // These are the widgets to put in each tab in the tab bar.
392
                      tabs: _tabs.map<Widget>((String name) => Tab(text: name)).toList(),
393 394 395 396 397
                    ),
                  ),
                ),
              ];
            },
398
            body: TabBarView(
399
              // These are the contents of the tab views, below the tabs.
400
              children: _tabs.map<Widget>((String name) {
401
                return SafeArea(
402 403
                  top: false,
                  bottom: false,
404
                  child: Builder(
405 406 407 408
                    // This Builder is needed to provide a BuildContext that is "inside"
                    // the NestedScrollView, so that sliverOverlapAbsorberHandleFor() can
                    // find the NestedScrollView.
                    builder: (BuildContext context) {
409
                      return CustomScrollView(
410 411 412 413 414 415 416 417
                        // The "controller" and "primary" members should be left
                        // unset, so that the NestedScrollView can control this
                        // inner scroll view.
                        // If the "controller" property is set, then this scroll
                        // view will not be associated with the NestedScrollView.
                        // The PageStorageKey should be unique to this ScrollView;
                        // it allows the list to remember its scroll position when
                        // the tab view is not on the screen.
418
                        key: PageStorageKey<String>(name),
419
                        slivers: <Widget>[
420
                          SliverOverlapInjector(
421 422 423
                            // This is the flip side of the SliverOverlapAbsorber above.
                            handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                          ),
424
                          SliverPadding(
425 426 427 428 429
                            padding: const EdgeInsets.all(8.0),
                            // In this example, the inner scroll view has
                            // fixed-height list items, hence the use of
                            // SliverFixedExtentList. However, one could use any
                            // sliver widget here, e.g. SliverList or SliverGrid.
430
                            sliver: SliverFixedExtentList(
431 432 433 434
                              // The items in this example are fixed to 48 pixels
                              // high. This matches the Material Design spec for
                              // ListTile widgets.
                              itemExtent: 48.0,
435
                              delegate: SliverChildBuilderDelegate(
436 437 438
                                (BuildContext context, int index) {
                                  // This builder is called for each child.
                                  // In this example, we just number each list item.
439 440
                                  return ListTile(
                                    title: Text('Item $index'),
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
                                  );
                                },
                                // The childCount of the SliverChildBuilderDelegate
                                // specifies how many children this inner list
                                // has. In this example, each tab has a list of
                                // exactly 30 items, but this is arbitrary.
                                childCount: 30,
                              ),
                            ),
                          ),
                        ],
                      );
                    },
                  ),
                );
              }).toList(),
            ),
          ),
        )
        // END
      )),
    );
463 464 465
    int expectedBuildCount = 0;
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
466
    expect(find.text('Item 2'), findsOneWidget);
467
    expect(find.text('Item 18'), findsNothing);
468 469
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
    // scroll down
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    final TestGesture gesture0 = await tester.startGesture(tester.getCenter(find.text('Item 2')));
    await gesture0.moveBy(const Offset(0.0, -120.0)); // tiny bit more than the pinned app bar height (56px * 2)
    await tester.pump();
    expect(buildCount, expectedBuildCount);
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 18'), findsNothing);
    await gesture0.up();
    await tester.pump(const Duration(milliseconds: 1)); // start shadow animation
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
    await tester.pump(const Duration(milliseconds: 1)); // during shadow animation
    expect(buildCount, expectedBuildCount);
    expect(find.byType(NestedScrollView), paints..shadow());
    await tester.pump(const Duration(seconds: 1)); // end shadow animation
    expect(buildCount, expectedBuildCount);
    expect(find.byType(NestedScrollView), paints..shadow());
    // scroll down
487 488
    final TestGesture gesture1 = await tester.startGesture(tester.getCenter(find.text('Item 2')));
    await gesture1.moveBy(const Offset(0.0, -800.0));
489 490 491
    await tester.pump();
    expect(buildCount, expectedBuildCount);
    expect(find.byType(NestedScrollView), paints..shadow());
492 493 494
    expect(find.text('Item 2'), findsNothing);
    expect(find.text('Item 18'), findsOneWidget);
    await gesture1.up();
495 496
    await tester.pump(const Duration(seconds: 1));
    expect(buildCount, expectedBuildCount);
497 498 499 500 501
    expect(find.byType(NestedScrollView), paints..shadow());
    // swipe left to bring in tap on the right
    final TestGesture gesture2 = await tester.startGesture(tester.getCenter(find.byType(NestedScrollView)));
    await gesture2.moveBy(const Offset(-400.0, 0.0));
    await tester.pump();
502
    expect(buildCount, expectedBuildCount);
503 504 505 506 507 508 509 510
    expect(find.text('Item 18'), findsOneWidget);
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 0'), findsOneWidget);
    expect(tester.getTopLeft(find.ancestor(of: find.text('Item 0'), matching: find.byType(ListTile))).dy,
           tester.getBottomLeft(find.byType(AppBar)).dy + 8.0);
    expect(find.byType(NestedScrollView), paints..shadow());
    await gesture2.up();
    await tester.pump(); // start sideways scroll
511 512 513 514 515
    await tester.pump(const Duration(seconds: 1)); // end sideways scroll, triggers shadow going away
    expect(buildCount, expectedBuildCount);
    await tester.pump(const Duration(seconds: 1)); // start shadow going away
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
516
    await tester.pump(const Duration(seconds: 1)); // end shadow going away
517
    expect(buildCount, expectedBuildCount);
518 519 520
    expect(find.text('Item 18'), findsNothing);
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
521 522
    await tester.pump(const Duration(seconds: 1)); // just checking we don't rebuild...
    expect(buildCount, expectedBuildCount);
523 524 525 526
    // peek left to see it's still in the right place
    final TestGesture gesture3 = await tester.startGesture(tester.getCenter(find.byType(NestedScrollView)));
    await gesture3.moveBy(const Offset(400.0, 0.0));
    await tester.pump(); // bring the left page into view
527
    expect(buildCount, expectedBuildCount);
528
    await tester.pump(); // shadow comes back starting here
529 530
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
531 532 533 534
    expect(find.text('Item 18'), findsOneWidget);
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
    await tester.pump(const Duration(seconds: 1)); // shadow finishes coming back
535
    expect(buildCount, expectedBuildCount);
536 537 538 539
    expect(find.byType(NestedScrollView), paints..shadow());
    await gesture3.moveBy(const Offset(-400.0, 0.0));
    await gesture3.up();
    await tester.pump(); // left tab view goes away
540
    expect(buildCount, expectedBuildCount);
541
    await tester.pump(); // shadow goes away starting here
542 543
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
544 545
    expect(find.byType(NestedScrollView), paints..shadow());
    await tester.pump(const Duration(seconds: 1)); // shadow finishes going away
546
    expect(buildCount, expectedBuildCount);
547 548 549 550 551
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
    // scroll back up
    final TestGesture gesture4 = await tester.startGesture(tester.getCenter(find.byType(NestedScrollView)));
    await gesture4.moveBy(const Offset(0.0, 200.0)); // expands the appbar again
    await tester.pump();
552
    expect(buildCount, expectedBuildCount);
553 554 555 556 557
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 18'), findsNothing);
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
    await gesture4.up();
    await tester.pump(const Duration(seconds: 1));
558
    expect(buildCount, expectedBuildCount);
559 560 561 562 563 564
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
    // peek left to see it's now back at zero
    final TestGesture gesture5 = await tester.startGesture(tester.getCenter(find.byType(NestedScrollView)));
    await gesture5.moveBy(const Offset(400.0, 0.0));
    await tester.pump(); // bring the left page into view
    await tester.pump(); // shadow would come back starting here, but there's no shadow to show
565
    expect(buildCount, expectedBuildCount);
566 567 568 569 570 571 572 573
    expect(find.text('Item 18'), findsNothing);
    expect(find.text('Item 2'), findsNWidgets(2));
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
    await tester.pump(const Duration(seconds: 1)); // shadow would be finished coming back
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
    await gesture5.up();
    await tester.pump(); // right tab view goes away
    await tester.pumpAndSettle();
574
    expect(buildCount, expectedBuildCount);
575
    expect(find.byType(NestedScrollView), isNot(paints..shadow()));
576
    debugDisableShadows = true;
577
  });
578 579 580 581 582 583 584

  testWidgets('NestedScrollView and iOS bouncing', (WidgetTester tester) async {
    // This verifies that overscroll bouncing works correctly on iOS. For
    // example, this checks that if you pull to overscroll, friction is applied;
    // it also makes sure that if you scroll back the other way, the scroll
    // positions of the inner and outer list don't have a discontinuity.
    debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
585 586
    const Key key1 = ValueKey<int>(1);
    const Key key2 = ValueKey<int>(2);
587
    await tester.pumpWidget(
588 589 590
      MaterialApp(
        home: Material(
          child: DefaultTabController(
591
            length: 1,
592
            child: NestedScrollView(
593 594 595
              headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                return <Widget>[
                  const SliverPersistentHeader(
596
                    delegate: TestHeader(key: key1),
597 598 599
                  ),
                ];
              },
600 601
              body: SingleChildScrollView(
                child: Container(
602 603 604 605 606 607 608 609 610
                  height: 1000.0,
                  child: const Placeholder(key: key2),
                ),
              ),
            ),
          ),
        ),
      ),
    );
611 612
    expect(tester.getRect(find.byKey(key1)), Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
    expect(tester.getRect(find.byKey(key2)), Rect.fromLTWH(0.0, 100.0, 800.0, 1000.0));
613 614 615
    final TestGesture gesture = await tester.startGesture(const Offset(10.0, 10.0));
    await gesture.moveBy(const Offset(0.0, -10.0)); // scroll up
    await tester.pump();
616 617
    expect(tester.getRect(find.byKey(key1)), Rect.fromLTWH(0.0, -10.0, 800.0, 100.0));
    expect(tester.getRect(find.byKey(key2)), Rect.fromLTWH(0.0, 90.0, 800.0, 1000.0));
618 619
    await gesture.moveBy(const Offset(0.0, 10.0)); // scroll back to origin
    await tester.pump();
620 621
    expect(tester.getRect(find.byKey(key1)), Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
    expect(tester.getRect(find.byKey(key2)), Rect.fromLTWH(0.0, 100.0, 800.0, 1000.0));
622 623 624 625
    await gesture.moveBy(const Offset(0.0, 10.0)); // overscroll
    await gesture.moveBy(const Offset(0.0, 10.0)); // overscroll
    await gesture.moveBy(const Offset(0.0, 10.0)); // overscroll
    await tester.pump();
626
    expect(tester.getRect(find.byKey(key1)), Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
627 628 629 630
    expect(tester.getRect(find.byKey(key2)).top, greaterThan(100.0));
    expect(tester.getRect(find.byKey(key2)).top, lessThan(130.0));
    await gesture.moveBy(const Offset(0.0, -1.0)); // scroll back a little
    await tester.pump();
631
    expect(tester.getRect(find.byKey(key1)), Rect.fromLTWH(0.0, -1.0, 800.0, 100.0));
632 633 634 635
    expect(tester.getRect(find.byKey(key2)).top, greaterThan(100.0));
    expect(tester.getRect(find.byKey(key2)).top, lessThan(129.0));
    await gesture.moveBy(const Offset(0.0, -10.0)); // scroll back a lot
    await tester.pump();
636
    expect(tester.getRect(find.byKey(key1)), Rect.fromLTWH(0.0, -11.0, 800.0, 100.0));
637 638
    await gesture.moveBy(const Offset(0.0, 20.0)); // overscroll again
    await tester.pump();
639
    expect(tester.getRect(find.byKey(key1)), Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
640 641 642
    await gesture.up();
    debugDefaultTargetPlatformOverride = null;
  });
643
}
644 645 646 647 648 649 650 651 652 653

class TestHeader extends SliverPersistentHeaderDelegate {
  const TestHeader({ this.key });
  final Key key;
  @override
  double get minExtent => 100.0;
  @override
  double get maxExtent => 100.0;
  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
654
    return Placeholder(key: key);
655 656 657
  }
  @override
  bool shouldRebuild(TestHeader oldDelegate) => false;
658
}