page_view_test.dart 43.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth's avatar
Adam Barth committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/gestures.dart' show DragStartBehavior;
7
import 'package:flutter/material.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter_test/flutter_test.dart';
10
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
Adam Barth's avatar
Adam Barth committed
11

12
import '../rendering/rendering_tester.dart' show TestClipPaintingContext;
13
import 'semantics_tester.dart';
Adam Barth's avatar
Adam Barth committed
14 15 16
import 'states.dart';

void main() {
17
  // Regression test for https://github.com/flutter/flutter/issues/100451
18
  testWidgetsWithLeakTracking('PageView.builder respects findChildIndexCallback', (WidgetTester tester) async {
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
    bool finderCalled = false;
    int itemCount = 7;
    late StateSetter stateSetter;

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            stateSetter = setState;
            return PageView.builder(
              itemCount: itemCount,
              itemBuilder: (BuildContext _, int index) => Container(
                key: Key('$index'),
                height: 2000.0,
              ),
              findChildIndexCallback: (Key key) {
                finderCalled = true;
                return null;
              },
            );
          },
        ),
      )
    );
    expect(finderCalled, false);

    // Trigger update.
    stateSetter(() => itemCount = 77);
    await tester.pump();

    expect(finderCalled, true);
  });

53
  testWidgetsWithLeakTracking('PageView resize from zero-size viewport should not lose state', (WidgetTester tester) async {
54
    // Regression test for https://github.com/flutter/flutter/issues/88956
55 56
    final PageController controller = PageController(initialPage: 1);
    addTearDown(controller.dispose);
57 58 59 60 61 62 63 64 65 66

    Widget build(Size size) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: SizedBox.fromSize(
            size: size,
            child: PageView(
              controller: controller,
              onPageChanged: (int page) { },
67
              children: kStates.map<Widget>((String state) => Text(state)).toList(),
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
            ),
          ),
        ),
      );
    }

    // The pageView have a zero viewport, so nothing display.
    await tester.pumpWidget(build(Size.zero));
    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alabama', skipOffstage: false), findsOneWidget);

    // Resize from zero viewport to non-zero, the controller's initialPage 1 will display.
    await tester.pumpWidget(build(const Size(200.0, 200.0)));
    expect(find.text('Alaska'), findsOneWidget);

    // Jump to page 'Iowa'.
    controller.jumpToPage(kStates.indexOf('Iowa'));
    await tester.pump();
    expect(find.text('Iowa'), findsOneWidget);

    // Resize to zero viewport again, nothing display.
    await tester.pumpWidget(build(Size.zero));
    expect(find.text('Iowa'), findsNothing);

    // Resize from zero to non-zero, the pageView should not lose state, so the page 'Iowa' show again.
    await tester.pumpWidget(build(const Size(200.0, 200.0)));
    expect(find.text('Iowa'), findsOneWidget);
  });

97
  testWidgetsWithLeakTracking('Change the page through the controller when zero-size viewport', (WidgetTester tester) async {
98
    // Regression test for https://github.com/flutter/flutter/issues/88956
99 100
    final PageController controller = PageController(initialPage: 1);
    addTearDown(controller.dispose);
101 102 103 104 105 106 107 108 109 110

    Widget build(Size size) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: SizedBox.fromSize(
            size: size,
            child: PageView(
              controller: controller,
              onPageChanged: (int page) { },
111
              children: kStates.map<Widget>((String state) => Text(state)).toList(),
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            ),
          ),
        ),
      );
    }

    // The pageView have a zero viewport, so nothing display.
    await tester.pumpWidget(build(Size.zero));
    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alabama', skipOffstage: false), findsOneWidget);

    // Change the page through the page controller when zero viewport
    controller.animateToPage(kStates.indexOf('Iowa'), duration: kTabScrollDuration, curve: Curves.ease);
    expect(controller.page, kStates.indexOf('Iowa'));

    controller.jumpToPage(kStates.indexOf('Illinois'));
    expect(controller.page, kStates.indexOf('Illinois'));

    // Resize from zero viewport to non-zero, the latest state should not lost.
    await tester.pumpWidget(build(const Size(200.0, 200.0)));
    expect(controller.page, kStates.indexOf('Illinois'));
    expect(find.text('Illinois'), findsOneWidget);
  });

136
  testWidgetsWithLeakTracking('_PagePosition.applyViewportDimension should not throw', (WidgetTester tester) async {
137
    // Regression test for https://github.com/flutter/flutter/issues/101007
138 139
    final PageController controller = PageController(initialPage: 1);
    addTearDown(controller.dispose);
140 141 142

    // Set the starting viewportDimension to 0.0
    await tester.binding.setSurfaceSize(Size.zero);
143
    final MediaQueryData mediaQueryData = MediaQueryData.fromView(tester.view);
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

    Widget build(Size size) {
      return MediaQuery(
        data: mediaQueryData.copyWith(size: size),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: SizedBox.expand(
              child: PageView(
                controller: controller,
                onPageChanged: (int page) { },
                children: kStates.map<Widget>((String state) => Text(state)).toList(),
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(build(Size.zero));
    const Size surfaceSize = Size(500,400);
    await tester.binding.setSurfaceSize(surfaceSize);
    await tester.pumpWidget(build(surfaceSize));

    expect(tester.takeException(), isNull);

    // Reset TestWidgetsFlutterBinding surfaceSize
    await tester.binding.setSurfaceSize(null);
  });

174
  testWidgetsWithLeakTracking('PageController cannot return page while unattached',
175 176
      (WidgetTester tester) async {
    final PageController controller = PageController();
177
    addTearDown(controller.dispose);
178 179 180
    expect(() => controller.page, throwsAssertionError);
  });

181
  testWidgetsWithLeakTracking('PageView control test', (WidgetTester tester) async {
182
    final List<String> log = <String>[];
Adam Barth's avatar
Adam Barth committed
183

184
    await tester.pumpWidget(Directionality(
185
      textDirection: TextDirection.ltr,
186
      child: PageView(
187
        dragStartBehavior: DragStartBehavior.down,
188
        children: kStates.map<Widget>((String state) {
189
          return GestureDetector(
190
            dragStartBehavior: DragStartBehavior.down,
191 192 193
            onTap: () {
              log.add(state);
            },
194
            child: Container(
195 196
              height: 200.0,
              color: const Color(0xFF0000FF),
197
              child: Text(state),
198 199 200 201
            ),
          );
        }).toList(),
      ),
Adam Barth's avatar
Adam Barth committed
202 203 204 205 206 207 208 209
    ));

    await tester.tap(find.text('Alabama'));
    expect(log, equals(<String>['Alabama']));
    log.clear();

    expect(find.text('Alaska'), findsNothing);

210
    await tester.drag(find.byType(PageView), const Offset(-20.0, 0.0));
Adam Barth's avatar
Adam Barth committed
211 212 213 214 215 216
    await tester.pump();

    expect(find.text('Alabama'), findsOneWidget);
    expect(find.text('Alaska'), findsOneWidget);
    expect(find.text('Arizona'), findsNothing);

217
    await tester.pumpAndSettle();
Adam Barth's avatar
Adam Barth committed
218 219 220 221

    expect(find.text('Alabama'), findsOneWidget);
    expect(find.text('Alaska'), findsNothing);

222
    await tester.drag(find.byType(PageView), const Offset(-401.0, 0.0));
223
    await tester.pumpAndSettle();
Adam Barth's avatar
Adam Barth committed
224 225 226 227 228 229 230 231 232

    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alaska'), findsOneWidget);
    expect(find.text('Arizona'), findsNothing);

    await tester.tap(find.text('Alaska'));
    expect(log, equals(<String>['Alaska']));
    log.clear();

233
    await tester.fling(find.byType(PageView), const Offset(-200.0, 0.0), 1000.0);
234
    await tester.pumpAndSettle();
Adam Barth's avatar
Adam Barth committed
235 236 237 238 239 240

    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alaska'), findsNothing);
    expect(find.text('Arizona'), findsOneWidget);

    await tester.fling(find.byType(PageView), const Offset(200.0, 0.0), 1000.0);
241
    await tester.pumpAndSettle();
Adam Barth's avatar
Adam Barth committed
242 243 244 245 246

    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alaska'), findsOneWidget);
    expect(find.text('Arizona'), findsNothing);
  });
247

248
  testWidgetsWithLeakTracking('PageView does not squish when overscrolled', (WidgetTester tester) async {
249 250 251 252 253
    await tester.pumpWidget(MaterialApp(
      home: PageView(
        children: List<Widget>.generate(10, (int i) {
          return Container(
            key: ValueKey<int>(i),
254
            color: const Color(0xFF0000FF),
255 256 257 258 259
          );
        }),
      ),
    ));

260 261
    Size sizeOf(int i) => tester.getSize(find.byKey(ValueKey<int>(i)));
    double leftOf(int i) => tester.getTopLeft(find.byKey(ValueKey<int>(i))).dx;
262 263 264 265

    expect(leftOf(0), equals(0.0));
    expect(sizeOf(0), equals(const Size(800.0, 600.0)));

266
    // Going into overscroll.
267
    await tester.drag(find.byType(PageView), const Offset(100.0, 0.0));
268 269
    await tester.pump();

270
    expect(leftOf(0), greaterThan(0.0));
271 272
    expect(sizeOf(0), equals(const Size(800.0, 600.0)));

273
    // Easing overscroll past overscroll limit.
274 275 276 277 278 279
    if (debugDefaultTargetPlatformOverride == TargetPlatform.macOS) {
      await tester.drag(find.byType(PageView), const Offset(-500.0, 0.0));
    }
    else {
      await tester.drag(find.byType(PageView), const Offset(-200.0, 0.0));
    }
280 281
    await tester.pump();

282
    expect(leftOf(0), lessThan(0.0));
283
    expect(sizeOf(0), equals(const Size(800.0, 600.0)));
Dan Field's avatar
Dan Field committed
284
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
285

286
  testWidgetsWithLeakTracking('PageController control test', (WidgetTester tester) async {
287
    final PageController controller = PageController(initialPage: 4);
288
    addTearDown(controller.dispose);
289

290
    await tester.pumpWidget(Directionality(
291
      textDirection: TextDirection.ltr,
292 293
      child: Center(
        child: SizedBox(
294 295
          width: 600.0,
          height: 400.0,
296
          child: PageView(
297
            controller: controller,
298
            children: kStates.map<Widget>((String state) => Text(state)).toList(),
299
          ),
300 301 302 303 304 305
        ),
      ),
    ));

    expect(find.text('California'), findsOneWidget);

306
    controller.nextPage(duration: const Duration(milliseconds: 150), curve: Curves.ease);
307
    await tester.pumpAndSettle();
308 309 310

    expect(find.text('Colorado'), findsOneWidget);

311
    await tester.pumpWidget(Directionality(
312
      textDirection: TextDirection.ltr,
313 314
      child: Center(
        child: SizedBox(
315 316
          width: 300.0,
          height: 400.0,
317
          child: PageView(
318
            controller: controller,
319
            children: kStates.map<Widget>((String state) => Text(state)).toList(),
320
          ),
321 322 323 324 325 326
        ),
      ),
    ));

    expect(find.text('Colorado'), findsOneWidget);

327
    controller.previousPage(duration: const Duration(milliseconds: 150), curve: Curves.ease);
328
    await tester.pumpAndSettle();
329 330 331 332

    expect(find.text('California'), findsOneWidget);
  });

333
  testWidgetsWithLeakTracking('PageController page stability', (WidgetTester tester) async {
334
    await tester.pumpWidget(Directionality(
335
      textDirection: TextDirection.ltr,
336 337
      child: Center(
        child: SizedBox(
338 339
          width: 600.0,
          height: 400.0,
340 341
          child: PageView(
            children: kStates.map<Widget>((String state) => Text(state)).toList(),
342
          ),
343
        ),
344 345 346 347 348
      ),
    ));

    expect(find.text('Alabama'), findsOneWidget);

349
    await tester.drag(find.byType(PageView), const Offset(-1250.0, 0.0));
350
    await tester.pumpAndSettle();
351 352 353

    expect(find.text('Arizona'), findsOneWidget);

354
    await tester.pumpWidget(Directionality(
355
      textDirection: TextDirection.ltr,
356 357
      child: Center(
        child: SizedBox(
358 359
          width: 250.0,
          height: 100.0,
360 361
          child: PageView(
            children: kStates.map<Widget>((String state) => Text(state)).toList(),
362
          ),
363 364 365 366 367 368
        ),
      ),
    ));

    expect(find.text('Arizona'), findsOneWidget);

369
    await tester.pumpWidget(Directionality(
370
      textDirection: TextDirection.ltr,
371 372
      child: Center(
        child: SizedBox(
373 374
          width: 450.0,
          height: 400.0,
375 376
          child: PageView(
            children: kStates.map<Widget>((String state) => Text(state)).toList(),
377
          ),
378 379 380 381 382 383
        ),
      ),
    ));

    expect(find.text('Arizona'), findsOneWidget);
  });
384

385
  testWidgetsWithLeakTracking('PageController nextPage and previousPage return Futures that resolve', (WidgetTester tester) async {
386
    final PageController controller = PageController();
387 388
    addTearDown(controller.dispose);

389
    await tester.pumpWidget(Directionality(
390
        textDirection: TextDirection.ltr,
391
        child: PageView(
392
          controller: controller,
393
          children: kStates.map<Widget>((String state) => Text(state)).toList(),
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
        ),
    ));

    bool nextPageCompleted = false;
    controller.nextPage(duration: const Duration(milliseconds: 150), curve: Curves.ease)
        .then((_) => nextPageCompleted = true);

    expect(nextPageCompleted, false);
    await tester.pump(const Duration(milliseconds: 200));
    expect(nextPageCompleted, false);
    await tester.pump(const Duration(milliseconds: 200));
    expect(nextPageCompleted, true);


    bool previousPageCompleted = false;
    controller.previousPage(duration: const Duration(milliseconds: 150), curve: Curves.ease)
        .then((_) => previousPageCompleted = true);

    expect(previousPageCompleted, false);
    await tester.pump(const Duration(milliseconds: 200));
    expect(previousPageCompleted, false);
    await tester.pump(const Duration(milliseconds: 200));
    expect(previousPageCompleted, true);
  });

419
  testWidgetsWithLeakTracking('PageView in zero-size container', (WidgetTester tester) async {
420
    await tester.pumpWidget(Directionality(
421
      textDirection: TextDirection.ltr,
422
      child: Center(
423
        child: SizedBox.shrink(
424 425
          child: PageView(
            children: kStates.map<Widget>((String state) => Text(state)).toList(),
426
          ),
427 428 429 430
        ),
      ),
    ));

431
    expect(find.text('Alabama', skipOffstage: false), findsOneWidget);
432

433
    await tester.pumpWidget(Directionality(
434
      textDirection: TextDirection.ltr,
435 436
      child: Center(
        child: SizedBox(
437 438
          width: 200.0,
          height: 200.0,
439 440
          child: PageView(
            children: kStates.map<Widget>((String state) => Text(state)).toList(),
441
          ),
442 443 444 445 446
        ),
      ),
    ));

    expect(find.text('Alabama'), findsOneWidget);
447
  });
448

449
  testWidgetsWithLeakTracking('Page changes at halfway point', (WidgetTester tester) async {
450
    final List<int> log = <int>[];
451
    await tester.pumpWidget(Directionality(
452
      textDirection: TextDirection.ltr,
453
      child: PageView(
454
        onPageChanged: log.add,
455
        children: kStates.map<Widget>((String state) => Text(state)).toList(),
456
      ),
457 458 459 460
    ));

    expect(log, isEmpty);

461
    final TestGesture gesture =
462
        await tester.startGesture(const Offset(100.0, 100.0));
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
    // The page view is 800.0 wide, so this move is just short of halfway.
    await gesture.moveBy(const Offset(-380.0, 0.0));

    expect(log, isEmpty);

    // We've crossed the halfway mark.
    await gesture.moveBy(const Offset(-40.0, 0.0));

    expect(log, equals(const <int>[1]));
    log.clear();

    // Moving a bit more should not generate redundant notifications.
    await gesture.moveBy(const Offset(-40.0, 0.0));

    expect(log, isEmpty);

    await gesture.moveBy(const Offset(-40.0, 0.0));
    await tester.pump();

    await gesture.moveBy(const Offset(-40.0, 0.0));
    await tester.pump();

    await gesture.moveBy(const Offset(-40.0, 0.0));
    await tester.pump();

    expect(log, isEmpty);

    await gesture.up();
491
    await tester.pumpAndSettle();
492 493 494 495 496 497 498

    expect(log, isEmpty);

    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alaska'), findsOneWidget);
  });

499
  testWidgetsWithLeakTracking('Bouncing scroll physics ballistics does not overshoot', (WidgetTester tester) async {
500
    final List<int> log = <int>[];
501
    final PageController controller = PageController(viewportFraction: 0.9);
502
    addTearDown(controller.dispose);
503

504
    Widget build(PageController controller, { Size? size }) {
505
      final Widget pageView = Directionality(
506
        textDirection: TextDirection.ltr,
507
        child: PageView(
508 509 510
          controller: controller,
          onPageChanged: log.add,
          physics: const BouncingScrollPhysics(),
511
          children: kStates.map<Widget>((String state) => Text(state)).toList(),
512 513 514 515
        ),
      );

      if (size != null) {
516
        return OverflowBox(
517 518 519 520
          minWidth: size.width,
          minHeight: size.height,
          maxWidth: size.width,
          maxHeight: size.height,
521
          child: pageView,
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
        );
      } else {
        return pageView;
      }
    }

    await tester.pumpWidget(build(controller));
    expect(log, isEmpty);

    // Fling right to move to a non-existent page at the beginning of the
    // PageView, and confirm that the PageView settles back on the first page.
    await tester.fling(find.byType(PageView), const Offset(100.0, 0.0), 800.0);
    await tester.pumpAndSettle();
    expect(log, isEmpty);

    expect(find.text('Alabama'), findsOneWidget);
    expect(find.text('Alaska'), findsOneWidget);
    expect(find.text('Arizona'), findsNothing);

    // Try again with a Cupertino "Plus" device size.
    await tester.pumpWidget(build(controller, size: const Size(414.0, 736.0)));
    expect(log, isEmpty);

    await tester.fling(find.byType(PageView), const Offset(100.0, 0.0), 800.0);
    await tester.pumpAndSettle();
    expect(log, isEmpty);

    expect(find.text('Alabama'), findsOneWidget);
    expect(find.text('Alaska'), findsOneWidget);
    expect(find.text('Arizona'), findsNothing);
  });

554
  testWidgetsWithLeakTracking('PageView viewportFraction', (WidgetTester tester) async {
555
    PageController controller = PageController(viewportFraction: 7/8);
556
    addTearDown(controller.dispose);
557 558

    Widget build(PageController controller) {
559
      return Directionality(
560
        textDirection: TextDirection.ltr,
561
        child: PageView.builder(
562 563 564
          controller: controller,
          itemCount: kStates.length,
          itemBuilder: (BuildContext context, int index) {
565
            return Container(
566
              height: 200.0,
567
              color: index.isEven
568 569
                ? const Color(0xFF0000FF)
                : const Color(0xFF00FF00),
570
              child: Text(kStates[index]),
571 572 573
            );
          },
        ),
574 575 576 577 578
      );
    }

    await tester.pumpWidget(build(controller));

579 580
    expect(tester.getTopLeft(find.text('Alabama')), const Offset(50.0, 0.0));
    expect(tester.getTopLeft(find.text('Alaska')), const Offset(750.0, 0.0));
581 582 583 584

    controller.jumpToPage(10);
    await tester.pump();

585 586 587
    expect(tester.getTopLeft(find.text('Georgia')), const Offset(-650.0, 0.0));
    expect(tester.getTopLeft(find.text('Hawaii')), const Offset(50.0, 0.0));
    expect(tester.getTopLeft(find.text('Idaho')), const Offset(750.0, 0.0));
588

589
    controller = PageController(viewportFraction: 39/40);
590
    addTearDown(controller.dispose);
591 592 593

    await tester.pumpWidget(build(controller));

594 595 596
    expect(tester.getTopLeft(find.text('Georgia')), const Offset(-770.0, 0.0));
    expect(tester.getTopLeft(find.text('Hawaii')), const Offset(10.0, 0.0));
    expect(tester.getTopLeft(find.text('Idaho')), const Offset(790.0, 0.0));
597 598
  });

599
  testWidgetsWithLeakTracking('Page snapping disable and reenable', (WidgetTester tester) async {
600 601
    final List<int> log = <int>[];

602
    Widget build({ required bool pageSnapping }) {
603
      return Directionality(
604
        textDirection: TextDirection.ltr,
605
        child: PageView(
606 607 608
          pageSnapping: pageSnapping,
          onPageChanged: log.add,
          children:
609
              kStates.map<Widget>((String state) => Text(state)).toList(),
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        ),
      );
    }

    await tester.pumpWidget(build(pageSnapping: true));
    expect(log, isEmpty);

    // Drag more than halfway to the next page, to confirm the default behavior.
    TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
    // The page view is 800.0 wide, so this move is just beyond halfway.
    await gesture.moveBy(const Offset(-420.0, 0.0));

    expect(log, equals(const <int>[1]));
    log.clear();

    // Release the gesture, confirm that the page settles on the next.
    await gesture.up();
    await tester.pumpAndSettle();

    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alaska'), findsOneWidget);

    // Disable page snapping, and try moving halfway. Confirm it doesn't snap.
    await tester.pumpWidget(build(pageSnapping: false));
    gesture = await tester.startGesture(const Offset(100.0, 100.0));
    // Move just beyond halfway, again.
    await gesture.moveBy(const Offset(-420.0, 0.0));

    // Page notifications still get sent.
    expect(log, equals(const <int>[2]));
    log.clear();

    // Release the gesture, confirm that both pages are visible.
    await gesture.up();
    await tester.pumpAndSettle();

    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Alaska'), findsOneWidget);
    expect(find.text('Arizona'), findsOneWidget);
    expect(find.text('Arkansas'), findsNothing);

    // Now re-enable snapping, confirm that we've settled on a page.
    await tester.pumpWidget(build(pageSnapping: true));
    await tester.pumpAndSettle();

    expect(log, isEmpty);

    expect(find.text('Alaska'), findsNothing);
    expect(find.text('Arizona'), findsOneWidget);
    expect(find.text('Arkansas'), findsNothing);
  });

662
  testWidgetsWithLeakTracking('PageView small viewportFraction', (WidgetTester tester) async {
663
    final PageController controller = PageController(viewportFraction: 1/8);
664
    addTearDown(controller.dispose);
665 666

    Widget build(PageController controller) {
667
      return Directionality(
668
        textDirection: TextDirection.ltr,
669
        child: PageView.builder(
670 671 672
          controller: controller,
          itemCount: kStates.length,
          itemBuilder: (BuildContext context, int index) {
673
            return Container(
674
              height: 200.0,
675
              color: index.isEven
676 677
                ? const Color(0xFF0000FF)
                : const Color(0xFF00FF00),
678
              child: Text(kStates[index]),
679 680 681
            );
          },
        ),
682 683 684 685 686
      );
    }

    await tester.pumpWidget(build(controller));

687 688 689 690 691
    expect(tester.getTopLeft(find.text('Alabama')), const Offset(350.0, 0.0));
    expect(tester.getTopLeft(find.text('Alaska')), const Offset(450.0, 0.0));
    expect(tester.getTopLeft(find.text('Arizona')), const Offset(550.0, 0.0));
    expect(tester.getTopLeft(find.text('Arkansas')), const Offset(650.0, 0.0));
    expect(tester.getTopLeft(find.text('California')), const Offset(750.0, 0.0));
692 693 694 695

    controller.jumpToPage(10);
    await tester.pump();

696 697 698 699 700 701 702 703 704
    expect(tester.getTopLeft(find.text('Connecticut')), const Offset(-50.0, 0.0));
    expect(tester.getTopLeft(find.text('Delaware')), const Offset(50.0, 0.0));
    expect(tester.getTopLeft(find.text('Florida')), const Offset(150.0, 0.0));
    expect(tester.getTopLeft(find.text('Georgia')), const Offset(250.0, 0.0));
    expect(tester.getTopLeft(find.text('Hawaii')), const Offset(350.0, 0.0));
    expect(tester.getTopLeft(find.text('Idaho')), const Offset(450.0, 0.0));
    expect(tester.getTopLeft(find.text('Illinois')), const Offset(550.0, 0.0));
    expect(tester.getTopLeft(find.text('Indiana')), const Offset(650.0, 0.0));
    expect(tester.getTopLeft(find.text('Iowa')), const Offset(750.0, 0.0));
705 706
  });

707
  testWidgetsWithLeakTracking('PageView large viewportFraction', (WidgetTester tester) async {
708
    final PageController controller = PageController(viewportFraction: 5/4);
709
    addTearDown(controller.dispose);
710 711

    Widget build(PageController controller) {
712
      return Directionality(
713
        textDirection: TextDirection.ltr,
714
        child: PageView.builder(
715 716 717
          controller: controller,
          itemCount: kStates.length,
          itemBuilder: (BuildContext context, int index) {
718
            return Container(
719
              height: 200.0,
720
              color: index.isEven
721 722
                ? const Color(0xFF0000FF)
                : const Color(0xFF00FF00),
723
              child: Text(kStates[index]),
724 725 726
            );
          },
        ),
727 728 729 730 731
      );
    }

    await tester.pumpWidget(build(controller));

732 733
    expect(tester.getTopLeft(find.text('Alabama')), const Offset(-100.0, 0.0));
    expect(tester.getBottomRight(find.text('Alabama')), const Offset(900.0, 600.0));
734 735 736 737

    controller.jumpToPage(10);
    await tester.pump();

738
    expect(tester.getTopLeft(find.text('Hawaii')), const Offset(-100.0, 0.0));
739
  });
740

741
  testWidgetsWithLeakTracking(
742 743 744 745 746 747 748 749 750 751 752
    'Updating PageView large viewportFraction',
    (WidgetTester tester) async {
      Widget build(PageController controller) {
        return Directionality(
          textDirection: TextDirection.ltr,
          child: PageView.builder(
            controller: controller,
            itemCount: kStates.length,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                height: 200.0,
753
                color: index.isEven
754 755 756 757 758 759 760 761 762 763
                  ? const Color(0xFF0000FF)
                  : const Color(0xFF00FF00),
                child: Text(kStates[index]),
              );
            },
          ),
        );
      }

      final PageController oldController = PageController(viewportFraction: 5/4);
764
      addTearDown(oldController.dispose);
765 766 767 768 769 770
      await tester.pumpWidget(build(oldController));

      expect(tester.getTopLeft(find.text('Alabama')), const Offset(-100, 0));
      expect(tester.getBottomRight(find.text('Alabama')), const Offset(900.0, 600.0));

      final PageController newController = PageController(viewportFraction: 4);
771
      addTearDown(newController.dispose);
772 773 774 775 776
      await tester.pumpWidget(build(newController));
      newController.jumpToPage(10);
      await tester.pump();

      expect(tester.getTopLeft(find.text('Hawaii')), const Offset(-(4 - 1) * 800 / 2, 0));
777 778
    },
  );
779

780
  testWidgetsWithLeakTracking(
781 782 783 784
    'PageView large viewportFraction can scroll to the last page and snap',
    (WidgetTester tester) async {
      // Regression test for https://github.com/flutter/flutter/issues/45096.
      final PageController controller = PageController(viewportFraction: 5/4);
785
      addTearDown(controller.dispose);
786 787 788 789 790 791 792 793 794 795

      Widget build(PageController controller) {
        return Directionality(
          textDirection: TextDirection.ltr,
          child: PageView.builder(
            controller: controller,
            itemCount: 3,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                height: 200.0,
796
                color: index.isEven
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
                  ? const Color(0xFF0000FF)
                  : const Color(0xFF00FF00),
                  child: Text(index.toString()),
              );
            },
          ),
        );
      }

      await tester.pumpWidget(build(controller));

      expect(tester.getCenter(find.text('0')), const Offset(400, 300));

      controller.jumpToPage(2);
      await tester.pump();
      await tester.pumpAndSettle();

      expect(tester.getCenter(find.text('2')), const Offset(400, 300));
815 816
    },
  );
817

818
  testWidgetsWithLeakTracking(
819 820 821
    'All visible pages are able to receive touch events',
    (WidgetTester tester) async {
      // Regression test for https://github.com/flutter/flutter/issues/23873.
822
      final PageController controller = PageController(viewportFraction: 1/4);
823
      addTearDown(controller.dispose);
824
      late int tappedIndex;
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845

      Widget build() {
        return Directionality(
          textDirection: TextDirection.ltr,
          child: PageView.builder(
            controller: controller,
            itemCount: 20,
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                onTap: () => tappedIndex = index,
                child: SizedBox.expand(child: Text('$index')),
              );
            },
          ),
        );
      }

      Iterable<int> visiblePages = const <int> [0, 1, 2];
      await tester.pumpWidget(build());

      // The first 3 items should be visible and tappable.
846
      for (final int index in visiblePages) {
847 848 849 850 851 852 853 854 855 856 857 858
        expect(find.text(index.toString()), findsOneWidget);
        // The center of page 2's x-coordinate is 800, so we have to manually
        // offset it a bit to make sure the tap lands within the screen.
        final Offset center = tester.getCenter(find.text('$index')) - const Offset(3, 0);
        await tester.tapAt(center);
        expect(tappedIndex, index);
      }

      controller.jumpToPage(19);
      await tester.pump();
      // The last 3 items should be visible and tappable.
      visiblePages = const <int> [17, 18, 19];
859
      for (final int index in visiblePages) {
860 861 862 863
        expect(find.text('$index'), findsOneWidget);
        await tester.tap(find.text('$index'));
        expect(tappedIndex, index);
      }
864 865
    },
  );
866

867
  testWidgetsWithLeakTracking('the current item remains centered on constraint change', (WidgetTester tester) async {
868 869 870 871 872
    // Regression test for https://github.com/flutter/flutter/issues/50505.
    final PageController controller = PageController(
      initialPage: kStates.length - 1,
      viewportFraction: 0.5,
    );
873
    addTearDown(controller.dispose);
874 875 876 877 878 879 880 881 882

    Widget build(Size size) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: SizedBox.fromSize(
            size: size,
            child: PageView(
              controller: controller,
883
              children: kStates.map<Widget>((String state) => Text(state)).toList(),
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
              onPageChanged: (int page) { },
            ),
          ),
        ),
      );
    }

    // Verifies that the last item is centered on screen.
    void verifyCentered() {
      expect(
        tester.getCenter(find.text(kStates.last)),
        offsetMoreOrLessEquals(const Offset(400, 300)),
      );
    }

    await tester.pumpWidget(build(const Size(300, 300)));
    await tester.pumpAndSettle();

    verifyCentered();

    await tester.pumpWidget(build(const Size(200, 300)));
    await tester.pumpAndSettle();

    verifyCentered();
  });

910
  testWidgetsWithLeakTracking('PageView does not report page changed on overscroll', (WidgetTester tester) async {
911
    final PageController controller = PageController(
912 913
      initialPage: kStates.length - 1,
    );
914
    addTearDown(controller.dispose);
915 916
    int changeIndex = 0;
    Widget build() {
917
      return Directionality(
918
        textDirection: TextDirection.ltr,
919
        child: PageView(
920
          controller: controller,
921
          children: kStates.map<Widget>((String state) => Text(state)).toList(),
922 923 924 925
          onPageChanged: (int page) {
            changeIndex = page;
          },
        ),
926 927 928 929 930 931 932 933 934 935
      );
    }

    await tester.pumpWidget(build());
    controller.jumpToPage(kStates.length * 2); // try to move beyond max range
    // change index should be zero, shouldn't fire onPageChanged
    expect(changeIndex, 0);
    await tester.pump();
    expect(changeIndex, 0);
  });
936

937
  testWidgetsWithLeakTracking('PageView can restore page', (WidgetTester tester) async {
938
    final PageController controller = PageController();
939
    addTearDown(controller.dispose);
940 941
    expect(
      () => controller.page,
942
      throwsA(isAssertionError.having(
943 944 945 946 947
        (AssertionError error) => error.message,
        'message',
        equals('PageController.page cannot be accessed before a PageView is built with it.'),
      )),
    );
948 949
    final PageStorageBucket bucket = PageStorageBucket();
    await tester.pumpWidget(Directionality(
950
      textDirection: TextDirection.ltr,
951
      child: PageStorage(
952
        bucket: bucket,
953
        child: PageView(
954
          key: const PageStorageKey<String>('PageView'),
955
          controller: controller,
956
          children: const <Widget>[
957 958 959
            Placeholder(),
            Placeholder(),
            Placeholder(),
960 961 962
          ],
        ),
      ),
963
    ));
964 965
    expect(controller.page, 0);
    controller.jumpToPage(2);
966
    expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 2);
967 968
    expect(controller.page, 2);
    await tester.pumpWidget(
969
      PageStorage(
970
        bucket: bucket,
971
        child: Container(),
972 973
      ),
    );
974 975
    expect(
      () => controller.page,
976
      throwsA(isAssertionError.having(
977 978 979 980 981
        (AssertionError error) => error.message,
        'message',
        equals('PageController.page cannot be accessed before a PageView is built with it.'),
      )),
    );
982
    await tester.pumpWidget(Directionality(
983
      textDirection: TextDirection.ltr,
984
      child: PageStorage(
985
        bucket: bucket,
986
        child: PageView(
987
          key: const PageStorageKey<String>('PageView'),
988
          controller: controller,
989
          children: const <Widget>[
990 991 992
            Placeholder(),
            Placeholder(),
            Placeholder(),
993 994 995
          ],
        ),
      ),
996
    ));
997
    expect(controller.page, 2);
998

999
    final PageController controller2 = PageController(keepPage: false);
1000
    addTearDown(controller2.dispose);
1001
    await tester.pumpWidget(Directionality(
1002
      textDirection: TextDirection.ltr,
1003
      child: PageStorage(
1004
        bucket: bucket,
1005
        child: PageView(
1006
          key: const PageStorageKey<String>('Check it again against your list and see consistency!'),
1007
          controller: controller2,
1008
          children: const <Widget>[
1009 1010 1011
            Placeholder(),
            Placeholder(),
            Placeholder(),
1012 1013 1014
          ],
        ),
      ),
1015
    ));
1016
    expect(controller2.page, 0);
1017
  });
1018

1019
  testWidgetsWithLeakTracking('PageView exposes semantics of children', (WidgetTester tester) async {
1020
    final SemanticsTester semantics = SemanticsTester(tester);
1021

1022
    final PageController controller = PageController();
1023
    addTearDown(controller.dispose);
1024
    await tester.pumpWidget(Directionality(
1025
      textDirection: TextDirection.ltr,
1026
      child: PageView(
1027
          controller: controller,
1028 1029
          children: List<Widget>.generate(3, (int i) {
            return Semantics(
1030
              container: true,
1031
              child: Text('Page #$i'),
1032
            );
1033
          }),
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
        ),
    ));
    expect(controller.page, 0);

    expect(semantics, includesNodeWith(label: 'Page #0'));
    expect(semantics, isNot(includesNodeWith(label: 'Page #1')));
    expect(semantics, isNot(includesNodeWith(label: 'Page #2')));

    controller.jumpToPage(1);
    await tester.pumpAndSettle();

    expect(semantics, isNot(includesNodeWith(label: 'Page #0')));
    expect(semantics, includesNodeWith(label: 'Page #1'));
    expect(semantics, isNot(includesNodeWith(label: 'Page #2')));

    controller.jumpToPage(2);
    await tester.pumpAndSettle();

    expect(semantics, isNot(includesNodeWith(label: 'Page #0')));
    expect(semantics, isNot(includesNodeWith(label: 'Page #1')));
    expect(semantics, includesNodeWith(label: 'Page #2'));

    semantics.dispose();
  });
1058

1059
  testWidgetsWithLeakTracking('PageMetrics', (WidgetTester tester) async {
1060
    final PageMetrics page = PageMetrics(
1061 1062 1063 1064 1065 1066
      minScrollExtent: 100.0,
      maxScrollExtent: 200.0,
      pixels: 150.0,
      viewportDimension: 25.0,
      axisDirection: AxisDirection.right,
      viewportFraction: 1.0,
1067
      devicePixelRatio: tester.view.devicePixelRatio,
1068 1069 1070 1071 1072 1073 1074
    );
    expect(page.page, 6);
    final PageMetrics page2 = page.copyWith(
      pixels: page.pixels - 100.0,
    );
    expect(page2.page, 4.0);
  });
1075

1076
  testWidgetsWithLeakTracking('Page controller can handle rounding issue', (WidgetTester tester) async {
1077
    final PageController pageController = PageController();
1078
    addTearDown(pageController.dispose);
1079 1080 1081 1082 1083 1084 1085 1086

    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: PageView(
        controller: pageController,
        children: List<Widget>.generate(3, (int i) {
          return Semantics(
            container: true,
1087
            child: Text('Page #$i'),
1088 1089 1090 1091 1092 1093 1094 1095
          );
        }),
      ),
    ));
    // Simulate precision error.
    pageController.position.jumpTo(799.99999999999);
    expect(pageController.page, 1);
  });
1096

1097
  testWidgetsWithLeakTracking('PageView can participate in a11y scrolling', (WidgetTester tester) async {
1098 1099 1100
    final SemanticsTester semantics = SemanticsTester(tester);

    final PageController controller = PageController();
1101
    addTearDown(controller.dispose);
1102 1103 1104 1105
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: PageView(
          controller: controller,
1106
          allowImplicitScrolling: true,
1107 1108 1109
          children: List<Widget>.generate(4, (int i) {
            return Semantics(
              container: true,
1110
              child: Text('Page #$i'),
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
            );
          }),
        ),
    ));
    expect(controller.page, 0);

    expect(semantics, includesNodeWith(flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling]));
    expect(semantics, includesNodeWith(label: 'Page #0'));
    expect(semantics, includesNodeWith(label: 'Page #1', flags: <SemanticsFlag>[SemanticsFlag.isHidden]));
    expect(semantics, isNot(includesNodeWith(label: 'Page #2', flags: <SemanticsFlag>[SemanticsFlag.isHidden])));
    expect(semantics, isNot(includesNodeWith(label: 'Page #3', flags: <SemanticsFlag>[SemanticsFlag.isHidden])));

    controller.nextPage(duration: const Duration(milliseconds: 150), curve: Curves.ease);
    await tester.pumpAndSettle();
    expect(semantics, includesNodeWith(label: 'Page #0', flags: <SemanticsFlag>[SemanticsFlag.isHidden]));
    expect(semantics, includesNodeWith(label: 'Page #1'));
    expect(semantics, includesNodeWith(label: 'Page #2', flags: <SemanticsFlag>[SemanticsFlag.isHidden]));
    expect(semantics, isNot(includesNodeWith(label: 'Page #3', flags: <SemanticsFlag>[SemanticsFlag.isHidden])));

    controller.nextPage(duration: const Duration(milliseconds: 150), curve: Curves.ease);
    await tester.pumpAndSettle();
    expect(semantics, isNot(includesNodeWith(label: 'Page #0', flags: <SemanticsFlag>[SemanticsFlag.isHidden])));
    expect(semantics, includesNodeWith(label: 'Page #1', flags: <SemanticsFlag>[SemanticsFlag.isHidden]));
    expect(semantics, includesNodeWith(label: 'Page #2'));
    expect(semantics, includesNodeWith(label: 'Page #3', flags: <SemanticsFlag>[SemanticsFlag.isHidden]));

    semantics.dispose();
  });
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

  testWidgets('PageView respects clipBehavior', (WidgetTester tester) async {
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: PageView(
          children: <Widget>[Container(height: 2000.0)],
        ),
      ),
    );

    // 1st, check that the render object has received the default clip behavior.
    final RenderViewport renderObject = tester.allRenderObjects.whereType<RenderViewport>().first;
    expect(renderObject.clipBehavior, equals(Clip.hardEdge));

    // 2nd, check that the painting context has received the default clip behavior.
    final TestClipPaintingContext context = TestClipPaintingContext();
    renderObject.paint(context, Offset.zero);
    expect(context.clipBehavior, equals(Clip.hardEdge));

    // 3rd, pump a new widget to check that the render object can update its clip behavior.
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: PageView(
          clipBehavior: Clip.antiAlias,
1165
          children: <Widget>[Container(height: 2000.0)],
1166 1167 1168 1169 1170 1171 1172 1173 1174
        ),
      ),
    );
    expect(renderObject.clipBehavior, equals(Clip.antiAlias));

    // 4th, check that a non-default clip behavior can be sent to the painting context.
    renderObject.paint(context, Offset.zero);
    expect(context.clipBehavior, equals(Clip.antiAlias));
  });
1175

1176
  testWidgetsWithLeakTracking('PageView.padEnds tests', (WidgetTester tester) async {
1177 1178 1179 1180 1181
    Finder viewportFinder() => find.byType(SliverFillViewport, skipOffstage: false);

    // PageView() defaults to true.
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
1182
      child: PageView(),
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    ));

    expect(tester.widget<SliverFillViewport>(viewportFinder()).padEnds, true);

    // PageView(padEnds: false) is propagated properly.
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: PageView(
        padEnds: false,
      ),
    ));

    expect(tester.widget<SliverFillViewport>(viewportFinder()).padEnds, false);
  });
1197

1198
  testWidgetsWithLeakTracking('PageView - precision error inside RenderSliverFixedExtentBoxAdaptor', (WidgetTester tester) async {
1199 1200
    // Regression test for https://github.com/flutter/flutter/issues/95101
    final PageController controller = PageController(initialPage: 152);
1201 1202
    addTearDown(controller.dispose);

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    await tester.pumpWidget(
      Center(
        child: SizedBox(
          width: 392.72727272727275,
          child: Directionality(
            textDirection: TextDirection.ltr,
            child: PageView.builder(
              controller: controller,
              itemCount: 366,
              itemBuilder: (BuildContext context, int index) {
                return const SizedBox();
              },
            ),
          ),
        ),
      ),
    );

    controller.jumpToPage(365);
    await tester.pump();
    expect(tester.takeException(), isNull);
  });
1225

1226
  testWidgetsWithLeakTracking('PageView content should not be stretched on precision error', (WidgetTester tester) async {
1227 1228
    // Regression test for https://github.com/flutter/flutter/issues/126561.
    final PageController controller = PageController();
1229
    addTearDown(controller.dispose);
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

    const double pixel6EmulatorWidth = 411.42857142857144;

    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: Center(
        child: SizedBox(
          width: pixel6EmulatorWidth,
          child: PageView(
            controller: controller,
            physics: const PageScrollPhysics().applyTo(const ClampingScrollPhysics()),
            children: const <Widget>[
              Center(child: Text('First Page')),
              Center(child: Text('Second Page')),
              Center(child: Text('Third Page')),
            ],
          ),
        ),
      ),
    ));

    controller.animateToPage(2, duration: const Duration(milliseconds: 300), curve: Curves.ease);
    await tester.pumpAndSettle();

    final Finder transformFinder = find.descendant(of: find.byType(PageView), matching: find.byType(Transform));
    expect(transformFinder, findsOneWidget);

    // Get the Transform widget that stretches the PageView.
    final Transform transform = tester.firstWidget<Transform>(
      find.descendant(
        of: find.byType(PageView),
        matching: find.byType(Transform),
      ),
    );

    // Check the stretch factor in the first element of the transform matrix.
    expect(transform.transform.storage.first, 1.0);
  });
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

  testWidgetsWithLeakTracking('PageController onAttach, onDetach', (WidgetTester tester) async {
    int attach = 0;
    int detach = 0;
    final PageController controller = PageController(
      onAttach: (_) { attach++; },
      onDetach: (_) { detach++; },
    );
    addTearDown(controller.dispose);

    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: Center(
        child: PageView(
          controller: controller,
          physics: const PageScrollPhysics().applyTo(const ClampingScrollPhysics()),
          children: const <Widget>[
            Center(child: Text('First Page')),
            Center(child: Text('Second Page')),
            Center(child: Text('Third Page')),
          ],
        ),
      ),
    ));
    await tester.pumpAndSettle();

    expect(attach, 1);
    expect(detach, 0);

    await tester.pumpWidget(Container());
    await tester.pumpAndSettle();

    expect(attach, 1);
    expect(detach, 1);
  });
Adam Barth's avatar
Adam Barth committed
1303
}