scaffold_test.dart 72.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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 8
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
9
import 'package:flutter_test/flutter_test.dart';
10

11 12
import '../widgets/semantics_tester.dart';

13
void main() {
14
  testWidgets('Scaffold control test', (WidgetTester tester) async {
15
    final Key bodyKey = UniqueKey();
16 17 18 19 20 21 22 23 24 25 26 27 28 29
    Widget boilerplate(Widget child) {
      return Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: child,
        ),
      );
    }
    await tester.pumpWidget(boilerplate(Scaffold(
30 31
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
32
      ),
33
    ));
34
    expect(tester.takeException(), isFlutterError);
35

36 37 38 39
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
40 41
      ),
    ));
42
    RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
43
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));
44

45
    await tester.pumpWidget(boilerplate(MediaQuery(
46
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
47 48 49
        child: Scaffold(
          appBar: AppBar(title: const Text('Title')),
          body: Container(key: bodyKey),
50 51
        ),
      ),
52
    ));
53

54
    bodyBox = tester.renderObject(find.byKey(bodyKey));
55
    expect(bodyBox.size, equals(const Size(800.0, 444.0)));
56

57 58 59 60 61
    await tester.pumpWidget(boilerplate(MediaQuery(
      data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
      child: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
62 63 64 65 66 67 68
        resizeToAvoidBottomInset: false,
      ),
    )));

    bodyBox = tester.renderObject(find.byKey(bodyKey));
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));

Chris Bracken's avatar
Chris Bracken committed
69
    // Backwards compatibility: deprecated resizeToAvoidBottomPadding flag
70 71 72 73 74
    await tester.pumpWidget(boilerplate(MediaQuery(
      data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
      child: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
75
        resizeToAvoidBottomPadding: false,
76
      ),
77
    )));
78

79
    bodyBox = tester.renderObject(find.byKey(bodyKey));
80
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));
81
  });
82

83
  testWidgets('Scaffold large bottom padding test', (WidgetTester tester) async {
84
    final Key bodyKey = UniqueKey();
85 86 87 88 89 90 91 92 93 94 95

    Widget boilerplate(Widget child) {
      return Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: child,
96
        ),
97 98 99 100 101 102
      );
    }

    await tester.pumpWidget(boilerplate(MediaQuery(
      data: const MediaQueryData(
        viewInsets: EdgeInsets.only(bottom: 700.0),
103
      ),
104 105
      child: Scaffold(
        body: Container(key: bodyKey),
106 107
      ),
    )));
108

109
    final RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
110 111
    expect(bodyBox.size, equals(const Size(800.0, 0.0)));

112
    await tester.pumpWidget(boilerplate(MediaQuery(
113
        data: const MediaQueryData(
114
          viewInsets: EdgeInsets.only(bottom: 500.0),
115
        ),
116 117
        child: Scaffold(
          body: Container(key: bodyKey),
118
        ),
119 120 121 122 123
      ),
    ));

    expect(bodyBox.size, equals(const Size(800.0, 100.0)));

124
    await tester.pumpWidget(boilerplate(MediaQuery(
125
        data: const MediaQueryData(
126
          viewInsets: EdgeInsets.only(bottom: 580.0),
127
        ),
128 129
        child: Scaffold(
          appBar: AppBar(
130 131
            title: const Text('Title'),
          ),
132
          body: Container(key: bodyKey),
133 134 135 136 137 138 139
        ),
      ),
    ));

    expect(bodyBox.size, equals(const Size(800.0, 0.0)));
  });

140
  testWidgets('Floating action entrance/exit animation', (WidgetTester tester) async {
141
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
142 143
      floatingActionButton: FloatingActionButton(
        key: Key('one'),
144
        onPressed: null,
145
        child: Text('1'),
146
      ),
147
    )));
148 149 150

    expect(tester.binding.transientCallbackCount, 0);

151
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
152 153
      floatingActionButton: FloatingActionButton(
        key: Key('two'),
154
        onPressed: null,
155
        child: Text('2'),
156
      ),
157
    )));
158 159

    expect(tester.binding.transientCallbackCount, greaterThan(0));
160
    await tester.pumpWidget(Container());
161
    expect(tester.binding.transientCallbackCount, 0);
162

163
    await tester.pumpWidget(const MaterialApp(home: Scaffold()));
164

165 166
    expect(tester.binding.transientCallbackCount, 0);

167
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
168 169
      floatingActionButton: FloatingActionButton(
        key: Key('one'),
170
        onPressed: null,
171
        child: Text('1'),
172
      ),
173
    )));
174 175 176

    expect(tester.binding.transientCallbackCount, greaterThan(0));
  });
177

178
  testWidgets('Floating action button directionality', (WidgetTester tester) async {
179
    Widget build(TextDirection textDirection) {
180
      return Directionality(
181
        textDirection: textDirection,
182
        child: const MediaQuery(
183 184
          data: MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 200.0),
185
          ),
186 187
          child: Scaffold(
            floatingActionButton: FloatingActionButton(
188
              onPressed: null,
189
              child: Text('1'),
190 191 192 193 194 195 196 197 198 199 200
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(build(TextDirection.ltr));

    expect(tester.getCenter(find.byType(FloatingActionButton)), const Offset(756.0, 356.0));

    await tester.pumpWidget(build(TextDirection.rtl));
201
    expect(tester.binding.transientCallbackCount, 0);
202 203 204 205

    expect(tester.getCenter(find.byType(FloatingActionButton)), const Offset(44.0, 356.0));
  });

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  testWidgets('Floating Action Button bottom padding not consumed by viewInsets', (WidgetTester tester) async {
    final Widget child = Directionality(
      textDirection: TextDirection.ltr,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: Container(),
        floatingActionButton: const Placeholder(),
      ),
    );

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(bottom: 20.0),
        ),
        child: child,
222
      ),
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    );
    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );
    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
    expect(initialPoint, finalPoint);
  });

240
  testWidgets('Drawer scrolling', (WidgetTester tester) async {
241
    final Key drawerKey = UniqueKey();
242 243
    const double appBarHeight = 256.0;

244
    final ScrollController scrollOffset = ScrollController();
245

246
    await tester.pumpWidget(
247 248 249
      MaterialApp(
        home: Scaffold(
          drawer: Drawer(
250
            key: drawerKey,
251
            child: ListView(
252
              dragStartBehavior: DragStartBehavior.down,
253
              controller: scrollOffset,
254
              children: List<Widget>.generate(10,
255 256 257
                (int index) => SizedBox(height: 100.0, child: Text('D$index')),
              ),
            ),
258
          ),
259
          body: CustomScrollView(
260
            slivers: <Widget>[
261
              const SliverAppBar(
262 263
                pinned: true,
                expandedHeight: appBarHeight,
264 265
                title: Text('Title'),
                flexibleSpace: FlexibleSpaceBar(title: Text('Title')),
266
              ),
267
              SliverPadding(
268
                padding: const EdgeInsets.only(top: appBarHeight),
269 270 271
                sliver: SliverList(
                  delegate: SliverChildListDelegate(List<Widget>.generate(
                    10, (int index) => SizedBox(height: 100.0, child: Text('B$index')),
272 273 274 275
                  )),
                ),
              ),
            ],
276
          ),
277
        ),
278
      ),
279 280
    );

281
    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
282 283 284 285 286
    state.openDrawer();

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

287
    expect(scrollOffset.offset, 0.0);
288 289

    const double scrollDelta = 80.0;
290
    await tester.drag(find.byKey(drawerKey), const Offset(0.0, -scrollDelta));
291 292
    await tester.pump();

293
    expect(scrollOffset.offset, scrollDelta);
294

295
    final RenderBox renderBox = tester.renderObject(find.byType(AppBar));
296 297
    expect(renderBox.size.height, equals(appBarHeight));
  });
298

299
  Widget _buildStatusBarTestApp(TargetPlatform platform) {
300 301 302
    return MaterialApp(
      theme: ThemeData(platform: platform),
      home: MediaQuery(
303
        data: const MediaQueryData(padding: EdgeInsets.only(top: 25.0)), // status bar
304 305
        child: Scaffold(
          body: CustomScrollView(
306 307
            primary: true,
            slivers: <Widget>[
308
              const SliverAppBar(
309
                title: Text('Title'),
310
              ),
311 312 313
              SliverList(
                delegate: SliverChildListDelegate(List<Widget>.generate(
                  20, (int index) => SizedBox(height: 100.0, child: Text('$index')),
314 315 316 317 318 319
                )),
              ),
            ],
          ),
        ),
      ),
320
    );
321
  }
322

323 324
  testWidgets('Tapping the status bar scrolls to top on iOS', (WidgetTester tester) async {
    await tester.pumpWidget(_buildStatusBarTestApp(TargetPlatform.iOS));
Adam Barth's avatar
Adam Barth committed
325
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
326 327
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
328
    await tester.tapAt(const Offset(100.0, 10.0));
329
    await tester.pumpAndSettle();
330
    expect(scrollable.position.pixels, equals(0.0));
331 332 333
  });

  testWidgets('Tapping the status bar does not scroll to top on Android', (WidgetTester tester) async {
334
    await tester.pumpWidget(_buildStatusBarTestApp(TargetPlatform.android));
Adam Barth's avatar
Adam Barth committed
335
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
336 337
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
338
    await tester.tapAt(const Offset(100.0, 10.0));
339
    await tester.pump();
340
    await tester.pump(const Duration(seconds: 1));
341
    expect(scrollable.position.pixels, equals(500.0));
342
  });
343 344

  testWidgets('Bottom sheet cannot overlap app bar', (WidgetTester tester) async {
345
    final Key sheetKey = UniqueKey();
346 347

    await tester.pumpWidget(
348 349 350 351
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
352
            title: const Text('Title'),
353
          ),
354
          body: Builder(
355
            builder: (BuildContext context) {
356
              return GestureDetector(
357
                onTap: () {
358
                  Scaffold.of(context).showBottomSheet<void>((BuildContext context) {
359
                    return Container(
360
                      key: sheetKey,
361
                      color: Colors.blue[500],
362 363 364
                    );
                  });
                },
365
                child: const Text('X'),
366
              );
367 368 369 370
            },
          ),
        ),
      ),
371 372 373 374 375
    );
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(seconds: 1));

376 377
    final RenderBox appBarBox = tester.renderObject(find.byType(AppBar));
    final RenderBox sheetBox = tester.renderObject(find.byKey(sheetKey));
378

379 380
    final Offset appBarBottomRight = appBarBox.localToGlobal(appBarBox.size.bottomRight(Offset.zero));
    final Offset sheetTopRight = sheetBox.localToGlobal(sheetBox.size.topRight(Offset.zero));
381 382 383

    expect(appBarBottomRight, equals(sheetTopRight));
  });
384

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  testWidgets('BottomSheet bottom padding is not consumed by viewInsets', (WidgetTester tester) async {
    final Widget child = Directionality(
      textDirection: TextDirection.ltr,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: Container(),
        bottomSheet: const Placeholder(),
      ),
    );

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(bottom: 20.0),
        ),
        child: child,
401
      ),
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    );
    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );
    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
    expect(initialPoint, finalPoint);
  });

419 420 421
  testWidgets('Persistent bottom buttons are persistent', (WidgetTester tester) async {
    bool didPressButton = false;
    await tester.pumpWidget(
422 423 424 425
      MaterialApp(
        home: Scaffold(
          body: SingleChildScrollView(
            child: Container(
426
              color: Colors.amber[500],
427
              height: 5000.0,
428
              child: const Text('body'),
429 430 431
            ),
          ),
          persistentFooterButtons: <Widget>[
432
            FlatButton(
433 434 435
              onPressed: () {
                didPressButton = true;
              },
436
              child: const Text('X'),
437
            ),
438 439 440 441 442
          ],
        ),
      ),
    );

443
    await tester.drag(find.text('body'), const Offset(0.0, -1000.0));
444 445 446 447 448
    expect(didPressButton, isFalse);
    await tester.tap(find.text('X'));
    expect(didPressButton, isTrue);
  });

449 450
  testWidgets('Persistent bottom buttons apply media padding', (WidgetTester tester) async {
    await tester.pumpWidget(
451
      Directionality(
452
        textDirection: TextDirection.ltr,
453
        child: MediaQuery(
454
          data: const MediaQueryData(
455
            padding: EdgeInsets.fromLTRB(10.0, 20.0, 30.0, 40.0),
456
          ),
457 458 459
          child: Scaffold(
            body: SingleChildScrollView(
              child: Container(
460 461 462 463 464
                color: Colors.amber[500],
                height: 5000.0,
                child: const Text('body'),
              ),
            ),
465
            persistentFooterButtons: const <Widget>[Placeholder()],
466 467 468 469 470 471 472 473
          ),
        ),
      ),
    );
    expect(tester.getBottomLeft(find.byType(ButtonBar)), const Offset(10.0, 560.0));
    expect(tester.getBottomRight(find.byType(ButtonBar)), const Offset(770.0, 560.0));
  });

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  testWidgets('Persistent bottom buttons bottom padding is not consumed by viewInsets', (WidgetTester tester) async {
    final Widget child = Directionality(
      textDirection: TextDirection.ltr,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: Container(),
        persistentFooterButtons: const <Widget>[Placeholder()],
      ),
    );

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(bottom: 20.0),
        ),
        child: child,
490
      ),
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    );
    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );
    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
    expect(initialPoint, finalPoint);
  });

508
  group('back arrow', () {
509
    Future<void> expectBackIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon) async {
510
      final GlobalKey rootKey = GlobalKey();
511
      final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
512 513 514
        '/': (_) => Container(key: rootKey, child: const Text('Home')),
        '/scaffold': (_) => Scaffold(
            appBar: AppBar(),
515
            body: const Text('Scaffold'),
516
        ),
517 518
      };
      await tester.pumpWidget(
519
        MaterialApp(theme: ThemeData(platform: platform), routes: routes)
520 521 522 523 524 525
      );

      Navigator.pushNamed(rootKey.currentContext, '/scaffold');
      await tester.pump();
      await tester.pump(const Duration(seconds: 1));

526
      final Icon icon = tester.widget(find.byType(Icon));
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
      expect(icon.icon, expectedIcon);
    }

    testWidgets('Back arrow uses correct default on Android', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.android, Icons.arrow_back);
    });

    testWidgets('Back arrow uses correct default on Fuchsia', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.fuchsia, Icons.arrow_back);
    });

    testWidgets('Back arrow uses correct default on iOS', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.iOS, Icons.arrow_back_ios);
    });
  });
542

543
  group('close button', () {
544
    Future<void> expectCloseIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon, PageRoute<void> routeBuilder()) async {
545
      await tester.pumpWidget(
546 547 548
        MaterialApp(
          theme: ThemeData(platform: platform),
          home: Scaffold(appBar: AppBar(), body: const Text('Page 1')),
549
        ),
550 551
      );

552
      tester.state<NavigatorState>(find.byType(Navigator)).push(routeBuilder());
553 554 555 556 557 558

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

      final Icon icon = tester.widget(find.byType(Icon));
      expect(icon.icon, expectedIcon);
559 560 561 562
      expect(find.byType(CloseButton), findsOneWidget);
    }

    PageRoute<void> materialRouteBuilder() {
563
      return MaterialPageRoute<void>(
564
        builder: (BuildContext context) {
565
          return Scaffold(appBar: AppBar(), body: const Text('Page 2'));
566 567 568 569 570
        },
        fullscreenDialog: true,
      );
    }

571 572 573 574 575 576 577 578 579
    PageRoute<void> pageRouteBuilder() {
      return PageRouteBuilder<void>(
        pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
          return Scaffold(appBar: AppBar(), body: const Text('Page 2'));
        },
        fullscreenDialog: true,
      );
    }

580
    PageRoute<void> customPageRouteBuilder() {
581
      return _CustomPageRoute<void>(
582
        builder: (BuildContext context) {
583
          return Scaffold(appBar: AppBar(), body: const Text('Page 2'));
584 585 586
        },
        fullscreenDialog: true,
      );
587 588 589
    }

    testWidgets('Close button shows correctly on Android', (WidgetTester tester) async {
590
      await expectCloseIcon(tester, TargetPlatform.android, Icons.close, materialRouteBuilder);
591 592 593
    });

    testWidgets('Close button shows correctly on Fuchsia', (WidgetTester tester) async {
594
      await expectCloseIcon(tester, TargetPlatform.fuchsia, Icons.close, materialRouteBuilder);
595 596 597
    });

    testWidgets('Close button shows correctly on iOS', (WidgetTester tester) async {
598 599 600
      await expectCloseIcon(tester, TargetPlatform.iOS, Icons.close, materialRouteBuilder);
    });

601 602 603 604 605 606 607 608 609 610 611 612
    testWidgets('Close button shows correctly with PageRouteBuilder on Android', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.android, Icons.close, pageRouteBuilder);
    });

    testWidgets('Close button shows correctly with PageRouteBuilder on Fuchsia', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.fuchsia, Icons.close, pageRouteBuilder);
    });

    testWidgets('Close button shows correctly with PageRouteBuilder on iOS', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.iOS, Icons.close, pageRouteBuilder);
    });

613 614 615 616 617 618 619 620 621 622
    testWidgets('Close button shows correctly with custom page route on Android', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.android, Icons.close, customPageRouteBuilder);
    });

    testWidgets('Close button shows correctly with custom page route on Fuchsia', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.fuchsia, Icons.close, customPageRouteBuilder);
    });

    testWidgets('Close button shows correctly with custom page route on iOS', (WidgetTester tester) async {
      await expectCloseIcon(tester, TargetPlatform.iOS, Icons.close, customPageRouteBuilder);
623 624 625
    });
  });

626 627
  group('body size', () {
    testWidgets('body size with container', (WidgetTester tester) async {
628 629
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
630
        textDirection: TextDirection.ltr,
631
        child: MediaQuery(
632
          data: const MediaQueryData(),
633 634
          child: Scaffold(
            body: Container(
635 636 637 638
              key: testKey,
            ),
          ),
        ),
639
      ));
640
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
641
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
642 643 644
    });

    testWidgets('body size with sized container', (WidgetTester tester) async {
645 646
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
647
        textDirection: TextDirection.ltr,
648
        child: MediaQuery(
649
          data: const MediaQueryData(),
650 651
          child: Scaffold(
            body: Container(
652 653 654 655 656
              key: testKey,
              height: 100.0,
            ),
          ),
        ),
657
      ));
658
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 100.0));
659
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
660 661 662
    });

    testWidgets('body size with centered container', (WidgetTester tester) async {
663 664
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
665
        textDirection: TextDirection.ltr,
666
        child: MediaQuery(
667
          data: const MediaQueryData(),
668 669 670
          child: Scaffold(
            body: Center(
              child: Container(
671 672 673 674 675
                key: testKey,
              ),
            ),
          ),
        ),
676
      ));
677
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
678
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
679 680 681
    });

    testWidgets('body size with button', (WidgetTester tester) async {
682 683
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
684
        textDirection: TextDirection.ltr,
685
        child: MediaQuery(
686
          data: const MediaQueryData(),
687 688
          child: Scaffold(
            body: FlatButton(
689 690 691 692 693 694
              key: testKey,
              onPressed: () { },
              child: const Text(''),
            ),
          ),
        ),
695
      ));
696
      expect(tester.element(find.byKey(testKey)).size, const Size(88.0, 48.0));
697
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
698
    });
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

    testWidgets('body size with extendBody', (WidgetTester tester) async {
      final Key bodyKey = UniqueKey();
      double mediaQueryBottom;

      Widget buildFrame({ bool extendBody, bool resizeToAvoidBottomInset, double viewInsetBottom = 0.0 }) {
        return Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: MediaQueryData(
              viewInsets: EdgeInsets.only(bottom: viewInsetBottom),
            ),
            child: Scaffold(
              resizeToAvoidBottomInset: resizeToAvoidBottomInset,
              extendBody: extendBody,
              body: Builder(
                builder: (BuildContext context) {
                  mediaQueryBottom = MediaQuery.of(context).padding.bottom;
                  return Container(key: bodyKey);
                },
              ),
              bottomNavigationBar: const BottomAppBar(
                child: SizedBox(height: 48.0,),
              ),
            ),
          ),
        );
      }

      await tester.pumpWidget(buildFrame(extendBody: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(mediaQueryBottom, 48.0);

      await tester.pumpWidget(buildFrame(extendBody: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 552.0)); // 552 = 600 - 48 (BAB height)
      expect(mediaQueryBottom, 0.0);

      // If resizeToAvoidBottomInsets is false, same results as if it was unspecified (null).
      await tester.pumpWidget(buildFrame(extendBody: true, resizeToAvoidBottomInset: false, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(mediaQueryBottom, 48.0);

      await tester.pumpWidget(buildFrame(extendBody: false, resizeToAvoidBottomInset: false, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 552.0));
      expect(mediaQueryBottom, 0.0);

      // If resizeToAvoidBottomInsets is true and viewInsets.bottom is > the bottom
      // navigation bar's height then the body always resizes and the MediaQuery
      // isn't adjusted. This case corresponds to the keyboard appearing.
      await tester.pumpWidget(buildFrame(extendBody: true, resizeToAvoidBottomInset: true, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));
      expect(mediaQueryBottom, 0.0);

      await tester.pumpWidget(buildFrame(extendBody: false, resizeToAvoidBottomInset: true, viewInsetBottom: 100.0));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));
      expect(mediaQueryBottom, 0.0);
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 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 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 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

    testWidgets('body size with extendBodyBehindAppBar', (WidgetTester tester) async {
      final Key appBarKey = UniqueKey();
      final Key bodyKey = UniqueKey();

      const double appBarHeight = 100;
      const double windowPaddingTop = 24;
      bool fixedHeightAppBar;
      double mediaQueryTop;

      Widget buildFrame({ bool extendBodyBehindAppBar, bool hasAppBar }) {
        return Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(top: windowPaddingTop),
            ),
            child: Builder(
              builder: (BuildContext context) {
                return Scaffold(
                  extendBodyBehindAppBar: extendBodyBehindAppBar,
                  appBar: !hasAppBar ? null : PreferredSize(
                    key: appBarKey,
                    preferredSize: const Size.fromHeight(appBarHeight),
                    child: Container(
                      constraints: BoxConstraints(
                        minHeight: appBarHeight,
                        maxHeight: fixedHeightAppBar ? appBarHeight : double.infinity,
                      ),
                    ),
                  ),
                  body: Builder(
                    builder: (BuildContext context) {
                      mediaQueryTop = MediaQuery.of(context).padding.top;
                      return Container(key: bodyKey);
                    }
                  ),
                );
              },
            ),
          ),
        );
      }

      fixedHeightAppBar = false;

      // When an appbar is provided, the Scaffold's body is built within a
      // MediaQuery with padding.top = 0, and the appBar's maxHeight is
      // constrained to its preferredSize.height + the original MediaQuery
      // padding.top. When extendBodyBehindAppBar is true, an additional
      // inner MediaQuery is added around the Scaffold's body with padding.top
      // equal to the overall height of the appBar. See _BodyBuilder in
      // material/scaffold.dart.

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight + windowPaddingTop));
      expect(mediaQueryTop, appBarHeight + windowPaddingTop);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0 - appBarHeight - windowPaddingTop));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight + windowPaddingTop));
      expect(mediaQueryTop, 0.0);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);

      fixedHeightAppBar = true;

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight));
      expect(mediaQueryTop, appBarHeight);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: true, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: true));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0 - appBarHeight));
      expect(tester.getSize(find.byKey(appBarKey)), const Size(800.0, appBarHeight));
      expect(mediaQueryTop, 0.0);

      await tester.pumpWidget(buildFrame(extendBodyBehindAppBar: false, hasAppBar: false));
      expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));
      expect(find.byKey(appBarKey), findsNothing);
      expect(mediaQueryTop, windowPaddingTop);
    });
852
  });
853 854 855 856 857 858 859 860

  testWidgets('Open drawer hides underlying semantics tree', (WidgetTester tester) async {
    const String bodyLabel = 'I am the body';
    const String persistentFooterButtonLabel = 'a button on the bottom';
    const String bottomNavigationBarLabel = 'a bar in an app';
    const String floatingActionButtonLabel = 'I float in space';
    const String drawerLabel = 'I am the reason for this test';

861
    final SemanticsTester semantics = SemanticsTester(tester);
862
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
863 864 865 866 867
      body: Text(bodyLabel),
      persistentFooterButtons: <Widget>[Text(persistentFooterButtonLabel)],
      bottomNavigationBar: Text(bottomNavigationBarLabel),
      floatingActionButton: Text(floatingActionButtonLabel),
      drawer: Drawer(child: Text(drawerLabel)),
868 869
    )));

870 871 872 873 874
    expect(semantics, includesNodeWith(label: bodyLabel));
    expect(semantics, includesNodeWith(label: persistentFooterButtonLabel));
    expect(semantics, includesNodeWith(label: bottomNavigationBarLabel));
    expect(semantics, includesNodeWith(label: floatingActionButtonLabel));
    expect(semantics, isNot(includesNodeWith(label: drawerLabel)));
875 876 877 878 879 880

    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
    state.openDrawer();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

881 882 883 884 885
    expect(semantics, isNot(includesNodeWith(label: bodyLabel)));
    expect(semantics, isNot(includesNodeWith(label: persistentFooterButtonLabel)));
    expect(semantics, isNot(includesNodeWith(label: bottomNavigationBarLabel)));
    expect(semantics, isNot(includesNodeWith(label: floatingActionButtonLabel)));
    expect(semantics, includesNodeWith(label: drawerLabel));
886 887 888

    semantics.dispose();
  });
Ian Hickson's avatar
Ian Hickson committed
889 890

  testWidgets('Scaffold and extreme window padding', (WidgetTester tester) async {
891 892 893 894 895 896 897 898 899 900 901 902
    final Key appBar = UniqueKey();
    final Key body = UniqueKey();
    final Key floatingActionButton = UniqueKey();
    final Key persistentFooterButton = UniqueKey();
    final Key drawer = UniqueKey();
    final Key bottomNavigationBar = UniqueKey();
    final Key insideAppBar = UniqueKey();
    final Key insideBody = UniqueKey();
    final Key insideFloatingActionButton = UniqueKey();
    final Key insidePersistentFooterButton = UniqueKey();
    final Key insideDrawer = UniqueKey();
    final Key insideBottomNavigationBar = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
903
    await tester.pumpWidget(
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
      Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(
                left: 20.0,
                top: 30.0,
                right: 50.0,
                bottom: 60.0,
Ian Hickson's avatar
Ian Hickson committed
919
              ),
920
              viewInsets: EdgeInsets.only(bottom: 200.0),
Ian Hickson's avatar
Ian Hickson committed
921
            ),
922
            child: Scaffold(
923
              drawerDragStartBehavior: DragStartBehavior.down,
924 925 926 927 928 929 930 931
              appBar: PreferredSize(
                preferredSize: const Size(11.0, 13.0),
                child: Container(
                  key: appBar,
                  child: SafeArea(
                    child: Placeholder(key: insideAppBar),
                  ),
                ),
Ian Hickson's avatar
Ian Hickson committed
932
              ),
933 934 935 936 937
              body: Container(
                key: body,
                child: SafeArea(
                  child: Placeholder(key: insideBody),
                ),
Ian Hickson's avatar
Ian Hickson committed
938
              ),
939 940 941 942
              floatingActionButton: SizedBox(
                key: floatingActionButton,
                width: 77.0,
                height: 77.0,
943
                child: SafeArea(
944
                  child: Placeholder(key: insideFloatingActionButton),
Ian Hickson's avatar
Ian Hickson committed
945 946
                ),
              ),
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
              persistentFooterButtons: <Widget>[
                SizedBox(
                  key: persistentFooterButton,
                  width: 100.0,
                  height: 90.0,
                  child: SafeArea(
                    child: Placeholder(key: insidePersistentFooterButton),
                  ),
                ),
              ],
              drawer: Container(
                key: drawer,
                width: 204.0,
                child: SafeArea(
                  child: Placeholder(key: insideDrawer),
                ),
Ian Hickson's avatar
Ian Hickson committed
963
              ),
964 965 966 967 968 969
              bottomNavigationBar: SizedBox(
                key: bottomNavigationBar,
                height: 85.0,
                child: SafeArea(
                  child: Placeholder(key: insideBottomNavigationBar),
                ),
Ian Hickson's avatar
Ian Hickson committed
970 971 972 973 974 975 976 977 978 979 980
              ),
            ),
          ),
        ),
      ),
    );
    // open drawer
    await tester.flingFrom(const Offset(795.0, 5.0), const Offset(-200.0, 0.0), 10.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

Dan Field's avatar
Dan Field committed
981 982 983 984 985 986 987 988 989 990 991 992
    expect(tester.getRect(find.byKey(appBar)), const Rect.fromLTRB(0.0, 0.0, 800.0, 43.0));
    expect(tester.getRect(find.byKey(body)), const Rect.fromLTRB(0.0, 43.0, 800.0, 348.0));
    expect(tester.getRect(find.byKey(floatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 255.0, 113.0, 332.0)));
    expect(tester.getRect(find.byKey(persistentFooterButton)),const  Rect.fromLTRB(28.0, 357.0, 128.0, 447.0)); // Note: has 8px each top/bottom padding.
    expect(tester.getRect(find.byKey(drawer)), const Rect.fromLTRB(596.0, 0.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(bottomNavigationBar)), const Rect.fromLTRB(0.0, 515.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(insideAppBar)), const Rect.fromLTRB(20.0, 30.0, 750.0, 43.0));
    expect(tester.getRect(find.byKey(insideBody)), const Rect.fromLTRB(20.0, 43.0, 750.0, 348.0));
    expect(tester.getRect(find.byKey(insideFloatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 255.0, 113.0, 332.0)));
    expect(tester.getRect(find.byKey(insidePersistentFooterButton)), const Rect.fromLTRB(28.0, 357.0, 128.0, 447.0));
    expect(tester.getRect(find.byKey(insideDrawer)), const Rect.fromLTRB(596.0, 30.0, 750.0, 540.0));
    expect(tester.getRect(find.byKey(insideBottomNavigationBar)), const Rect.fromLTRB(20.0, 515.0, 750.0, 540.0));
993 994 995
  });

  testWidgets('Scaffold and extreme window padding - persistent footer buttons only', (WidgetTester tester) async {
996 997 998 999 1000 1001 1002 1003 1004 1005
    final Key appBar = UniqueKey();
    final Key body = UniqueKey();
    final Key floatingActionButton = UniqueKey();
    final Key persistentFooterButton = UniqueKey();
    final Key drawer = UniqueKey();
    final Key insideAppBar = UniqueKey();
    final Key insideBody = UniqueKey();
    final Key insideFloatingActionButton = UniqueKey();
    final Key insidePersistentFooterButton = UniqueKey();
    final Key insideDrawer = UniqueKey();
1006
    await tester.pumpWidget(
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
      Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(
                left: 20.0,
                top: 30.0,
                right: 50.0,
                bottom: 60.0,
1022
              ),
1023
              viewInsets: EdgeInsets.only(bottom: 200.0),
1024
            ),
1025 1026 1027 1028 1029 1030 1031 1032 1033
            child: Scaffold(
              appBar: PreferredSize(
                preferredSize: const Size(11.0, 13.0),
                child: Container(
                  key: appBar,
                  child: SafeArea(
                    child: Placeholder(key: insideAppBar),
                  ),
                ),
1034
              ),
1035 1036 1037 1038 1039
              body: Container(
                key: body,
                child: SafeArea(
                  child: Placeholder(key: insideBody),
                ),
1040
              ),
1041 1042 1043 1044
              floatingActionButton: SizedBox(
                key: floatingActionButton,
                width: 77.0,
                height: 77.0,
1045
                child: SafeArea(
1046
                  child: Placeholder(key: insideFloatingActionButton),
1047 1048
                ),
              ),
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
              persistentFooterButtons: <Widget>[
                SizedBox(
                  key: persistentFooterButton,
                  width: 100.0,
                  height: 90.0,
                  child: SafeArea(
                    child: Placeholder(key: insidePersistentFooterButton),
                  ),
                ),
              ],
              drawer: Container(
                key: drawer,
                width: 204.0,
                child: SafeArea(
                  child: Placeholder(key: insideDrawer),
                ),
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
              ),
            ),
          ),
        ),
      ),
    );
    // open drawer
    await tester.flingFrom(const Offset(795.0, 5.0), const Offset(-200.0, 0.0), 10.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

Dan Field's avatar
Dan Field committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    expect(tester.getRect(find.byKey(appBar)), const Rect.fromLTRB(0.0, 0.0, 800.0, 43.0));
    expect(tester.getRect(find.byKey(body)), const Rect.fromLTRB(0.0, 43.0, 800.0, 400.0));
    expect(tester.getRect(find.byKey(floatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 307.0, 113.0, 384.0)));
    expect(tester.getRect(find.byKey(persistentFooterButton)), const Rect.fromLTRB(28.0, 442.0, 128.0, 532.0)); // Note: has 8px each top/bottom padding.
    expect(tester.getRect(find.byKey(drawer)), const Rect.fromLTRB(596.0, 0.0, 800.0, 600.0));
    expect(tester.getRect(find.byKey(insideAppBar)), const Rect.fromLTRB(20.0, 30.0, 750.0, 43.0));
    expect(tester.getRect(find.byKey(insideBody)), const Rect.fromLTRB(20.0, 43.0, 750.0, 400.0));
    expect(tester.getRect(find.byKey(insideFloatingActionButton)), rectMoreOrLessEquals(const Rect.fromLTRB(36.0, 307.0, 113.0, 384.0)));
    expect(tester.getRect(find.byKey(insidePersistentFooterButton)), const Rect.fromLTRB(28.0, 442.0, 128.0, 532.0));
    expect(tester.getRect(find.byKey(insideDrawer)), const Rect.fromLTRB(596.0, 30.0, 750.0, 540.0));
Ian Hickson's avatar
Ian Hickson committed
1086
  });
1087 1088


1089 1090
  group('ScaffoldGeometry', () {
    testWidgets('bottomNavigationBar', (WidgetTester tester) async {
1091 1092 1093 1094
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            bottomNavigationBar: ConstrainedBox(
1095 1096
              key: key,
              constraints: const BoxConstraints.expand(height: 80.0),
1097
              child: _GeometryListener(),
1098 1099 1100 1101 1102
            ),
      )));

      final RenderBox navigationBox = tester.renderObject(find.byKey(key));
      final RenderBox appBox = tester.renderObject(find.byType(MaterialApp));
1103
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1104 1105 1106 1107
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
        geometry.bottomNavigationBarTop,
1108
        appBox.size.height - navigationBox.size.height,
1109 1110 1111 1112
      );
    });

    testWidgets('no bottomNavigationBar', (WidgetTester tester) async {
1113 1114
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1115
              constraints: const BoxConstraints.expand(height: 80.0),
1116
              child: _GeometryListener(),
1117 1118 1119
            ),
      )));

1120
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1121 1122 1123 1124
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
        geometry.bottomNavigationBarTop,
1125
        null,
1126 1127 1128
      );
    });

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    testWidgets('Scaffold BottomNavigationBar bottom padding is not consumed by viewInsets.', (WidgetTester tester) async {
      Widget boilerplate(Widget child) {
        return Localizations(
          locale: const Locale('en', 'us'),
          delegates: const <LocalizationsDelegate<dynamic>>[
            DefaultWidgetsLocalizations.delegate,
            DefaultMaterialLocalizations.delegate,
          ],
          child: Directionality(
            textDirection: TextDirection.ltr,
            child: child,
          ),
        );
      }

      final Widget child = boilerplate(
        Scaffold(
          resizeToAvoidBottomInset: false,
          body: const Placeholder(),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.add),
                title: Text('test'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.add),
                title: Text('test'),
1157
              ),
1158
            ],
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
          ),
        ),
      );

      await tester.pumpWidget(
        MediaQuery(
          data: const MediaQueryData(padding: EdgeInsets.only(bottom: 20.0)),
          child: child,
        ),
      );
      final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
      // Consume bottom padding - as if by the keyboard opening
      await tester.pumpWidget(
        MediaQuery(
          data: const MediaQueryData(
            padding: EdgeInsets.zero,
            viewPadding: EdgeInsets.only(bottom: 20),
            viewInsets: EdgeInsets.only(bottom: 300),
          ),
          child: child,
        ),
      );
      final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
      expect(initialPoint, finalPoint);
    });

1185
    testWidgets('floatingActionButton', (WidgetTester tester) async {
1186 1187 1188 1189
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1190
              key: key,
1191
              child: _GeometryListener(),
1192
              onPressed: () { },
1193 1194 1195 1196
            ),
      )));

      final RenderBox floatingActionButtonBox = tester.renderObject(find.byKey(key));
1197
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1198 1199 1200 1201 1202 1203
      final ScaffoldGeometry geometry = listenerState.cache.value;

      final Rect fabRect = floatingActionButtonBox.localToGlobal(Offset.zero) & floatingActionButtonBox.size;

      expect(
        geometry.floatingActionButtonArea,
1204
        fabRect,
1205 1206 1207 1208
      );
    });

    testWidgets('no floatingActionButton', (WidgetTester tester) async {
1209 1210
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1211
              constraints: const BoxConstraints.expand(height: 80.0),
1212
              child: _GeometryListener(),
1213 1214 1215
            ),
      )));

1216
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1217 1218 1219 1220
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
          geometry.floatingActionButtonArea,
1221
          null,
1222 1223 1224
      );
    });

1225
    testWidgets('floatingActionButton entrance/exit animation', (WidgetTester tester) async {
1226 1227 1228
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1229
              constraints: const BoxConstraints.expand(height: 80.0),
1230
              child: _GeometryListener(),
1231 1232 1233
            ),
      )));

1234 1235 1236
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1237
              key: key,
1238
              child: _GeometryListener(),
1239
              onPressed: () { },
1240 1241 1242
            ),
      )));

1243
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1244 1245
      await tester.pump(const Duration(milliseconds: 50));

1246 1247 1248
      ScaffoldGeometry geometry = listenerState.cache.value;
      final Rect transitioningFabRect = geometry.floatingActionButtonArea;

1249 1250 1251 1252
      final double transitioningRotation = tester.widget<RotationTransition>(
        find.byType(RotationTransition),
      ).turns.value;

1253 1254 1255 1256 1257
      await tester.pump(const Duration(seconds: 3));
      geometry = listenerState.cache.value;
      final RenderBox floatingActionButtonBox = tester.renderObject(find.byKey(key));
      final Rect fabRect = floatingActionButtonBox.localToGlobal(Offset.zero) & floatingActionButtonBox.size;

1258 1259 1260 1261 1262 1263 1264 1265
      final double completedRotation = tester.widget<RotationTransition>(
        find.byType(RotationTransition),
      ).turns.value;

      expect(transitioningRotation, lessThan(1.0));

      expect(completedRotation, equals(1.0));

1266 1267
      expect(
        geometry.floatingActionButtonArea,
1268
        fabRect,
1269 1270 1271 1272
      );

      expect(
        geometry.floatingActionButtonArea.center,
1273
        transitioningFabRect.center,
1274
      );
1275 1276

      expect(
1277
        geometry.floatingActionButtonArea.width,
1278
        greaterThan(transitioningFabRect.width),
1279 1280 1281 1282
      );

      expect(
        geometry.floatingActionButtonArea.height,
1283
        greaterThan(transitioningFabRect.height),
1284 1285 1286
      );
    });

1287
    testWidgets('change notifications', (WidgetTester tester) async {
1288
      final GlobalKey key = GlobalKey();
1289
      int numNotificationsAtLastFrame = 0;
1290 1291
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1292
              constraints: const BoxConstraints.expand(height: 80.0),
1293
              child: _GeometryListener(),
1294 1295 1296
            ),
      )));

1297
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1298 1299 1300 1301

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;

1302 1303 1304
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1305
              key: key,
1306
              child: _GeometryListener(),
1307
              onPressed: () { },
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
            ),
      )));

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;

      await tester.pump(const Duration(milliseconds: 50));

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;

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

      expect(listenerState.numNotifications, greaterThan(numNotificationsAtLastFrame));
      numNotificationsAtLastFrame = listenerState.numNotifications;
    });
1324

jslavitz's avatar
jslavitz committed
1325 1326 1327 1328 1329
    testWidgets('Simultaneous drawers on either side', (WidgetTester tester) async {
      const String bodyLabel = 'I am the body';
      const String drawerLabel = 'I am the label on start side';
      const String endDrawerLabel = 'I am the label on end side';

1330
      final SemanticsTester semantics = SemanticsTester(tester);
1331
      await tester.pumpWidget(const MaterialApp(home: Scaffold(
1332 1333 1334
        body: Text(bodyLabel),
        drawer: Drawer(child: Text(drawerLabel)),
        endDrawer: Drawer(child: Text(endDrawerLabel)),
jslavitz's avatar
jslavitz committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
      )));

      expect(semantics, includesNodeWith(label: bodyLabel));
      expect(semantics, isNot(includesNodeWith(label: drawerLabel)));
      expect(semantics, isNot(includesNodeWith(label: endDrawerLabel)));

      final ScaffoldState state = tester.firstState(find.byType(Scaffold));
      state.openDrawer();
      await tester.pump();
      await tester.pump(const Duration(seconds: 1));

      expect(semantics, isNot(includesNodeWith(label: bodyLabel)));
      expect(semantics, includesNodeWith(label: drawerLabel));

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

      expect(semantics, isNot(includesNodeWith(label: bodyLabel)));
      expect(semantics, includesNodeWith(label: endDrawerLabel));

      semantics.dispose();
    });

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
    testWidgets('Drawer state query correctly', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: SafeArea(
            left: false,
            top: true,
            right: false,
            bottom: false,
            child: Scaffold(
              endDrawer: const Drawer(
                child: Text('endDrawer'),
              ),
              drawer: const Drawer(
                child: Text('drawer'),
              ),
              body: const Text('scaffold body'),
              appBar: AppBar(
                centerTitle: true,
1377
                title: const Text('Title'),
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
              ),
            ),
          ),
        ),
      );

      final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));

      final Finder drawerOpenButton = find.byType(IconButton).first;
      final Finder endDrawerOpenButton = find.byType(IconButton).last;

      await tester.tap(drawerOpenButton);
      await tester.pumpAndSettle();
      expect(true, scaffoldState.isDrawerOpen);
      await tester.tap(endDrawerOpenButton);
      await tester.pumpAndSettle();
      expect(false, scaffoldState.isDrawerOpen);

      await tester.tap(endDrawerOpenButton);
      await tester.pumpAndSettle();
      expect(true, scaffoldState.isEndDrawerOpen);
      await tester.tap(drawerOpenButton);
      await tester.pumpAndSettle();
      expect(false, scaffoldState.isEndDrawerOpen);
jslavitz's avatar
jslavitz committed
1402

1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
      scaffoldState.openDrawer();
      expect(true, scaffoldState.isDrawerOpen);
      await tester.tap(drawerOpenButton);
      await tester.pumpAndSettle();

      scaffoldState.openEndDrawer();
      expect(true, scaffoldState.isEndDrawerOpen);
    });

    testWidgets('Dual Drawer Opening', (WidgetTester tester) async {
jslavitz's avatar
jslavitz committed
1413
      await tester.pumpWidget(
1414 1415
        MaterialApp(
          home: SafeArea(
jslavitz's avatar
jslavitz committed
1416 1417 1418 1419
            left: false,
            top: true,
            right: false,
            bottom: false,
1420
            child: Scaffold(
jslavitz's avatar
jslavitz committed
1421
              endDrawer: const Drawer(
1422
                child: Text('endDrawer'),
jslavitz's avatar
jslavitz committed
1423 1424
              ),
              drawer: const Drawer(
1425
                child: Text('drawer'),
jslavitz's avatar
jslavitz committed
1426 1427
              ),
              body: const Text('scaffold body'),
1428
              appBar: AppBar(
jslavitz's avatar
jslavitz committed
1429
                centerTitle: true,
1430 1431
                title: const Text('Title'),
              ),
jslavitz's avatar
jslavitz committed
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
            ),
          ),
        ),
      );

      // Open Drawer, tap on end drawer, which closes the drawer, but does
      // not open the drawer.
      await tester.tap(find.byType(IconButton).first);
      await tester.pumpAndSettle();
      await tester.tap(find.byType(IconButton).last);
      await tester.pumpAndSettle();

      expect(find.text('endDrawer'), findsNothing);
      expect(find.text('drawer'), findsNothing);

      // Tapping the first opens the first drawer
      await tester.tap(find.byType(IconButton).first);
      await tester.pumpAndSettle();

      expect(find.text('endDrawer'), findsNothing);
      expect(find.text('drawer'), findsOneWidget);

      // Tapping on the end drawer and then on the drawer should close the
      // drawer and then reopen it.
      await tester.tap(find.byType(IconButton).last);
      await tester.pumpAndSettle();
      await tester.tap(find.byType(IconButton).first);
      await tester.pumpAndSettle();

      expect(find.text('endDrawer'), findsNothing);
      expect(find.text('drawer'), findsOneWidget);
    });
1464

1465 1466
    testWidgets('Drawer opens correctly with padding from MediaQuery (LTR)', (WidgetTester tester) async {
      const double simulatedNotchSize = 40.0;
1467 1468 1469 1470
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            drawer: const Drawer(
1471
              child: Text('Drawer'),
1472
            ),
1473
            body: const Text('Scaffold Body'),
1474 1475
            appBar: AppBar(
              centerTitle: true,
1476
              title: const Text('Title'),
1477 1478 1479 1480 1481 1482 1483 1484
            ),
          ),
        ),
      );

      ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);

1485
      await tester.dragFrom(const Offset(simulatedNotchSize + 15.0, 100), const Offset(300, 0));
1486 1487 1488 1489 1490 1491 1492
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, false);

      await tester.pumpWidget(
        MaterialApp(
          home: MediaQuery(
            data: const MediaQueryData(
1493
              padding: EdgeInsets.fromLTRB(simulatedNotchSize, 0, 0, 0),
1494 1495 1496
            ),
            child: Scaffold(
              drawer: const Drawer(
1497
                child: Text('Drawer'),
1498
              ),
1499
              body: const Text('Scaffold Body'),
1500 1501
              appBar: AppBar(
                centerTitle: true,
1502
                title: const Text('Title'),
1503 1504 1505 1506 1507 1508 1509 1510
              ),
            ),
          ),
        ),
      );
      scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);

1511 1512 1513 1514
      await tester.dragFrom(
        const Offset(simulatedNotchSize + 15.0, 100),
        const Offset(300, 0),
      );
1515 1516 1517 1518
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, true);
    });

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    testWidgets('Drawer opens correctly with padding from MediaQuery (RTL)', (WidgetTester tester) async {
      const double simulatedNotchSize = 40.0;
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            drawer: const Drawer(
              child: Text('Drawer'),
            ),
            body: const Text('Scaffold Body'),
            appBar: AppBar(
              centerTitle: true,
              title: const Text('Title'),
            ),
          ),
        ),
      );

      final double scaffoldWidth = tester.renderObject<RenderBox>(
        find.byType(Scaffold),
      ).size.width;
      ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);

      await tester.dragFrom(
        Offset(scaffoldWidth - simulatedNotchSize - 15.0, 100),
        const Offset(-300, 0),
      );
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, false);

1549 1550 1551 1552
      await tester.pumpWidget(
        MaterialApp(
          home: MediaQuery(
            data: const MediaQueryData(
1553
              padding: EdgeInsets.fromLTRB(0, 0, simulatedNotchSize, 0),
1554 1555 1556 1557 1558
            ),
            child: Directionality(
              textDirection: TextDirection.rtl,
              child: Scaffold(
                drawer: const Drawer(
1559
                  child: Text('Drawer'),
1560
                ),
1561
                body: const Text('Scaffold body'),
1562 1563
                appBar: AppBar(
                  centerTitle: true,
1564
                  title: const Text('Title'),
1565 1566 1567 1568 1569 1570
                ),
              ),
            ),
          ),
        ),
      );
1571 1572
      scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);
1573

1574 1575 1576 1577 1578 1579 1580 1581
      await tester.dragFrom(
        Offset(scaffoldWidth - simulatedNotchSize - 15.0, 100),
        const Offset(-300, 0),
      );
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, true);
    });
  });
1582

1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  testWidgets('Drawer opens correctly with custom edgeDragWidth', (WidgetTester tester) async {
    // The default edge drag width is 20.0.
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          drawer: const Drawer(
            child: Text('Drawer'),
          ),
          body: const Text('Scaffold body'),
          appBar: AppBar(
            centerTitle: true,
            title: const Text('Title'),
1595 1596
          ),
        ),
1597 1598 1599 1600
      ),
    );
    ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isDrawerOpen, false);
1601

1602 1603 1604
    await tester.dragFrom(const Offset(35, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isDrawerOpen, false);
1605

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          drawer: const Drawer(
            child: Text('Drawer'),
          ),
          drawerEdgeDragWidth: 40.0,
          body: const Text('Scaffold Body'),
          appBar: AppBar(
            centerTitle: true,
            title: const Text('Title'),
1617 1618
          ),
        ),
1619 1620 1621 1622
      ),
    );
    scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isDrawerOpen, false);
1623

1624 1625 1626 1627
    await tester.dragFrom(const Offset(35, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isDrawerOpen, true);
  });
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678

  testWidgets('Nested scaffold body insets', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/20295
    final Key bodyKey = UniqueKey();

    Widget buildFrame(bool innerResizeToAvoidBottomInset, bool outerResizeToAvoidBottomInset) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
          child: Builder(
            builder: (BuildContext context) {
              return Scaffold(
                resizeToAvoidBottomInset: outerResizeToAvoidBottomInset,
                body: Builder(
                  builder: (BuildContext context) {
                    return Scaffold(
                      resizeToAvoidBottomInset: innerResizeToAvoidBottomInset,
                      body: Container(key: bodyKey),
                    );
                  },
                ),
              );
            },
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(true, true));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(false, true));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(true, false));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    // This is the only case where the body is not bottom inset.
    await tester.pumpWidget(buildFrame(false, false));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 600.0));

    await tester.pumpWidget(buildFrame(null, null));  // resizeToAvoidBottomInset default  is true
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(null, false));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));

    await tester.pumpWidget(buildFrame(false, null));
    expect(tester.getSize(find.byKey(bodyKey)), const Size(800.0, 500.0));
  });
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

  group('FlutterError control test', () {
    testWidgets('showBottomSheet() while Scaffold has bottom sheet',
      (WidgetTester tester) async {
        final GlobalKey<ScaffoldState> key = GlobalKey<ScaffoldState>();
        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              key: key,
              body: Center(
                child: Container(),
              ),
              bottomSheet: Container(
                child: const Text('Bottom sheet'),
              ),
            ),
          ),
        );
        FlutterError error;
        try {
          key.currentState.showBottomSheet<void>((BuildContext context) {
            final ThemeData themeData = Theme.of(context);
            return Container(
              decoration: BoxDecoration(
                border: Border(top: BorderSide(color: themeData.disabledColor))
              ),
              child: Padding(
                padding: const EdgeInsets.all(32.0),
                child: Text('This is a Material persistent bottom sheet. Drag downwards to dismiss it.',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: themeData.accentColor,
                    fontSize: 24.0,
                  ),
                ),
              ),
            );
          },);
        } on FlutterError catch (e) {
          error = e;
        } finally {
          expect(error, isNotNull);
          expect(error.toStringDeep(), equalsIgnoringHashCodes(
            'FlutterError\n'
            '   Scaffold.bottomSheet cannot be specified while a bottom\n'
            '   sheetdisplayed with showBottomSheet() is still visible.\n'
            '   Rebuild the Scaffold with a null bottomSheet before calling\n'
            '   showBottomSheet().\n',
          ));
        }
      }
    );

    testWidgets('didUpdate bottomSheet while a previous bottom sheet is still displayed',
        (WidgetTester tester) async {
        final GlobalKey<ScaffoldState> key = GlobalKey<ScaffoldState>();
        const Key buttonKey = Key('button');
        final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
        FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
        int state = 0;
        await tester.pumpWidget(
          MaterialApp(
            home: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return Scaffold(
                  key: key,
                  body: Container(),
                  floatingActionButton: FloatingActionButton(
                    key: buttonKey,
                    onPressed: () {
                      state += 1;
                      setState(() {});
                    }
                  ),
                  bottomSheet: state == 0 ? null : const SizedBox(),
                );
              }
            ),
          ),
        );
        key.currentState.showBottomSheet<void>((_) => Container());
        await tester.tap(find.byKey(buttonKey));
        await tester.pump();
        expect(errors, isNotEmpty);
        expect(errors.first.exception, isFlutterError);
1764
        final FlutterError error = errors.first.exception as FlutterError;
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
        expect(error.diagnostics.length, 2);
        expect(error.diagnostics.last.level, DiagnosticLevel.hint);
        expect(
          error.diagnostics.last.toStringDeep(),
          'Use the PersistentBottomSheetController returned by\n'
          'showBottomSheet() to close the old bottom sheet before creating a\n'
          'Scaffold with a (non null) bottomSheet.\n',
        );
        expect(
          error.toStringDeep(),
          'FlutterError\n'
          '   Scaffold.bottomSheet cannot be specified while a bottom sheet\n'
          '   displayed with showBottomSheet() is still visible.\n'
          '   Use the PersistentBottomSheetController returned by\n'
          '   showBottomSheet() to close the old bottom sheet before creating a\n'
          '   Scaffold with a (non null) bottomSheet.\n'
        );
    });

    testWidgets('Call to Scaffold.of() without context', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: Builder(
            builder: (BuildContext context) {
              Scaffold.of(context).showBottomSheet<void>((BuildContext context) {
                return Container();
              });
              return Container();
            },
          ),
        ),
      );
      final dynamic exception = tester.takeException();
      expect(exception, isFlutterError);
1799
      final FlutterError error = exception as FlutterError;
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
      expect(error.diagnostics.length, 5);
      expect(error.diagnostics[2].level, DiagnosticLevel.hint);
      expect(
        error.diagnostics[2].toStringDeep(),
        equalsIgnoringHashCodes(
          'There are several ways to avoid this problem. The simplest is to\n'
          'use a Builder to get a context that is "under" the Scaffold. For\n'
          'an example of this, please see the documentation for\n'
          'Scaffold.of():\n'
          '  https://api.flutter.dev/flutter/material/Scaffold/of.html\n',
        ),
      );
      expect(error.diagnostics[3].level, DiagnosticLevel.hint);
      expect(
        error.diagnostics[3].toStringDeep(),
        equalsIgnoringHashCodes(
          'A more efficient solution is to split your build function into\n'
          'several widgets. This introduces a new context from which you can\n'
          'obtain the Scaffold. In this solution, you would have an outer\n'
          'widget that creates the Scaffold populated by instances of your\n'
          'new inner widgets, and then in these inner widgets you would use\n'
          'Scaffold.of().\n'
          'A less elegant but more expedient solution is assign a GlobalKey\n'
          'to the Scaffold, then use the key.currentState property to obtain\n'
          'the ScaffoldState rather than using the Scaffold.of() function.\n',
        ),
      );
      expect(error.diagnostics[4], isInstanceOf<DiagnosticsProperty<Element>>());
      expect(error.toStringDeep(),
        'FlutterError\n'
        '   Scaffold.of() called with a context that does not contain a\n'
        '   Scaffold.\n'
        '   No Scaffold ancestor could be found starting from the context\n'
        '   that was passed to Scaffold.of(). This usually happens when the\n'
        '   context provided is from the same StatefulWidget as that whose\n'
        '   build function actually creates the Scaffold widget being sought.\n'
        '   There are several ways to avoid this problem. The simplest is to\n'
        '   use a Builder to get a context that is "under" the Scaffold. For\n'
        '   an example of this, please see the documentation for\n'
        '   Scaffold.of():\n'
        '     https://api.flutter.dev/flutter/material/Scaffold/of.html\n'
        '   A more efficient solution is to split your build function into\n'
        '   several widgets. This introduces a new context from which you can\n'
        '   obtain the Scaffold. In this solution, you would have an outer\n'
        '   widget that creates the Scaffold populated by instances of your\n'
        '   new inner widgets, and then in these inner widgets you would use\n'
        '   Scaffold.of().\n'
        '   A less elegant but more expedient solution is assign a GlobalKey\n'
        '   to the Scaffold, then use the key.currentState property to obtain\n'
        '   the ScaffoldState rather than using the Scaffold.of() function.\n'
        '   The context used was:\n'
        '     Builder\n'
      );
      await tester.pumpAndSettle();
    });

    testWidgets('Call to Scaffold.geometryOf() without context', (WidgetTester tester) async {
      ValueListenable<ScaffoldGeometry> geometry;
      await tester.pumpWidget(
        MaterialApp(
          home: Builder(
            builder: (BuildContext context) {
              geometry = Scaffold.geometryOf(context);
              return Container();
            },
          ),
        ),
      );
      final dynamic exception = tester.takeException();
      expect(exception, isFlutterError);
      expect(geometry, isNull);
1871
      final FlutterError error = exception as FlutterError;
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
      expect(error.diagnostics.length, 5);
      expect(error.diagnostics[2].level, DiagnosticLevel.hint);
      expect(
        error.diagnostics[2].toStringDeep(),
        equalsIgnoringHashCodes(
          'There are several ways to avoid this problem. The simplest is to\n'
          'use a Builder to get a context that is "under" the Scaffold. For\n'
          'an example of this, please see the documentation for\n'
          'Scaffold.of():\n'
          '  https://api.flutter.dev/flutter/material/Scaffold/of.html\n',
        ),
      );
      expect(error.diagnostics[3].level, DiagnosticLevel.hint);
      expect(
        error.diagnostics[3].toStringDeep(),
        equalsIgnoringHashCodes(
          'A more efficient solution is to split your build function into\n'
          'several widgets. This introduces a new context from which you can\n'
          'obtain the Scaffold. In this solution, you would have an outer\n'
          'widget that creates the Scaffold populated by instances of your\n'
          'new inner widgets, and then in these inner widgets you would use\n'
          'Scaffold.geometryOf().\n',
        ),
      );
      expect(error.diagnostics[4], isInstanceOf<DiagnosticsProperty<Element>>());
      expect(error.toStringDeep(),
        'FlutterError\n'
        '   Scaffold.geometryOf() called with a context that does not contain\n'
        '   a Scaffold.\n'
        '   This usually happens when the context provided is from the same\n'
        '   StatefulWidget as that whose build function actually creates the\n'
        '   Scaffold widget being sought.\n'
        '   There are several ways to avoid this problem. The simplest is to\n'
        '   use a Builder to get a context that is "under" the Scaffold. For\n'
        '   an example of this, please see the documentation for\n'
        '   Scaffold.of():\n'
        '     https://api.flutter.dev/flutter/material/Scaffold/of.html\n'
        '   A more efficient solution is to split your build function into\n'
        '   several widgets. This introduces a new context from which you can\n'
        '   obtain the Scaffold. In this solution, you would have an outer\n'
        '   widget that creates the Scaffold populated by instances of your\n'
        '   new inner widgets, and then in these inner widgets you would use\n'
        '   Scaffold.geometryOf().\n'
        '   The context used was:\n'
        '     Builder\n'
      );
      await tester.pumpAndSettle();
    });
  });
1921 1922
}

1923
class _GeometryListener extends StatefulWidget {
1924
  @override
1925
  _GeometryListenerState createState() => _GeometryListenerState();
1926 1927
}

1928
class _GeometryListenerState extends State<_GeometryListener> {
1929 1930
  @override
  Widget build(BuildContext context) {
1931
    return CustomPaint(
1932 1933 1934 1935 1936 1937
      painter: cache
    );
  }

  int numNotifications = 0;
  ValueListenable<ScaffoldGeometry> geometryListenable;
1938
  _GeometryCachePainter cache;
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final ValueListenable<ScaffoldGeometry> newListenable = Scaffold.geometryOf(context);
    if (geometryListenable == newListenable)
      return;

    if (geometryListenable != null)
      geometryListenable.removeListener(onGeometryChanged);
1949

1950 1951
    geometryListenable = newListenable;
    geometryListenable.addListener(onGeometryChanged);
1952
    cache = _GeometryCachePainter(geometryListenable);
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
  }

  void onGeometryChanged() {
    numNotifications += 1;
  }
}

// The Scaffold.geometryOf() value is only available at paint time.
// To fetch it for the tests we implement this CustomPainter that just
// caches the ScaffoldGeometry value in its paint method.
1963 1964
class _GeometryCachePainter extends CustomPainter {
  _GeometryCachePainter(this.geometryListenable) : super(repaint: geometryListenable);
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974

  final ValueListenable<ScaffoldGeometry> geometryListenable;

  ScaffoldGeometry value;
  @override
  void paint(Canvas canvas, Size size) {
    value = geometryListenable.value;
  }

  @override
1975
  bool shouldRepaint(_GeometryCachePainter oldDelegate) {
1976 1977
    return true;
  }
1978
}
1979

1980 1981 1982
class _CustomPageRoute<T> extends PageRoute<T> {
  _CustomPageRoute({
    @required this.builder,
1983 1984 1985
    RouteSettings settings = const RouteSettings(),
    this.maintainState = true,
    bool fullscreenDialog = false,
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
  }) : assert(builder != null),
       super(settings: settings, fullscreenDialog: fullscreenDialog);

  final WidgetBuilder builder;

  @override
  Duration get transitionDuration => const Duration(milliseconds: 300);

  @override
  Color get barrierColor => null;

  @override
  String get barrierLabel => null;

  @override
  final bool maintainState;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return builder(context);
  }

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return child;
  }
}