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

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/material.dart';

8 9
import '../widgets/semantics_tester.dart';

10
void main() {
11 12 13 14 15 16 17 18 19 20 21 22
  // Pumps and ensures that the BottomSheet animates non-linearly.
  Future<void> _checkNonLinearAnimation(WidgetTester tester) async {
    final Offset firstPosition = tester.getCenter(find.text('BottomSheet'));
    await tester.pump(const Duration(milliseconds: 30));
    final Offset secondPosition = tester.getCenter(find.text('BottomSheet'));
    await tester.pump(const Duration(milliseconds: 30));
    final Offset thirdPosition = tester.getCenter(find.text('BottomSheet'));

    final double dyDelta1 = secondPosition.dy - firstPosition.dy;
    final double dyDelta2 = thirdPosition.dy - secondPosition.dy;

    // If the animation were linear, these two values would be the same.
23
    expect(dyDelta1, isNot(moreOrLessEquals(dyDelta2, epsilon: 0.1)));
24 25
  }

26
  testWidgets('Tapping on a modal BottomSheet should not dismiss it', (WidgetTester tester) async {
27
    late BuildContext savedContext;
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            savedContext = context;
            return Container();
          },
        ),
      ),
    );

    await tester.pump();
    expect(find.text('BottomSheet'), findsNothing);

    bool showBottomSheetThenCalled = false;
    showModalBottomSheet<void>(
      context: savedContext,
      builder: (BuildContext context) => const Text('BottomSheet'),
    ).then<void>((void value) {
      showBottomSheetThenCalled = true;
    });

    await tester.pumpAndSettle();
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);

    // Tap on the bottom sheet itself, it should not be dismissed
    await tester.tap(find.text('BottomSheet'));
    await tester.pumpAndSettle();
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);
  });

  testWidgets('Tapping outside a modal BottomSheet should dismiss it by default', (WidgetTester tester) async {
63
    late BuildContext savedContext;
64

65 66
    await tester.pumpWidget(MaterialApp(
      home: Builder(
67 68
        builder: (BuildContext context) {
          savedContext = context;
69
          return Container();
70
        },
71
      ),
72 73
    ));

74
    await tester.pump();
75 76
    expect(find.text('BottomSheet'), findsNothing);

77
    bool showBottomSheetThenCalled = false;
78
    showModalBottomSheet<void>(
79
      context: savedContext,
80
      builder: (BuildContext context) => const Text('BottomSheet'),
81
    ).then<void>((void value) {
82
      showBottomSheetThenCalled = true;
83
    });
84

85
    await tester.pumpAndSettle();
86 87 88
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);

89 90 91 92 93 94 95 96
    // Tap above the bottom sheet to dismiss it.
    await tester.tapAt(const Offset(20.0, 20.0));
    await tester.pumpAndSettle(); // Bottom sheet dismiss animation.
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsNothing);
  });

  testWidgets('Tapping outside a modal BottomSheet should dismiss it when isDismissible=true', (WidgetTester tester) async {
97
    late BuildContext savedContext;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

    await tester.pumpWidget(MaterialApp(
      home: Builder(
        builder: (BuildContext context) {
          savedContext = context;
          return Container();
        },
      ),
    ));

    await tester.pump();
    expect(find.text('BottomSheet'), findsNothing);

    bool showBottomSheetThenCalled = false;
    showModalBottomSheet<void>(
      context: savedContext,
      builder: (BuildContext context) => const Text('BottomSheet'),
      isDismissible: true,
    ).then<void>((void value) {
      showBottomSheetThenCalled = true;
    });

120 121 122
    await tester.pumpAndSettle();
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);
123 124 125 126 127 128

    // Tap above the bottom sheet to dismiss it.
    await tester.tapAt(const Offset(20.0, 20.0));
    await tester.pumpAndSettle(); // Bottom sheet dismiss animation.
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsNothing);
129 130
  });

131
  testWidgets('Verify that the BottomSheet animates non-linearly', (WidgetTester tester) async {
132
    late BuildContext savedContext;
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

    await tester.pumpWidget(MaterialApp(
      home: Builder(
        builder: (BuildContext context) {
          savedContext = context;
          return Container();
        },
      ),
    ));

    await tester.pump();
    expect(find.text('BottomSheet'), findsNothing);

    showModalBottomSheet<void>(
      context: savedContext,
      builder: (BuildContext context) => const Text('BottomSheet'),
    );
    await tester.pump();

    await _checkNonLinearAnimation(tester);
    await tester.pumpAndSettle();

    // Tap above the bottom sheet to dismiss it.
    await tester.tapAt(const Offset(20.0, 20.0));
    await tester.pump();
    await _checkNonLinearAnimation(tester);
    await tester.pumpAndSettle(); // Bottom sheet dismiss animation.
    expect(find.text('BottomSheet'), findsNothing);
  });

163
  testWidgets('Tapping outside a modal BottomSheet should not dismiss it when isDismissible=false', (WidgetTester tester) async {
164
    late BuildContext savedContext;
165

166 167 168
    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
169 170 171
          builder: (BuildContext context) {
            savedContext = context;
            return Container();
172 173
          },
        ),
174
      ),
175
    );
176 177

    await tester.pump();
178 179
    expect(find.text('BottomSheet'), findsNothing);

180
    bool showBottomSheetThenCalled = false;
181
    showModalBottomSheet<void>(
182
      context: savedContext,
183
      builder: (BuildContext context) => const Text('BottomSheet'),
184
      isDismissible: false,
185
    ).then<void>((void value) {
186 187
      showBottomSheetThenCalled = true;
    });
188 189

    await tester.pumpAndSettle();
190
    expect(find.text('BottomSheet'), findsOneWidget);
191
    expect(showBottomSheetThenCalled, isFalse);
192

193
    // Tap above the bottom sheet, attempting to dismiss it.
194
    await tester.tapAt(const Offset(20.0, 20.0));
195 196 197
    await tester.pumpAndSettle(); // Bottom sheet should not dismiss.
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsOneWidget);
198
  });
199

200
  testWidgets('Swiping down a modal BottomSheet should dismiss it by default', (WidgetTester tester) async {
201
    late BuildContext savedContext;
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

    await tester.pumpWidget(MaterialApp(
      home: Builder(
        builder: (BuildContext context) {
          savedContext = context;
          return Container();
        },
      ),
    ));

    await tester.pump();
    expect(find.text('BottomSheet'), findsNothing);

    bool showBottomSheetThenCalled = false;
    showModalBottomSheet<void>(
      context: savedContext,
      isDismissible: false,
      builder: (BuildContext context) => const Text('BottomSheet'),
    ).then<void>((void value) {
      showBottomSheetThenCalled = true;
    });

    await tester.pumpAndSettle();
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);

    // Swipe the bottom sheet to dismiss it.
    await tester.drag(find.text('BottomSheet'), const Offset(0.0, 150.0));
    await tester.pumpAndSettle(); // Bottom sheet dismiss animation.
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsNothing);
  });

  testWidgets('Swiping down a modal BottomSheet should not dismiss it when enableDrag is false', (WidgetTester tester) async {
236
    late BuildContext savedContext;
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

    await tester.pumpWidget(MaterialApp(
      home: Builder(
        builder: (BuildContext context) {
          savedContext = context;
          return Container();
        },
      ),
    ));

    await tester.pump();
    expect(find.text('BottomSheet'), findsNothing);

    bool showBottomSheetThenCalled = false;
    showModalBottomSheet<void>(
      context: savedContext,
      isDismissible: false,
      enableDrag: false,
      builder: (BuildContext context) => const Text('BottomSheet'),
    ).then<void>((void value) {
      showBottomSheetThenCalled = true;
    });

    await tester.pumpAndSettle();
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);

    // Swipe the bottom sheet, attempting to dismiss it.
    await tester.drag(find.text('BottomSheet'), const Offset(0.0, 150.0));
    await tester.pumpAndSettle(); // Bottom sheet should not dismiss.
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsOneWidget);
  });

  testWidgets('Swiping down a modal BottomSheet should dismiss it when enableDrag is true', (WidgetTester tester) async {
272
    late BuildContext savedContext;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

    await tester.pumpWidget(MaterialApp(
      home: Builder(
        builder: (BuildContext context) {
          savedContext = context;
          return Container();
        },
      ),
    ));

    await tester.pump();
    expect(find.text('BottomSheet'), findsNothing);

    bool showBottomSheetThenCalled = false;
    showModalBottomSheet<void>(
      context: savedContext,
      isDismissible: false,
      enableDrag: true,
      builder: (BuildContext context) => const Text('BottomSheet'),
    ).then<void>((void value) {
      showBottomSheetThenCalled = true;
    });

    await tester.pumpAndSettle();
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);

    // Swipe the bottom sheet to dismiss it.
    await tester.drag(find.text('BottomSheet'), const Offset(0.0, 150.0));
    await tester.pumpAndSettle(); // Bottom sheet dismiss animation.
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsNothing);
  });

307
  testWidgets('Modal BottomSheet builder should only be called once', (WidgetTester tester) async {
308
    late BuildContext savedContext;
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

    await tester.pumpWidget(MaterialApp(
      home: Builder(
        builder: (BuildContext context) {
          savedContext = context;
          return Container();
        },
      ),
    ));

    int numBuilderCalls = 0;
    showModalBottomSheet<void>(
      context: savedContext,
      isDismissible: false,
      enableDrag: true,
      builder: (BuildContext context) {
        numBuilderCalls++;
        return const Text('BottomSheet');
      },
    );

    await tester.pumpAndSettle();
    expect(numBuilderCalls, 1);

    // Swipe the bottom sheet to dismiss it.
    await tester.drag(find.text('BottomSheet'), const Offset(0.0, 150.0));
    await tester.pumpAndSettle(); // Bottom sheet dismiss animation.
    expect(numBuilderCalls, 1);
  });

339
  testWidgets('Verify that a downwards fling dismisses a persistent BottomSheet', (WidgetTester tester) async {
340
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
341 342
    bool showBottomSheetThenCalled = false;

343 344
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
345
        key: scaffoldKey,
346 347
        body: const Center(child: Text('body')),
      ),
348 349 350 351 352
    ));

    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsNothing);

353
    scaffoldKey.currentState!.showBottomSheet<void>((BuildContext context) {
354
      return Container(
355
        margin: const EdgeInsets.all(40.0),
356
        child: const Text('BottomSheet'),
357
      );
358
    }).closed.whenComplete(() {
359 360
      showBottomSheetThenCalled = true;
    });
361

362 363
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsNothing);
364

365
    await tester.pump(); // bottom sheet show animation starts
366

367 368
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsOneWidget);
369

370
    await tester.pump(const Duration(seconds: 1)); // animation done
371

372 373
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsOneWidget);
374

375 376
    // The fling below must be such that the velocity estimation examines an
    // offset greater than the kTouchSlop. Too slow or too short a distance, and
377
    // it won't trigger. Also, it must not be so much that it drags the bottom
378 379
    // sheet off the screen, or we won't see it after we pump!
    await tester.fling(find.text('BottomSheet'), const Offset(0.0, 50.0), 2000.0);
380
    await tester.pump(); // drain the microtask queue (Future completion callback)
381

382 383
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsOneWidget);
384

385
    await tester.pump(); // bottom sheet dismiss animation starts
386

387 388
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsOneWidget);
389

390
    await tester.pump(const Duration(seconds: 1)); // animation done
391

392 393
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsNothing);
394 395
  });

396 397
  testWidgets('Verify that dragging past the bottom dismisses a persistent BottomSheet', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/5528
398
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
399

400 401
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
402
        key: scaffoldKey,
403 404
        body: const Center(child: Text('body')),
      ),
405 406
    ));

407
    scaffoldKey.currentState!.showBottomSheet<void>((BuildContext context) {
408
      return Container(
409
        margin: const EdgeInsets.all(40.0),
410
        child: const Text('BottomSheet'),
411 412 413 414
      );
    });

    await tester.pump(); // bottom sheet show animation starts
415
    await tester.pump(const Duration(seconds: 1)); // animation done
416 417 418 419 420
    expect(find.text('BottomSheet'), findsOneWidget);

    await tester.fling(find.text('BottomSheet'), const Offset(0.0, 400.0), 1000.0);
    await tester.pump(); // drain the microtask queue (Future completion callback)
    await tester.pump(); // bottom sheet dismiss animation starts
421
    await tester.pump(const Duration(seconds: 1)); // animation done
422 423 424

    expect(find.text('BottomSheet'), findsNothing);
  });
425

426
  testWidgets('modal BottomSheet has no top MediaQuery', (WidgetTester tester) async {
427 428
    late BuildContext outerContext;
    late BuildContext innerContext;
429

430
    await tester.pumpWidget(Localizations(
431
      locale: const Locale('en', 'US'),
432
      delegates: const <LocalizationsDelegate<dynamic>>[
433 434 435
        DefaultWidgetsLocalizations.delegate,
        DefaultMaterialLocalizations.delegate,
      ],
436
      child: Directionality(
437
        textDirection: TextDirection.ltr,
438
        child: MediaQuery(
439
          data: const MediaQueryData(
440
            padding: EdgeInsets.all(50.0),
441
            size: Size(400.0, 600.0),
442
          ),
443
          child: Navigator(
444
            onGenerateRoute: (_) {
445
              return PageRouteBuilder<void>(
446 447
                pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
                  outerContext = context;
448
                  return Container();
449 450 451 452
                },
              );
            },
          ),
453 454 455 456
        ),
      ),
    ));

457
    showModalBottomSheet<void>(
458 459 460
      context: outerContext,
      builder: (BuildContext context) {
        innerContext = context;
461
        return Container();
462 463 464 465 466 467
      },
    );
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(
468
      MediaQuery.of(outerContext).padding,
469 470 471
      const EdgeInsets.all(50.0),
    );
    expect(
472
      MediaQuery.of(innerContext).padding,
473 474 475
      const EdgeInsets.only(left: 50.0, right: 50.0, bottom: 50.0),
    );
  });
476 477

  testWidgets('modal BottomSheet has semantics', (WidgetTester tester) async {
478 479
    final SemanticsTester semantics = SemanticsTester(tester);
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
480

481 482
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
483
        key: scaffoldKey,
484 485
        body: const Center(child: Text('body')),
      ),
486 487 488
    ));


489
    showModalBottomSheet<void>(context: scaffoldKey.currentContext!, builder: (BuildContext context) {
490
      return const Text('BottomSheet');
491 492 493 494 495
    });

    await tester.pump(); // bottom sheet show animation starts
    await tester.pump(const Duration(seconds: 1)); // animation done

496
    expect(semantics, hasSemantics(TestSemantics.root(
497
      children: <TestSemantics>[
498
        TestSemantics.rootChild(
499
          children: <TestSemantics>[
500
            TestSemantics(
501
              children: <TestSemantics>[
502
                TestSemantics(
503
                  label: 'Dialog',
504
                  textDirection: TextDirection.ltr,
505 506 507 508 509 510 511 512 513 514
                  flags: <SemanticsFlag>[
                    SemanticsFlag.scopesRoute,
                    SemanticsFlag.namesRoute,
                  ],
                  children: <TestSemantics>[
                    TestSemantics(
                      label: 'BottomSheet',
                      textDirection: TextDirection.ltr,
                    ),
                  ],
515 516 517
                ),
              ],
            ),
518
            TestSemantics(),
519 520 521 522 523 524
          ],
        ),
      ],
    ), ignoreTransform: true, ignoreRect: true, ignoreId: true));
    semantics.dispose();
  });
525

526 527 528 529 530
  testWidgets('Verify that visual properties are passed through', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    const Color color = Colors.pink;
    const double elevation = 9.0;
    final ShapeBorder shape = BeveledRectangleBorder(borderRadius: BorderRadius.circular(12));
531
    const Clip clipBehavior = Clip.antiAlias;
532
    const Color barrierColor = Colors.red;
533 534 535 536 537 538 539 540 541

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
        body: const Center(child: Text('body')),
      ),
    ));

    showModalBottomSheet<void>(
542
      context: scaffoldKey.currentContext!,
543
      backgroundColor: color,
544
      barrierColor: barrierColor,
545 546
      elevation: elevation,
      shape: shape,
547
      clipBehavior: clipBehavior,
548
      builder: (BuildContext context) {
549
        return const Text('BottomSheet');
550 551 552 553 554 555 556 557 558 559
      },
    );

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    final BottomSheet bottomSheet = tester.widget(find.byType(BottomSheet));
    expect(bottomSheet.backgroundColor, color);
    expect(bottomSheet.elevation, elevation);
    expect(bottomSheet.shape, shape);
560
    expect(bottomSheet.clipBehavior, clipBehavior);
561 562 563

    final ModalBarrier modalBarrier = tester.widget(find.byType(ModalBarrier).last);
    expect(modalBarrier.color, barrierColor);
564 565
  });

566 567 568 569 570 571 572
  testWidgets('modal BottomSheet with scrollController has semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
573
        body: const Center(child: Text('body')),
574
      ),
575 576 577 578
    ));


    showModalBottomSheet<void>(
579
      context: scaffoldKey.currentContext!,
580 581 582 583 584 585
      builder: (BuildContext context) {
        return DraggableScrollableSheet(
          expand: false,
          builder: (_, ScrollController controller) {
            return SingleChildScrollView(
              controller: controller,
586
              child: const Text('BottomSheet'),
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
            );
          },
        );
      },
    );

    await tester.pump(); // bottom sheet show animation starts
    await tester.pump(const Duration(seconds: 1)); // animation done

    expect(semantics, hasSemantics(TestSemantics.root(
      children: <TestSemantics>[
        TestSemantics.rootChild(
          children: <TestSemantics>[
            TestSemantics(
              children: <TestSemantics>[
                TestSemantics(
603 604 605 606 607 608
                  label: 'Dialog',
                  textDirection: TextDirection.ltr,
                  flags: <SemanticsFlag>[
                    SemanticsFlag.scopesRoute,
                    SemanticsFlag.namesRoute,
                  ],
609 610
                  children: <TestSemantics>[
                    TestSemantics(
611 612 613 614 615 616 617 618
                      flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
                      actions: <SemanticsAction>[SemanticsAction.scrollDown, SemanticsAction.scrollUp],
                      children: <TestSemantics>[
                        TestSemantics(
                          label: 'BottomSheet',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
619 620 621 622 623
                    ),
                  ],
                ),
              ],
            ),
624
            TestSemantics(),
625 626 627 628 629 630
          ],
        ),
      ],
    ), ignoreTransform: true, ignoreRect: true, ignoreId: true));
    semantics.dispose();
  });
631 632 633 634 635 636 637 638 639 640 641

  testWidgets('showModalBottomSheet does not use root Navigator by default', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Navigator(onGenerateRoute: (RouteSettings settings) => MaterialPageRoute<void>(builder: (_) {
          return const _TestPage();
        })),
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.ac_unit),
642
              label: 'Item 1',
643 644 645
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.style),
646
              label: 'Item 2',
647
            ),
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
          ],
        ),
      ),
    ));

    await tester.tap(find.text('Show bottom sheet'));
    await tester.pumpAndSettle();

    // Bottom sheet is displayed in correct position within the inner navigator
    // and above the BottomNavigationBar.
    expect(tester.getBottomLeft(find.byType(BottomSheet)).dy, 544.0);
  });

  testWidgets('showModalBottomSheet uses root Navigator when specified', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Navigator(onGenerateRoute: (RouteSettings settings) => MaterialPageRoute<void>(builder: (_) {
          return const _TestPage(useRootNavigator: true);
        })),
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.ac_unit),
671
              label: 'Item 1',
672 673 674
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.style),
675
              label: 'Item 2',
676
            ),
677 678 679 680 681 682 683 684 685 686 687 688
          ],
        ),
      ),
    ));

    await tester.tap(find.text('Show bottom sheet'));
    await tester.pumpAndSettle();

    // Bottom sheet is displayed in correct position above all content including
    // the BottomNavigationBar.
    expect(tester.getBottomLeft(find.byType(BottomSheet)).dy, 600.0);
  });
689 690 691 692 693 694 695 696 697 698 699 700 701 702

  testWidgets('Verify that route settings can be set in the showModalBottomSheet',
      (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    const RouteSettings routeSettings =
        RouteSettings(name: 'route_name', arguments: 'route_argument');

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        key: scaffoldKey,
        body: const Center(child: Text('body')),
      ),
    ));

703
    late RouteSettings retrievedRouteSettings;
704 705

    showModalBottomSheet<void>(
706
      context: scaffoldKey.currentContext!,
707 708
      routeSettings: routeSettings,
      builder: (BuildContext context) {
709
        retrievedRouteSettings = ModalRoute.of(context)!.settings;
710
        return const Text('BottomSheet');
711 712 713 714 715 716 717 718
      },
    );

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(retrievedRouteSettings, routeSettings);
  });
719

720 721 722 723 724 725 726
  testWidgets('Verify showModalBottomSheet use AnimationController if provided.', (WidgetTester tester) async {
    const Key tapTarget = Key('tap-target');
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
          builder: (BuildContext context) {
            return GestureDetector(
727
              key: tapTarget,
728 729 730 731 732 733 734 735 736 737
              onTap: () {
                showModalBottomSheet<void>(
                  context: context,
                  // The default duration and reverseDuration is 1 second
                  transitionAnimationController: AnimationController(
                    vsync: const TestVSync(),
                    duration: const Duration(seconds: 2),
                    reverseDuration: const Duration(seconds: 2),
                  ),
                  builder: (BuildContext context) {
738
                    return const Text('BottomSheet');
739 740 741 742
                  },
                );
              },
              behavior: HitTestBehavior.opaque,
743
              child: const SizedBox(
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
                height: 100.0,
                width: 100.0,
              ),
            );
          },
        ),
      ),
    ));

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

    await tester.tap(find.byKey(tapTarget)); // Opening animation will start after tapping
    await tester.pump();

    expect(find.text('BottomSheet'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 2000));
    expect(find.text('BottomSheet'), findsOneWidget);

    // Tapping above the bottom sheet to dismiss it.
    await tester.tapAt(const Offset(20.0, 20.0)); // Closing animation will start after tapping
    await tester.pump();

    expect(find.text('BottomSheet'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 2000));
    // The bottom sheet should still be present at the very end of the animation.
    expect(find.text('BottomSheet'), findsOneWidget);

    await tester.pump(const Duration(milliseconds: 1));
    // The bottom sheet should not be showing any longer.
    expect(find.text('BottomSheet'), findsNothing);
  });

  testWidgets('Verify persistence BottomSheet use AnimationController if provided.', (WidgetTester tester) async {
    const Key tapTarget = Key('tap-target');
    const Key tapTargetToClose = Key('tap-target-to-close');
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
          builder: (BuildContext context) {
            return GestureDetector(
784
              key: tapTarget,
785 786 787 788 789 790 791 792 793 794
              onTap: () {
                showBottomSheet<void>(
                  context: context,
                  // The default duration and reverseDuration is 1 second
                  transitionAnimationController: AnimationController(
                    vsync: const TestVSync(),
                    duration: const Duration(seconds: 2),
                    reverseDuration: const Duration(seconds: 2),
                  ),
                  builder: (BuildContext context) {
795 796 797 798
                    return MaterialButton(
                      child: const Text('BottomSheet'),
                      onPressed: () => Navigator.pop(context),
                      key: tapTargetToClose,
799 800 801 802 803
                    );
                  },
                );
              },
              behavior: HitTestBehavior.opaque,
804
              child: const SizedBox(
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
                height: 100.0,
                width: 100.0,
              ),
            );
          },
        ),
      ),
    ));

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

    await tester.tap(find.byKey(tapTarget)); // Opening animation will start after tapping
    await tester.pump();

    expect(find.text('BottomSheet'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 2000));
    expect(find.text('BottomSheet'), findsOneWidget);

    // Tapping button on the bottom sheet to dismiss it.
    await tester.tap(find.byKey(tapTargetToClose)); // Closing animation will start after tapping
    await tester.pump();

    expect(find.text('BottomSheet'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 2000));
    // The bottom sheet should still be present at the very end of the animation.
    expect(find.text('BottomSheet'), findsOneWidget);

    await tester.pump(const Duration(milliseconds: 1));
    // The bottom sheet should not be showing any longer.
    expect(find.text('BottomSheet'), findsNothing);
  });
836 837 838
}

class _TestPage extends StatelessWidget {
839
  const _TestPage({Key? key, this.useRootNavigator}) : super(key: key);
840

841
  final bool? useRootNavigator;
842 843 844 845

  @override
  Widget build(BuildContext context) {
    return Center(
846
      child: TextButton(
847 848 849 850
        child: const Text('Show bottom sheet'),
        onPressed: () {
          if (useRootNavigator != null) {
            showModalBottomSheet<void>(
851
              useRootNavigator: useRootNavigator!,
852 853 854 855 856 857 858 859 860
              context: context,
              builder: (_) => const Text('Modal bottom sheet'),
            );
          } else {
            showModalBottomSheet<void>(
              context: context,
              builder: (_) => const Text('Modal bottom sheet'),
            );
          }
861
        },
862 863 864
      ),
    );
  }
865
}