nested_scroll_view_test.dart 31.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// 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';
8
import 'package:flutter/gestures.dart' show DragStartBehavior;
9
import 'package:flutter/rendering.dart';
10

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
        child: Scaffold(
37
          drawerDragStartBehavior: DragStartBehavior.down,
38
          body: DefaultTabController(
39
            length: 4,
40
            child: NestedScrollView(
41
              dragStartBehavior: DragStartBehavior.down,
42 43 44
              controller: controller,
              headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                return <Widget>[
45 46
                  SliverAppBar(
                    title: Text(title),
47 48 49 50
                    pinned: true,
                    expandedHeight: 200.0,
                    forceElevated: innerBoxIsScrolled,
                    bottom: const TabBar(
51 52 53 54 55
                      tabs: <Tab>[
                        Tab(text: 'AA'),
                        Tab(text: 'BB'),
                        Tab(text: 'CC'),
                        Tab(text: 'DD'),
56 57 58 59 60
                      ],
                    ),
                  ),
                ];
              },
61
              body: TabBarView(
62
                children: <Widget>[
63
                  ListView(
64
                    children: <Widget>[
65
                      Container(
66 67 68
                        height: 300.0,
                        child: const Text('aaa1'),
                      ),
69
                      Container(
70 71 72
                        height: 200.0,
                        child: const Text('aaa2'),
                      ),
73
                      Container(
74 75 76
                        height: 100.0,
                        child: const Text('aaa3'),
                      ),
77
                      Container(
78 79 80
                        height: 50.0,
                        child: const Text('aaa4'),
                      ),
81 82
                    ],
                  ),
83
                  ListView(
84
                    dragStartBehavior: DragStartBehavior.down,
85
                    children: <Widget>[
86
                      Container(
87 88 89 90 91
                        height: 100.0,
                        child: const Text('bbb1'),
                      ),
                    ],
                  ),
92
                  Container(
93
                    child: const Center(child: Text('ccc1')),
94
                  ),
95
                  ListView(
96
                    dragStartBehavior: DragStartBehavior.down,
97
                    children: <Widget>[
98
                      Container(
99 100 101 102 103 104 105
                        height: 10000.0,
                        child: const Text('ddd1'),
                      ),
                    ],
                  ),
                ],
              ),
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
          ),
        ),
      ),
    ),
  );
}

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);
  });
217 218

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

    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);
236
    await tester.pumpAndSettle();
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
    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 {
275
    final TrackingScrollController controller = TrackingScrollController();
276 277 278
    expect(controller.mostRecentlyUpdatedPosition, isNull);
    expect(controller.initialScrollOffset, 0.0);

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

290
    // Initially Page0 is visible and Page0's appbar is fully expanded (height = 200.0).
291 292 293 294 295 296 297
    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);
298
    await tester.pumpAndSettle();
299 300 301 302
    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);
303
    await tester.pumpAndSettle();
304 305 306 307 308
    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);

309
    // Expand Page1's appbar and then fling to Page2. Page2's appbar appears
310 311
    // fully expanded.
    controller.jumpTo(0.0);
312
    await tester.pumpAndSettle();
313 314
    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);
315
    await tester.pumpAndSettle();
316 317 318 319 320 321
    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);
  });

322
  testWidgets('NestedScrollViews with custom physics', (WidgetTester tester) async {
323
    await tester.pumpWidget(Directionality(
324
      textDirection: TextDirection.ltr,
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
      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(),
          ),
345 346 347
        ),
      ),
    ));
348 349 350 351 352
    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));
353
    final Offset point2 = tester.getCenter(find.text('AA', skipOffstage: false));
354 355 356
    expect(point1.dy, greaterThan(point2.dy));
  });

357
  testWidgets('NestedScrollView and internal scrolling', (WidgetTester tester) async {
358
    debugDisableShadows = false;
359
    const List<String> _tabs = <String>['Hello', 'World'];
360
    int buildCount = 0;
361
    await tester.pumpWidget(
362
      MaterialApp(home: Material(child:
363
        // THE FOLLOWING SECTION IS FROM THE NestedScrollView DOCUMENTATION
364
        // (EXCEPT FOR THE CHANGES TO THE buildCount COUNTER)
365
        DefaultTabController(
366
          length: _tabs.length, // This is the number of tabs.
367
          child: NestedScrollView(
368
            dragStartBehavior: DragStartBehavior.down,
369
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
370
              buildCount += 1; // THIS LINE IS NOT IN THE ORIGINAL -- ADDED FOR TEST
371 372
              // These are the slivers that show up in the "outer" scroll view.
              return <Widget>[
373
                SliverOverlapAbsorber(
374 375 376 377 378 379 380 381
                  // 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),
382
                  sliver: SliverAppBar(
383 384 385 386 387 388 389 390 391 392 393 394
                    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,
395
                    bottom: TabBar(
396
                      // These are the widgets to put in each tab in the tab bar.
397
                      tabs: _tabs.map<Widget>((String name) => Tab(text: name)).toList(),
398
                      dragStartBehavior: DragStartBehavior.down,
399 400 401 402 403
                    ),
                  ),
                ),
              ];
            },
404
            body: TabBarView(
405
              dragStartBehavior: DragStartBehavior.down,
406
              // These are the contents of the tab views, below the tabs.
407
              children: _tabs.map<Widget>((String name) {
408
                return SafeArea(
409 410
                  top: false,
                  bottom: false,
411
                  child: Builder(
412 413 414 415
                    // This Builder is needed to provide a BuildContext that is "inside"
                    // the NestedScrollView, so that sliverOverlapAbsorberHandleFor() can
                    // find the NestedScrollView.
                    builder: (BuildContext context) {
416
                      return CustomScrollView(
417 418 419 420 421 422 423 424
                        // 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.
425
                        key: PageStorageKey<String>(name),
426
                        dragStartBehavior: DragStartBehavior.down,
427
                        slivers: <Widget>[
428
                          SliverOverlapInjector(
429 430 431
                            // This is the flip side of the SliverOverlapAbsorber above.
                            handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                          ),
432
                          SliverPadding(
433 434 435 436 437
                            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.
438
                            sliver: SliverFixedExtentList(
439 440 441 442
                              // The items in this example are fixed to 48 pixels
                              // high. This matches the Material Design spec for
                              // ListTile widgets.
                              itemExtent: 48.0,
443
                              delegate: SliverChildBuilderDelegate(
444 445 446
                                (BuildContext context, int index) {
                                  // This builder is called for each child.
                                  // In this example, we just number each list item.
447 448
                                  return ListTile(
                                    title: Text('Item $index'),
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
                                  );
                                },
                                // 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(),
            ),
          ),
467
        ),
468 469 470
        // END
      )),
    );
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

    PhysicalModelLayer _dfsFindPhysicalLayer(ContainerLayer layer) {
      expect(layer, isNotNull);
      Layer child = layer.firstChild;
      while (child != null) {
        if (child is PhysicalModelLayer) {
          return child;
        }
        if (child is ContainerLayer) {
          final PhysicalModelLayer candidate = _dfsFindPhysicalLayer(child);
          if (candidate != null) {
            return candidate;
          }
        }
        child = child.nextSibling;
      }
      return null;
    }

    final ContainerLayer nestedScrollViewLayer = find.byType(NestedScrollView).evaluate().first.renderObject.debugLayer;
    void _checkPhysicalLayer({@required double elevation}) {
      final PhysicalModelLayer layer = _dfsFindPhysicalLayer(nestedScrollViewLayer);
      expect(layer, isNotNull);
      expect(layer.elevation, equals(elevation));
    }

497 498 499
    int expectedBuildCount = 0;
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
500
    expect(find.text('Item 2'), findsOneWidget);
501
    expect(find.text('Item 18'), findsNothing);
502
    _checkPhysicalLayer(elevation: 0);
503
    // scroll down
504 505 506 507 508 509 510 511 512 513 514 515
    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);
516
    _checkPhysicalLayer(elevation: 0.00018262863159179688);
517 518
    await tester.pump(const Duration(seconds: 1)); // end shadow animation
    expect(buildCount, expectedBuildCount);
519
    _checkPhysicalLayer(elevation: 4);
520
    // scroll down
521 522
    final TestGesture gesture1 = await tester.startGesture(tester.getCenter(find.text('Item 2')));
    await gesture1.moveBy(const Offset(0.0, -800.0));
523 524
    await tester.pump();
    expect(buildCount, expectedBuildCount);
525
    _checkPhysicalLayer(elevation: 4);
526 527 528
    expect(find.text('Item 2'), findsNothing);
    expect(find.text('Item 18'), findsOneWidget);
    await gesture1.up();
529 530
    await tester.pump(const Duration(seconds: 1));
    expect(buildCount, expectedBuildCount);
531
    _checkPhysicalLayer(elevation: 4);
532 533 534 535
    // 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();
536
    expect(buildCount, expectedBuildCount);
537 538 539 540 541
    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);
542
    _checkPhysicalLayer(elevation: 4);
543 544
    await gesture2.up();
    await tester.pump(); // start sideways scroll
545 546 547 548 549
    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);
550
    await tester.pump(const Duration(seconds: 1)); // end shadow going away
551
    expect(buildCount, expectedBuildCount);
552 553
    expect(find.text('Item 18'), findsNothing);
    expect(find.text('Item 2'), findsOneWidget);
554
    _checkPhysicalLayer(elevation: 0);
555 556
    await tester.pump(const Duration(seconds: 1)); // just checking we don't rebuild...
    expect(buildCount, expectedBuildCount);
557 558 559 560
    // 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
561
    expect(buildCount, expectedBuildCount);
562
    await tester.pump(); // shadow comes back starting here
563 564
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
565 566
    expect(find.text('Item 18'), findsOneWidget);
    expect(find.text('Item 2'), findsOneWidget);
567
    _checkPhysicalLayer(elevation: 0);
568
    await tester.pump(const Duration(seconds: 1)); // shadow finishes coming back
569
    expect(buildCount, expectedBuildCount);
570
    _checkPhysicalLayer(elevation: 4);
571 572 573
    await gesture3.moveBy(const Offset(-400.0, 0.0));
    await gesture3.up();
    await tester.pump(); // left tab view goes away
574
    expect(buildCount, expectedBuildCount);
575
    await tester.pump(); // shadow goes away starting here
576 577
    expectedBuildCount += 1;
    expect(buildCount, expectedBuildCount);
578
    _checkPhysicalLayer(elevation: 4);
579
    await tester.pump(const Duration(seconds: 1)); // shadow finishes going away
580
    expect(buildCount, expectedBuildCount);
581
    _checkPhysicalLayer(elevation: 0);
582 583 584 585
    // 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();
586
    expect(buildCount, expectedBuildCount);
587 588
    expect(find.text('Item 2'), findsOneWidget);
    expect(find.text('Item 18'), findsNothing);
589
    _checkPhysicalLayer(elevation: 0);
590 591
    await gesture4.up();
    await tester.pump(const Duration(seconds: 1));
592
    expect(buildCount, expectedBuildCount);
593
    _checkPhysicalLayer(elevation: 0);
594 595 596 597 598
    // 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
599
    expect(buildCount, expectedBuildCount);
600 601
    expect(find.text('Item 18'), findsNothing);
    expect(find.text('Item 2'), findsNWidgets(2));
602
    _checkPhysicalLayer(elevation: 0);
603
    await tester.pump(const Duration(seconds: 1)); // shadow would be finished coming back
604
    _checkPhysicalLayer(elevation: 0);
605 606 607
    await gesture5.up();
    await tester.pump(); // right tab view goes away
    await tester.pumpAndSettle();
608
    expect(buildCount, expectedBuildCount);
609
    _checkPhysicalLayer(elevation: 0);
610
    debugDisableShadows = true;
611
  });
612 613 614 615 616 617 618

  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;
619 620
    const Key key1 = ValueKey<int>(1);
    const Key key2 = ValueKey<int>(2);
621
    await tester.pumpWidget(
622 623 624
      MaterialApp(
        home: Material(
          child: DefaultTabController(
625
            length: 1,
626
            child: NestedScrollView(
627
              dragStartBehavior: DragStartBehavior.down,
628 629 630
              headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                return <Widget>[
                  const SliverPersistentHeader(
631
                    delegate: TestHeader(key: key1),
632 633 634
                  ),
                ];
              },
635
              body: SingleChildScrollView(
636
                dragStartBehavior: DragStartBehavior.down,
637
                child: Container(
638 639 640 641 642 643 644 645 646
                  height: 1000.0,
                  child: const Placeholder(key: key2),
                ),
              ),
            ),
          ),
        ),
      ),
    );
Dan Field's avatar
Dan Field committed
647 648
    expect(tester.getRect(find.byKey(key1)), const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
    expect(tester.getRect(find.byKey(key2)), const Rect.fromLTWH(0.0, 100.0, 800.0, 1000.0));
649 650 651
    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();
Dan Field's avatar
Dan Field committed
652 653
    expect(tester.getRect(find.byKey(key1)), const Rect.fromLTWH(0.0, -10.0, 800.0, 100.0));
    expect(tester.getRect(find.byKey(key2)), const Rect.fromLTWH(0.0, 90.0, 800.0, 1000.0));
654 655
    await gesture.moveBy(const Offset(0.0, 10.0)); // scroll back to origin
    await tester.pump();
Dan Field's avatar
Dan Field committed
656 657
    expect(tester.getRect(find.byKey(key1)), const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
    expect(tester.getRect(find.byKey(key2)), const Rect.fromLTWH(0.0, 100.0, 800.0, 1000.0));
658 659 660 661
    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();
Dan Field's avatar
Dan Field committed
662
    expect(tester.getRect(find.byKey(key1)), const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
663 664 665 666
    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();
Dan Field's avatar
Dan Field committed
667
    expect(tester.getRect(find.byKey(key1)), const Rect.fromLTWH(0.0, -1.0, 800.0, 100.0));
668 669 670 671
    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();
Dan Field's avatar
Dan Field committed
672
    expect(tester.getRect(find.byKey(key1)), const Rect.fromLTWH(0.0, -11.0, 800.0, 100.0));
673 674
    await gesture.moveBy(const Offset(0.0, 20.0)); // overscroll again
    await tester.pump();
Dan Field's avatar
Dan Field committed
675
    expect(tester.getRect(find.byKey(key1)), const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0));
676 677 678
    await gesture.up();
    debugDefaultTargetPlatformOverride = null;
  });
679
}
680 681 682 683 684 685 686 687 688 689

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) {
690
    return Placeholder(key: key);
691 692 693
  }
  @override
  bool shouldRebuild(TestHeader oldDelegate) => false;
694
}