scaffold_test.dart 85.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 15 16 17 18 19
  testWidgets('Scaffold drawer callback test', (WidgetTester tester) async {
    bool isDrawerOpen = false;
    bool isEndDrawerOpen = false;

    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
20 21 22 23 24 25 26 27 28 29 30 31 32 33
        drawer: Container(
          color: Colors.blue,
        ),
        onDrawerChanged: (bool isOpen) {
          isDrawerOpen = isOpen;
        },
        endDrawer: Container(
          color: Colors.green,
        ),
        onEndDrawerChanged: (bool isOpen) {
          isEndDrawerOpen = isOpen;
        },
        body: Container(),
      ),
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    ));

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

    scaffoldState.openDrawer();
    await tester.pumpAndSettle();
    expect(true, isDrawerOpen);
    scaffoldState.openEndDrawer();
    await tester.pumpAndSettle();
    expect(false, isDrawerOpen);

    scaffoldState.openEndDrawer();
    await tester.pumpAndSettle();
    expect(true, isEndDrawerOpen);
    scaffoldState.openDrawer();
    await tester.pumpAndSettle();
    expect(false, isEndDrawerOpen);
  });

53
  testWidgets('Scaffold control test', (WidgetTester tester) async {
54
    final Key bodyKey = UniqueKey();
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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(
69 70
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
71
      ),
72
    ));
73
    expect(tester.takeException(), isFlutterError);
74

75 76 77 78
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Title')),
        body: Container(key: bodyKey),
79 80
      ),
    ));
81
    RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
82
    expect(bodyBox.size, equals(const Size(800.0, 544.0)));
83

84
    await tester.pumpWidget(boilerplate(MediaQuery(
85
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
86 87 88
        child: Scaffold(
          appBar: AppBar(title: const Text('Title')),
          body: Container(key: bodyKey),
89 90
        ),
      ),
91
    ));
92

93
    bodyBox = tester.renderObject(find.byKey(bodyKey));
94
    expect(bodyBox.size, equals(const Size(800.0, 444.0)));
95

96 97 98 99 100
    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),
101 102 103 104 105 106
        resizeToAvoidBottomInset: false,
      ),
    )));

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

109
  testWidgets('Scaffold large bottom padding test', (WidgetTester tester) async {
110
    final Key bodyKey = UniqueKey();
111 112 113 114 115 116 117 118 119 120 121

    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,
122
        ),
123 124 125 126 127 128
      );
    }

    await tester.pumpWidget(boilerplate(MediaQuery(
      data: const MediaQueryData(
        viewInsets: EdgeInsets.only(bottom: 700.0),
129
      ),
130 131
      child: Scaffold(
        body: Container(key: bodyKey),
132 133
      ),
    )));
134

135
    final RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
136 137
    expect(bodyBox.size, equals(const Size(800.0, 0.0)));

138
    await tester.pumpWidget(boilerplate(MediaQuery(
139
        data: const MediaQueryData(
140
          viewInsets: EdgeInsets.only(bottom: 500.0),
141
        ),
142 143
        child: Scaffold(
          body: Container(key: bodyKey),
144
        ),
145 146 147 148 149
      ),
    ));

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

150
    await tester.pumpWidget(boilerplate(MediaQuery(
151
        data: const MediaQueryData(
152
          viewInsets: EdgeInsets.only(bottom: 580.0),
153
        ),
154 155
        child: Scaffold(
          appBar: AppBar(
156 157
            title: const Text('Title'),
          ),
158
          body: Container(key: bodyKey),
159 160 161 162 163 164 165
        ),
      ),
    ));

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

166
  testWidgets('Floating action entrance/exit animation', (WidgetTester tester) async {
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, 0);

177
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
178 179
      floatingActionButton: FloatingActionButton(
        key: Key('two'),
180
        onPressed: null,
181
        child: Text('2'),
182
      ),
183
    )));
184 185

    expect(tester.binding.transientCallbackCount, greaterThan(0));
186
    await tester.pumpWidget(Container());
187
    expect(tester.binding.transientCallbackCount, 0);
188

189
    await tester.pumpWidget(const MaterialApp(home: Scaffold()));
190

191 192
    expect(tester.binding.transientCallbackCount, 0);

193
    await tester.pumpWidget(const MaterialApp(home: Scaffold(
194 195
      floatingActionButton: FloatingActionButton(
        key: Key('one'),
196
        onPressed: null,
197
        child: Text('1'),
198
      ),
199
    )));
200 201 202

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

204
  testWidgets('Floating action button directionality', (WidgetTester tester) async {
205
    Widget build(TextDirection textDirection) {
206
      return Directionality(
207
        textDirection: textDirection,
208
        child: const MediaQuery(
209 210
          data: MediaQueryData(
            viewInsets: EdgeInsets.only(bottom: 200.0),
211
          ),
212 213
          child: Scaffold(
            floatingActionButton: FloatingActionButton(
214
              onPressed: null,
215
              child: Text('1'),
216 217 218 219 220 221 222 223 224 225 226
            ),
          ),
        ),
      );
    }

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

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

    await tester.pumpWidget(build(TextDirection.rtl));
227
    expect(tester.binding.transientCallbackCount, 0);
228 229 230 231

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

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  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,
248
      ),
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    );
    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);
  });

266
  testWidgets('Drawer scrolling', (WidgetTester tester) async {
267
    final Key drawerKey = UniqueKey();
268 269
    const double appBarHeight = 256.0;

270
    final ScrollController scrollOffset = ScrollController();
271

272
    await tester.pumpWidget(
273 274 275
      MaterialApp(
        home: Scaffold(
          drawer: Drawer(
276
            key: drawerKey,
277
            child: ListView(
278
              dragStartBehavior: DragStartBehavior.down,
279
              controller: scrollOffset,
280
              children: List<Widget>.generate(10,
281 282 283
                (int index) => SizedBox(height: 100.0, child: Text('D$index')),
              ),
            ),
284
          ),
285
          body: CustomScrollView(
286
            slivers: <Widget>[
287
              const SliverAppBar(
288 289
                pinned: true,
                expandedHeight: appBarHeight,
290 291
                title: Text('Title'),
                flexibleSpace: FlexibleSpaceBar(title: Text('Title')),
292
              ),
293
              SliverPadding(
294
                padding: const EdgeInsets.only(top: appBarHeight),
295 296 297
                sliver: SliverList(
                  delegate: SliverChildListDelegate(List<Widget>.generate(
                    10, (int index) => SizedBox(height: 100.0, child: Text('B$index')),
298 299 300 301
                  )),
                ),
              ),
            ],
302
          ),
303
        ),
304
      ),
305 306
    );

307
    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
308 309 310 311 312
    state.openDrawer();

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

313
    expect(scrollOffset.offset, 0.0);
314 315

    const double scrollDelta = 80.0;
316
    await tester.drag(find.byKey(drawerKey), const Offset(0.0, -scrollDelta));
317 318
    await tester.pump();

319
    expect(scrollOffset.offset, scrollDelta);
320

321
    final RenderBox renderBox = tester.renderObject(find.byType(AppBar));
322 323
    expect(renderBox.size.height, equals(appBarHeight));
  });
324

325
  Widget _buildStatusBarTestApp(TargetPlatform? platform) {
326 327 328
    return MaterialApp(
      theme: ThemeData(platform: platform),
      home: MediaQuery(
329
        data: const MediaQueryData(padding: EdgeInsets.only(top: 25.0)), // status bar
330 331
        child: Scaffold(
          body: CustomScrollView(
332 333
            primary: true,
            slivers: <Widget>[
334
              const SliverAppBar(
335
                title: Text('Title'),
336
              ),
337 338 339
              SliverList(
                delegate: SliverChildListDelegate(List<Widget>.generate(
                  20, (int index) => SizedBox(height: 100.0, child: Text('$index')),
340 341 342 343 344 345
                )),
              ),
            ],
          ),
        ),
      ),
346
    );
347
  }
348

Dan Field's avatar
Dan Field committed
349 350
  testWidgets('Tapping the status bar scrolls to top', (WidgetTester tester) async {
    await tester.pumpWidget(_buildStatusBarTestApp(debugDefaultTargetPlatformOverride));
Adam Barth's avatar
Adam Barth committed
351
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
352 353
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
354
    await tester.tapAt(const Offset(100.0, 10.0));
355
    await tester.pumpAndSettle();
356
    expect(scrollable.position.pixels, equals(0.0));
Dan Field's avatar
Dan Field committed
357
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
358

Dan Field's avatar
Dan Field committed
359
  testWidgets('Tapping the status bar does not scroll to top', (WidgetTester tester) async {
360
    await tester.pumpWidget(_buildStatusBarTestApp(TargetPlatform.android));
Adam Barth's avatar
Adam Barth committed
361
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
362 363
    scrollable.position.jumpTo(500.0);
    expect(scrollable.position.pixels, equals(500.0));
364
    await tester.tapAt(const Offset(100.0, 10.0));
365
    await tester.pump();
366
    await tester.pump(const Duration(seconds: 1));
367
    expect(scrollable.position.pixels, equals(500.0));
Dan Field's avatar
Dan Field committed
368
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android }));
369 370

  testWidgets('Bottom sheet cannot overlap app bar', (WidgetTester tester) async {
371
    final Key sheetKey = UniqueKey();
372 373

    await tester.pumpWidget(
374 375 376 377
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
378
            title: const Text('Title'),
379
          ),
380
          body: Builder(
381
            builder: (BuildContext context) {
382
              return GestureDetector(
383
                onTap: () {
384
                  Scaffold.of(context).showBottomSheet<void>((BuildContext context) {
385
                    return Container(
386
                      key: sheetKey,
387
                      color: Colors.blue[500],
388 389 390
                    );
                  });
                },
391
                child: const Text('X'),
392
              );
393 394 395 396
            },
          ),
        ),
      ),
397 398 399 400 401
    );
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(seconds: 1));

402 403
    final RenderBox appBarBox = tester.renderObject(find.byType(AppBar));
    final RenderBox sheetBox = tester.renderObject(find.byKey(sheetKey));
404

405 406
    final Offset appBarBottomRight = appBarBox.localToGlobal(appBarBox.size.bottomRight(Offset.zero));
    final Offset sheetTopRight = sheetBox.localToGlobal(sheetBox.size.topRight(Offset.zero));
407 408 409

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

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  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,
427
      ),
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    );
    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);
  });

445 446 447
  testWidgets('Persistent bottom buttons are persistent', (WidgetTester tester) async {
    bool didPressButton = false;
    await tester.pumpWidget(
448 449 450 451
      MaterialApp(
        home: Scaffold(
          body: SingleChildScrollView(
            child: Container(
452
              color: Colors.amber[500],
453
              height: 5000.0,
454
              child: const Text('body'),
455 456 457
            ),
          ),
          persistentFooterButtons: <Widget>[
458
            TextButton(
459 460 461
              onPressed: () {
                didPressButton = true;
              },
462
              child: const Text('X'),
463
            ),
464 465 466 467 468
          ],
        ),
      ),
    );

469
    await tester.drag(find.byType(SingleChildScrollView), const Offset(0.0, -1000.0));
470 471 472 473 474
    expect(didPressButton, isFalse);
    await tester.tap(find.text('X'));
    expect(didPressButton, isTrue);
  });

475 476
  testWidgets('Persistent bottom buttons apply media padding', (WidgetTester tester) async {
    await tester.pumpWidget(
477
      Directionality(
478
        textDirection: TextDirection.ltr,
479
        child: MediaQuery(
480
          data: const MediaQueryData(
481
            padding: EdgeInsets.fromLTRB(10.0, 20.0, 30.0, 40.0),
482
          ),
483 484 485
          child: Scaffold(
            body: SingleChildScrollView(
              child: Container(
486 487 488 489 490
                color: Colors.amber[500],
                height: 5000.0,
                child: const Text('body'),
              ),
            ),
491
            persistentFooterButtons: const <Widget>[Placeholder()],
492 493 494 495
          ),
        ),
      ),
    );
496 497 498 499

    final Finder buttonsBar = find.ancestor(of: find.byType(OverflowBar), matching: find.byType(Padding)).first;
    expect(tester.getBottomLeft(buttonsBar), const Offset(10.0, 560.0));
    expect(tester.getBottomRight(buttonsBar), const Offset(770.0, 560.0));
500 501
  });

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
  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,
518
      ),
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    );
    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);
  });

536
  group('back arrow', () {
Dan Field's avatar
Dan Field committed
537
    Future<void> expectBackIcon(WidgetTester tester, IconData expectedIcon) async {
538
      final GlobalKey rootKey = GlobalKey();
539
      final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
540 541 542
        '/': (_) => Container(key: rootKey, child: const Text('Home')),
        '/scaffold': (_) => Scaffold(
            appBar: AppBar(),
543
            body: const Text('Scaffold'),
544
        ),
545
      };
Dan Field's avatar
Dan Field committed
546
      await tester.pumpWidget(MaterialApp(routes: routes));
547

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

552
      final Icon icon = tester.widget(find.byType(Icon));
553 554 555
      expect(icon.icon, expectedIcon);
    }

Dan Field's avatar
Dan Field committed
556 557 558
    testWidgets('Back arrow uses correct default', (WidgetTester tester) async {
      await expectBackIcon(tester, Icons.arrow_back);
    }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.fuchsia }));
559

Dan Field's avatar
Dan Field committed
560 561 562
    testWidgets('Back arrow uses correct default', (WidgetTester tester) async {
      await expectBackIcon(tester, Icons.arrow_back_ios);
    }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
563
  });
564

565
  group('close button', () {
566
    Future<void> expectCloseIcon(WidgetTester tester, PageRoute<void> Function() routeBuilder, String type) async {
Dan Field's avatar
Dan Field committed
567
      const IconData expectedIcon = Icons.close;
568
      await tester.pumpWidget(
569 570
        MaterialApp(
          home: Scaffold(appBar: AppBar(), body: const Text('Page 1')),
571
        ),
572 573
      );

574
      tester.state<NavigatorState>(find.byType(Navigator)).push(routeBuilder());
575 576 577 578 579

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

      final Icon icon = tester.widget(find.byType(Icon));
Dan Field's avatar
Dan Field committed
580 581
      expect(icon.icon, expectedIcon, reason: "didn't find close icon for $type");
      expect(find.byType(CloseButton), findsOneWidget, reason: "didn't find close button for $type");
582 583 584
    }

    PageRoute<void> materialRouteBuilder() {
585
      return MaterialPageRoute<void>(
586
        builder: (BuildContext context) {
587
          return Scaffold(appBar: AppBar(), body: const Text('Page 2'));
588 589 590 591 592
        },
        fullscreenDialog: true,
      );
    }

593 594 595 596 597 598 599 600 601
    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,
      );
    }

602
    PageRoute<void> customPageRouteBuilder() {
603
      return _CustomPageRoute<void>(
604
        builder: (BuildContext context) {
605
          return Scaffold(appBar: AppBar(), body: const Text('Page 2'));
606 607 608
        },
        fullscreenDialog: true,
      );
609 610
    }

Dan Field's avatar
Dan Field committed
611 612 613
    testWidgets('Close button shows correctly', (WidgetTester tester) async {
      await expectCloseIcon(tester, materialRouteBuilder, 'materialRouteBuilder');
    }, variant: TargetPlatformVariant.all());
614

Dan Field's avatar
Dan Field committed
615 616 617
    testWidgets('Close button shows correctly with PageRouteBuilder', (WidgetTester tester) async {
      await expectCloseIcon(tester, pageRouteBuilder, 'pageRouteBuilder');
    }, variant: TargetPlatformVariant.all());
618

Dan Field's avatar
Dan Field committed
619 620 621
    testWidgets('Close button shows correctly with custom page route', (WidgetTester tester) async {
      await expectCloseIcon(tester, customPageRouteBuilder, 'customPageRouteBuilder');
    }, variant: TargetPlatformVariant.all());
622 623
  });

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

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

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

    testWidgets('body size with button', (WidgetTester tester) async {
680 681
      final Key testKey = UniqueKey();
      await tester.pumpWidget(Directionality(
682
        textDirection: TextDirection.ltr,
683
        child: MediaQuery(
684
          data: const MediaQueryData(),
685
          child: Scaffold(
686
            body: TextButton(
687 688 689 690 691 692
              key: testKey,
              onPressed: () { },
              child: const Text(''),
            ),
          ),
        ),
693
      ));
694
      expect(tester.element(find.byKey(testKey)).size, const Size(64.0, 48.0));
695
      expect(tester.renderObject<RenderBox>(find.byKey(testKey)).localToGlobal(Offset.zero), Offset.zero);
696
    });
697 698 699

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

702
      Widget buildFrame({ required bool extendBody, bool? resizeToAvoidBottomInset, double viewInsetBottom = 0.0 }) {
703 704 705 706 707 708 709 710 711 712 713
        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) {
714
                  mediaQueryBottom = MediaQuery.of(context).padding.bottom;
715 716 717 718
                  return Container(key: bodyKey);
                },
              ),
              bottomNavigationBar: const BottomAppBar(
719
                child: SizedBox(height: 48.0),
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
              ),
            ),
          ),
        );
      }

      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);
753
    });
754 755 756 757 758 759 760

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

      const double appBarHeight = 100;
      const double windowPaddingTop = 24;
761 762
      late bool fixedHeightAppBar;
      late double mediaQueryTop;
763

764
      Widget buildFrame({ required bool extendBodyBehindAppBar, required bool hasAppBar }) {
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
        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) {
787
                      mediaQueryTop = MediaQuery.of(context).padding.top;
788
                      return Container(key: bodyKey);
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
                  ),
                );
              },
            ),
          ),
        );
      }

      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);
    });
850
  });
851 852 853 854 855 856 857 858

  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';

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

868 869 870 871 872
    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)));
873 874 875 876 877 878

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

879 880 881 882 883
    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));
884 885 886

    semantics.dispose();
  });
Ian Hickson's avatar
Ian Hickson committed
887 888

  testWidgets('Scaffold and extreme window padding', (WidgetTester tester) async {
889 890 891 892 893 894 895 896 897 898 899 900
    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
901
    await tester.pumpWidget(
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
      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
917
              ),
918
              viewInsets: EdgeInsets.only(bottom: 200.0),
Ian Hickson's avatar
Ian Hickson committed
919
            ),
920
            child: Scaffold(
921
              drawerDragStartBehavior: DragStartBehavior.down,
922 923 924 925 926 927 928 929
              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
930
              ),
931 932 933 934 935
              body: Container(
                key: body,
                child: SafeArea(
                  child: Placeholder(key: insideBody),
                ),
Ian Hickson's avatar
Ian Hickson committed
936
              ),
937 938 939 940
              floatingActionButton: SizedBox(
                key: floatingActionButton,
                width: 77.0,
                height: 77.0,
941
                child: SafeArea(
942
                  child: Placeholder(key: insideFloatingActionButton),
Ian Hickson's avatar
Ian Hickson committed
943 944
                ),
              ),
945 946 947 948 949 950 951 952 953 954
              persistentFooterButtons: <Widget>[
                SizedBox(
                  key: persistentFooterButton,
                  width: 100.0,
                  height: 90.0,
                  child: SafeArea(
                    child: Placeholder(key: insidePersistentFooterButton),
                  ),
                ),
              ],
955
              drawer: SizedBox(
956 957 958 959 960
                key: drawer,
                width: 204.0,
                child: SafeArea(
                  child: Placeholder(key: insideDrawer),
                ),
Ian Hickson's avatar
Ian Hickson committed
961
              ),
962 963 964 965 966 967
              bottomNavigationBar: SizedBox(
                key: bottomNavigationBar,
                height: 85.0,
                child: SafeArea(
                  child: Placeholder(key: insideBottomNavigationBar),
                ),
Ian Hickson's avatar
Ian Hickson committed
968 969 970 971 972 973 974 975 976 977 978
              ),
            ),
          ),
        ),
      ),
    );
    // 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
979 980 981 982 983 984 985 986 987 988 989 990
    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));
991 992 993
  });

  testWidgets('Scaffold and extreme window padding - persistent footer buttons only', (WidgetTester tester) async {
994 995 996 997 998 999 1000 1001 1002 1003
    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();
1004
    await tester.pumpWidget(
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
      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,
1020
              ),
1021
              viewInsets: EdgeInsets.only(bottom: 200.0),
1022
            ),
1023 1024 1025 1026 1027 1028 1029 1030 1031
            child: Scaffold(
              appBar: PreferredSize(
                preferredSize: const Size(11.0, 13.0),
                child: Container(
                  key: appBar,
                  child: SafeArea(
                    child: Placeholder(key: insideAppBar),
                  ),
                ),
1032
              ),
1033 1034 1035 1036 1037
              body: Container(
                key: body,
                child: SafeArea(
                  child: Placeholder(key: insideBody),
                ),
1038
              ),
1039 1040 1041 1042
              floatingActionButton: SizedBox(
                key: floatingActionButton,
                width: 77.0,
                height: 77.0,
1043
                child: SafeArea(
1044
                  child: Placeholder(key: insideFloatingActionButton),
1045 1046
                ),
              ),
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
              persistentFooterButtons: <Widget>[
                SizedBox(
                  key: persistentFooterButton,
                  width: 100.0,
                  height: 90.0,
                  child: SafeArea(
                    child: Placeholder(key: insidePersistentFooterButton),
                  ),
                ),
              ],
1057
              drawer: SizedBox(
1058 1059 1060 1061 1062
                key: drawer,
                width: 204.0,
                child: SafeArea(
                  child: Placeholder(key: insideDrawer),
                ),
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
              ),
            ),
          ),
        ),
      ),
    );
    // 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
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    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
1084
  });
1085 1086


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

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

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

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

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

      expect(
        geometry.bottomNavigationBarTop,
1123
        null,
1124 1125 1126
      );
    });

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    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(),
1146 1147 1148 1149 1150 1151 1152 1153
          bottomNavigationBar: Navigator(
            onGenerateRoute: (RouteSettings settings) {
              return MaterialPageRoute<void>(
                builder: (BuildContext context) {
                  return BottomNavigationBar(
                    items: const <BottomNavigationBarItem>[
                      BottomNavigationBarItem(
                        icon: Icon(Icons.add),
1154
                        label: 'test',
1155 1156 1157
                      ),
                      BottomNavigationBarItem(
                        icon: Icon(Icons.add),
1158
                        label: 'test',
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 1185 1186 1187 1188 1189 1190
          ),
        ),
      );

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

1191
    testWidgets('floatingActionButton', (WidgetTester tester) async {
1192 1193 1194 1195
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1196
              key: key,
1197
              child: _GeometryListener(),
1198
              onPressed: () { },
1199 1200 1201 1202
            ),
      )));

      final RenderBox floatingActionButtonBox = tester.renderObject(find.byKey(key));
1203
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1204 1205 1206 1207 1208 1209
      final ScaffoldGeometry geometry = listenerState.cache.value;

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

      expect(
        geometry.floatingActionButtonArea,
1210
        fabRect,
1211 1212 1213 1214
      );
    });

    testWidgets('no floatingActionButton', (WidgetTester tester) async {
1215 1216
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1217
              constraints: const BoxConstraints.expand(height: 80.0),
1218
              child: _GeometryListener(),
1219 1220 1221
            ),
      )));

1222
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1223 1224 1225 1226
      final ScaffoldGeometry geometry = listenerState.cache.value;

      expect(
          geometry.floatingActionButtonArea,
1227
          null,
1228 1229 1230
      );
    });

1231
    testWidgets('floatingActionButton entrance/exit animation', (WidgetTester tester) async {
1232 1233 1234
      final GlobalKey key = GlobalKey();
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1235
              constraints: const BoxConstraints.expand(height: 80.0),
1236
              child: _GeometryListener(),
1237 1238 1239
            ),
      )));

1240 1241 1242
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1243
              key: key,
1244
              child: _GeometryListener(),
1245
              onPressed: () { },
1246 1247 1248
            ),
      )));

1249
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1250 1251
      await tester.pump(const Duration(milliseconds: 50));

1252
      ScaffoldGeometry geometry = listenerState.cache.value;
1253
      final Rect transitioningFabRect = geometry.floatingActionButtonArea!;
1254

1255 1256 1257 1258
      final double transitioningRotation = tester.widget<RotationTransition>(
        find.byType(RotationTransition),
      ).turns.value;

1259 1260 1261 1262 1263
      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;

1264 1265 1266 1267 1268 1269 1270 1271
      final double completedRotation = tester.widget<RotationTransition>(
        find.byType(RotationTransition),
      ).turns.value;

      expect(transitioningRotation, lessThan(1.0));

      expect(completedRotation, equals(1.0));

1272 1273
      expect(
        geometry.floatingActionButtonArea,
1274
        fabRect,
1275 1276 1277
      );

      expect(
1278
        geometry.floatingActionButtonArea!.center,
1279
        transitioningFabRect.center,
1280
      );
1281 1282

      expect(
1283
        geometry.floatingActionButtonArea!.width,
1284
        greaterThan(transitioningFabRect.width),
1285 1286 1287
      );

      expect(
1288
        geometry.floatingActionButtonArea!.height,
1289
        greaterThan(transitioningFabRect.height),
1290 1291 1292
      );
    });

1293
    testWidgets('change notifications', (WidgetTester tester) async {
1294
      final GlobalKey key = GlobalKey();
1295
      int numNotificationsAtLastFrame = 0;
1296 1297
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: ConstrainedBox(
1298
              constraints: const BoxConstraints.expand(height: 80.0),
1299
              child: _GeometryListener(),
1300 1301 1302
            ),
      )));

1303
      final _GeometryListenerState listenerState = tester.state(find.byType(_GeometryListener));
1304 1305 1306 1307

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

1308 1309 1310
      await tester.pumpWidget(MaterialApp(home: Scaffold(
            body: Container(),
            floatingActionButton: FloatingActionButton(
1311
              key: key,
1312
              child: _GeometryListener(),
1313
              onPressed: () { },
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
            ),
      )));

      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;
    });
1330

jslavitz's avatar
jslavitz committed
1331 1332 1333 1334 1335
    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';

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

      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();
    });

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
    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,
1383
                title: const Text('Title'),
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
              ),
            ),
          ),
        ),
      );

      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);
1398
      await tester.tap(endDrawerOpenButton, warnIfMissed: false); // hits the modal barrier
1399 1400 1401 1402 1403 1404
      await tester.pumpAndSettle();
      expect(false, scaffoldState.isDrawerOpen);

      await tester.tap(endDrawerOpenButton);
      await tester.pumpAndSettle();
      expect(true, scaffoldState.isEndDrawerOpen);
1405
      await tester.tap(drawerOpenButton, warnIfMissed: false); // hits the modal barrier
1406 1407
      await tester.pumpAndSettle();
      expect(false, scaffoldState.isEndDrawerOpen);
jslavitz's avatar
jslavitz committed
1408

1409 1410
      scaffoldState.openDrawer();
      expect(true, scaffoldState.isDrawerOpen);
1411
      await tester.tap(endDrawerOpenButton, warnIfMissed: false); // hits the modal barrier
1412
      await tester.pumpAndSettle();
1413
      expect(false, scaffoldState.isDrawerOpen);
1414 1415 1416

      scaffoldState.openEndDrawer();
      expect(true, scaffoldState.isEndDrawerOpen);
1417 1418 1419

      scaffoldState.openDrawer();
      expect(true, scaffoldState.isDrawerOpen);
1420 1421 1422
    });

    testWidgets('Dual Drawer Opening', (WidgetTester tester) async {
jslavitz's avatar
jslavitz committed
1423
      await tester.pumpWidget(
1424 1425
        MaterialApp(
          home: SafeArea(
jslavitz's avatar
jslavitz committed
1426 1427 1428 1429
            left: false,
            top: true,
            right: false,
            bottom: false,
1430
            child: Scaffold(
jslavitz's avatar
jslavitz committed
1431
              endDrawer: const Drawer(
1432
                child: Text('endDrawer'),
jslavitz's avatar
jslavitz committed
1433 1434
              ),
              drawer: const Drawer(
1435
                child: Text('drawer'),
jslavitz's avatar
jslavitz committed
1436 1437
              ),
              body: const Text('scaffold body'),
1438
              appBar: AppBar(
jslavitz's avatar
jslavitz committed
1439
                centerTitle: true,
1440 1441
                title: const Text('Title'),
              ),
jslavitz's avatar
jslavitz committed
1442 1443 1444 1445 1446 1447 1448 1449 1450
            ),
          ),
        ),
      );

      // 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();
1451
      await tester.tap(find.byType(IconButton).last, warnIfMissed: false); // hits the modal barrier
jslavitz's avatar
jslavitz committed
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
      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.
1466
      await tester.tap(find.byType(IconButton).last, warnIfMissed: false); // hits the modal barrier
jslavitz's avatar
jslavitz committed
1467 1468 1469 1470 1471 1472 1473
      await tester.pumpAndSettle();
      await tester.tap(find.byType(IconButton).first);
      await tester.pumpAndSettle();

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

1475 1476
    testWidgets('Drawer opens correctly with padding from MediaQuery (LTR)', (WidgetTester tester) async {
      const double simulatedNotchSize = 40.0;
1477 1478 1479 1480
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            drawer: const Drawer(
1481
              child: Text('Drawer'),
1482
            ),
1483
            body: const Text('Scaffold Body'),
1484 1485
            appBar: AppBar(
              centerTitle: true,
1486
              title: const Text('Title'),
1487 1488 1489 1490 1491 1492 1493 1494
            ),
          ),
        ),
      );

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

1495
      await tester.dragFrom(const Offset(simulatedNotchSize + 15.0, 100), const Offset(300, 0));
1496 1497 1498 1499 1500 1501 1502
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, false);

      await tester.pumpWidget(
        MaterialApp(
          home: MediaQuery(
            data: const MediaQueryData(
1503
              padding: EdgeInsets.fromLTRB(simulatedNotchSize, 0, 0, 0),
1504 1505 1506
            ),
            child: Scaffold(
              drawer: const Drawer(
1507
                child: Text('Drawer'),
1508
              ),
1509
              body: const Text('Scaffold Body'),
1510 1511
              appBar: AppBar(
                centerTitle: true,
1512
                title: const Text('Title'),
1513 1514 1515 1516 1517 1518 1519 1520
              ),
            ),
          ),
        ),
      );
      scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);

1521 1522 1523 1524
      await tester.dragFrom(
        const Offset(simulatedNotchSize + 15.0, 100),
        const Offset(300, 0),
      );
1525 1526 1527 1528
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, true);
    });

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
    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);

1559 1560 1561 1562
      await tester.pumpWidget(
        MaterialApp(
          home: MediaQuery(
            data: const MediaQueryData(
1563
              padding: EdgeInsets.fromLTRB(0, 0, simulatedNotchSize, 0),
1564 1565 1566 1567 1568
            ),
            child: Directionality(
              textDirection: TextDirection.rtl,
              child: Scaffold(
                drawer: const Drawer(
1569
                  child: Text('Drawer'),
1570
                ),
1571
                body: const Text('Scaffold body'),
1572 1573
                appBar: AppBar(
                  centerTitle: true,
1574
                  title: const Text('Title'),
1575 1576 1577 1578 1579 1580
                ),
              ),
            ),
          ),
        ),
      );
1581 1582
      scaffoldState = tester.state(find.byType(Scaffold));
      expect(scaffoldState.isDrawerOpen, false);
1583

1584 1585 1586 1587 1588 1589 1590 1591
      await tester.dragFrom(
        Offset(scaffoldWidth - simulatedNotchSize - 15.0, 100),
        const Offset(-300, 0),
      );
      await tester.pumpAndSettle();
      expect(scaffoldState.isDrawerOpen, true);
    });
  });
1592

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
  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'),
1605 1606
          ),
        ),
1607 1608 1609 1610
      ),
    );
    ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isDrawerOpen, false);
1611

1612 1613 1614
    await tester.dragFrom(const Offset(35, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isDrawerOpen, false);
1615

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
    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'),
1627 1628
          ),
        ),
1629 1630 1631 1632
      ),
    );
    scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isDrawerOpen, false);
1633

1634 1635 1636 1637
    await tester.dragFrom(const Offset(35, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isDrawerOpen, true);
  });
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 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
  testWidgets('Drawer does not open with a drag gesture when it is disabled', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          drawer: const Drawer(
            child: Text('Drawer'),
          ),
          drawerEnableOpenDragGesture: true,
          body: const Text('Scaffold Body'),
          appBar: AppBar(
            centerTitle: true,
            title: const Text('Title'),
          ),
        ),
      ),
    );
    ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isDrawerOpen, false);

    // Test that we can open the drawer with a drag gesture when
    // `Scaffold.drawerEnableDragGesture` is true.
    await tester.dragFrom(const Offset(0, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isDrawerOpen, true);

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

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          drawer: const Drawer(
            child: Text('Drawer'),
          ),
          drawerEnableOpenDragGesture: false,
          body: const Text('Scaffold body'),
          appBar: AppBar(
            centerTitle: true,
            title: const Text('Title'),
          ),
        ),
      ),
    );
    scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isDrawerOpen, false);

    // Test that we cannot open the drawer with a drag gesture when
    // `Scaffold.drawerEnableDragGesture` is false.
    await tester.dragFrom(const Offset(0, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isDrawerOpen, false);

    // Test that we can close drawer with a drag gesture when
    // `Scaffold.drawerEnableDragGesture` is false.
    final Finder drawerOpenButton = find.byType(IconButton).first;
    await tester.tap(drawerOpenButton);
    await tester.pumpAndSettle();
    expect(scaffoldState.isDrawerOpen, true);

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

  testWidgets('End drawer does not open with a drag gesture when it is disabled', (WidgetTester tester) async {
1705
    late double screenWidth;
1706 1707 1708 1709
    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
1710
            screenWidth = MediaQuery.of(context).size.width;
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
            return Scaffold(
              endDrawer: const Drawer(
                child: Text('Drawer'),
              ),
              endDrawerEnableOpenDragGesture: true,
              body: const Text('Scaffold Body'),
              appBar: AppBar(
                centerTitle: true,
                title: const Text('Title'),
              ),
            );
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 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
        ),
      ),
    );
    ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isEndDrawerOpen, false);

    // Test that we can open the end drawer with a drag gesture when
    // `Scaffold.endDrawerEnableDragGesture` is true.
    await tester.dragFrom(Offset(screenWidth - 1, 100), const Offset(-300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isEndDrawerOpen, true);

    await tester.dragFrom(Offset(screenWidth - 300, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isEndDrawerOpen, false);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          endDrawer: const Drawer(
            child: Text('Drawer'),
          ),
          endDrawerEnableOpenDragGesture: false,
          body: const Text('Scaffold body'),
          appBar: AppBar(
            centerTitle: true,
            title: const Text('Title'),
          ),
        ),
      ),
    );
    scaffoldState = tester.state(find.byType(Scaffold));
    expect(scaffoldState.isEndDrawerOpen, false);

    // Test that we cannot open the end drawer with a drag gesture when
    // `Scaffold.endDrawerEnableDragGesture` is false.
    await tester.dragFrom(Offset(screenWidth - 1, 100), const Offset(-300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isEndDrawerOpen, false);

    // Test that we can close the end drawer a with drag gesture when
    // `Scaffold.endDrawerEnableDragGesture` is false.
    final Finder endDrawerOpenButton = find.byType(IconButton).first;
    await tester.tap(endDrawerOpenButton);
    await tester.pumpAndSettle();
    expect(scaffoldState.isEndDrawerOpen, true);

    await tester.dragFrom(Offset(screenWidth - 300, 100), const Offset(300, 0));
    await tester.pumpAndSettle();
    expect(scaffoldState.isEndDrawerOpen, false);
  });

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

1779
    Widget buildFrame(bool? innerResizeToAvoidBottomInset, bool? outerResizeToAvoidBottomInset) {
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 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
      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));
  });
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836

  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(),
              ),
1837
              bottomSheet: const Text('Bottom sheet'),
1838 1839 1840
            ),
          ),
        );
1841
        late FlutterError error;
1842
        try {
1843
          key.currentState!.showBottomSheet<void>((BuildContext context) {
1844
            final ThemeData themeData = Theme.of(context);
1845 1846
            return Container(
              decoration: BoxDecoration(
1847
                border: Border(top: BorderSide(color: themeData.disabledColor)),
1848 1849 1850 1851 1852 1853
              ),
              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(
1854
                    color: themeData.colorScheme.secondary,
1855 1856 1857 1858 1859
                    fontSize: 24.0,
                  ),
                ),
              ),
            );
1860
          });
1861 1862 1863 1864 1865 1866
        } on FlutterError catch (e) {
          error = e;
        } finally {
          expect(error, isNotNull);
          expect(error.toStringDeep(), equalsIgnoringHashCodes(
            'FlutterError\n'
1867 1868
            '   Scaffold.bottomSheet cannot be specified while a bottom sheet\n'
            '   displayed with showBottomSheet() is still visible.\n'
1869 1870 1871 1872
            '   Rebuild the Scaffold with a null bottomSheet before calling\n'
            '   showBottomSheet().\n',
          ));
        }
1873
      },
1874 1875
    );

1876 1877 1878
    testWidgets(
      'didUpdate bottomSheet while a previous bottom sheet is still displayed',
      (WidgetTester tester) async {
1879 1880 1881
        final GlobalKey<ScaffoldState> key = GlobalKey<ScaffoldState>();
        const Key buttonKey = Key('button');
        final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
1882
        FlutterError.onError = (FlutterErrorDetails error) => errors.add(error);
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
        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(() {});
1896
                    },
1897 1898 1899
                  ),
                  bottomSheet: state == 0 ? null : const SizedBox(),
                );
1900
              },
1901 1902 1903
            ),
          ),
        );
1904
        key.currentState!.showBottomSheet<void>((_) => Container());
1905 1906 1907 1908
        await tester.tap(find.byKey(buttonKey));
        await tester.pump();
        expect(errors, isNotEmpty);
        expect(errors.first.exception, isFlutterError);
1909
        final FlutterError error = errors.first.exception as FlutterError;
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
        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'
1925
          '   Scaffold with a (non null) bottomSheet.\n',
1926
        );
1927
        await tester.pumpAndSettle();
1928 1929
      },
    );
1930 1931 1932 1933 1934 1935

    testWidgets('Call to Scaffold.of() without context', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: Builder(
            builder: (BuildContext context) {
1936
              Scaffold.of(context).showBottomSheet<void>((BuildContext context) {
1937 1938 1939 1940 1941 1942 1943 1944 1945
                return Container();
              });
              return Container();
            },
          ),
        ),
      );
      final dynamic exception = tester.takeException();
      expect(exception, isFlutterError);
1946
      final FlutterError error = exception as FlutterError;
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
      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',
        ),
      );
Dan Field's avatar
Dan Field committed
1974
      expect(error.diagnostics[4], isA<DiagnosticsProperty<Element>>());
1975 1976
      expect(
        error.toStringDeep(),
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
        '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'
1999
        '     Builder\n',
2000 2001 2002 2003 2004
      );
      await tester.pumpAndSettle();
    });

    testWidgets('Call to Scaffold.geometryOf() without context', (WidgetTester tester) async {
2005
      ValueListenable<ScaffoldGeometry>? geometry;
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
      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);
2019
      final FlutterError error = exception as FlutterError;
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
      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',
        ),
      );
Dan Field's avatar
Dan Field committed
2044
      expect(error.diagnostics[4], isA<DiagnosticsProperty<Element>>());
2045 2046
      expect(
        error.toStringDeep(),
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
        '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'
2065
        '     Builder\n',
2066 2067 2068
      );
      await tester.pumpAndSettle();
    });
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098

    testWidgets('FloatingActionButton always keeps the same position regardless of extendBodyBehindAppBar', (WidgetTester tester) async {
      await tester.pumpWidget(MaterialApp(
        home: Scaffold(
          appBar: AppBar(),
          floatingActionButton: FloatingActionButton(
            onPressed: () {},
            child: const Icon(Icons.add),
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
          extendBodyBehindAppBar: false,
        ),
      ));
      final Offset defaultOffset = tester.getCenter(find.byType(FloatingActionButton));

      await tester.pumpWidget(MaterialApp(
        home: Scaffold(
          appBar: AppBar(),
          floatingActionButton: FloatingActionButton(
            onPressed: () {},
            child: const Icon(Icons.add),
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
          extendBodyBehindAppBar: true,
        ),
      ));
      final Offset extendedBodyOffset = tester.getCenter(find.byType(FloatingActionButton));

      expect(defaultOffset.dy, extendedBodyOffset.dy);
    });
2099
  });
2100

2101
  testWidgets('ScaffoldMessenger.maybeOf can return null if not found', (WidgetTester tester) async {
2102
    ScaffoldMessengerState? scaffoldMessenger;
2103 2104 2105 2106 2107 2108 2109 2110 2111
    const Key tapTarget = Key('tap-target');
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(),
        child: Scaffold(
          body: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
2112
                key: tapTarget,
2113
                onTap: () {
2114
                  scaffoldMessenger = ScaffoldMessenger.maybeOf(context);
2115 2116
                },
                behavior: HitTestBehavior.opaque,
2117
                child: const SizedBox(
2118 2119 2120 2121
                  height: 100.0,
                  width: 100.0,
                ),
              );
2122
            },
2123 2124 2125 2126 2127 2128 2129 2130 2131
          ),
        ),
      ),
    ));
    await tester.tap(find.byKey(tapTarget));
    await tester.pump();
    expect(scaffoldMessenger, isNull);
  });

2132
  testWidgets('ScaffoldMessenger.of will assert if not found', (WidgetTester tester) async {
2133 2134 2135
    const Key tapTarget = Key('tap-target');

    final List<dynamic> exceptions = <dynamic>[];
2136
    final FlutterExceptionHandler? oldHandler = FlutterError.onError;
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
    FlutterError.onError = (FlutterErrorDetails details) {
      exceptions.add(details.exception);
    };

    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(),
        child: Scaffold(
          body: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
2149
                key: tapTarget,
2150 2151 2152 2153
                onTap: () {
                  ScaffoldMessenger.of(context);
                },
                behavior: HitTestBehavior.opaque,
2154
                child: const SizedBox(
2155 2156 2157 2158
                  height: 100.0,
                  width: 100.0,
                ),
              );
2159
            },
2160 2161 2162 2163 2164 2165 2166 2167
          ),
        ),
      ),
    ));
    await tester.tap(find.byKey(tapTarget));
    FlutterError.onError = oldHandler;

    expect(exceptions.length, 1);
2168
    // ignore: avoid_dynamic_calls
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
    expect(exceptions.single.runtimeType, FlutterError);
    final FlutterError error = exceptions.first as FlutterError;
    expect(error.diagnostics.length, 5);
    expect(error.diagnostics[2], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[3], isA<DiagnosticsBlock>());
    expect(error.diagnostics[4].level, DiagnosticLevel.hint);
    expect(
      error.diagnostics[4].toStringDeep(),
      equalsIgnoringHashCodes(
        'Typically, the ScaffoldMessenger widget is introduced by the\n'
        'MaterialApp at the top of your application widget tree.\n',
      ),
    );
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   No ScaffoldMessenger widget found.\n'
      '   Builder widgets require a ScaffoldMessenger widget ancestor.\n'
      '   The specific widget that could not find a ScaffoldMessenger\n'
      '   ancestor was:\n'
      '     Builder\n'
      '   The ancestors of this widget were:\n'
      '     _BodyBuilder\n'
      '     MediaQuery\n'
      '     LayoutId-[<_ScaffoldSlot.body>]\n'
      '     CustomMultiChildLayout\n'
      '     AnimatedBuilder\n'
      '     DefaultTextStyle\n'
      '     AnimatedDefaultTextStyle\n'
2197
      '     _InkFeatures-[GlobalKey#00000 ink renderer]\n'
2198 2199 2200 2201
      '     NotificationListener<LayoutChangedNotification>\n'
      '     PhysicalModel\n'
      '     AnimatedPhysicalModel\n'
      '     Material\n'
2202 2203 2204
      '     _ScrollNotificationObserverScope\n'
      '     NotificationListener<ScrollNotification>\n'
      '     ScrollNotificationObserver\n'
2205 2206 2207 2208 2209 2210
      '     _ScaffoldScope\n'
      '     Scaffold\n'
      '     MediaQuery\n'
      '     Directionality\n'
      '     [root]\n'
      '   Typically, the ScaffoldMessenger widget is introduced by the\n'
2211
      '   MaterialApp at the top of your application widget tree.\n',
2212 2213
    ));
  });
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245

  testWidgets('ScaffoldMessenger checks for nesting when a new Scaffold is registered', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/77251
    const String snackBarContent = 'SnackBar Content';
    await tester.pumpWidget(MaterialApp(
      home: Builder(
        builder: (BuildContext context) => Scaffold(
          body: Scaffold(
            body: TextButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute<void>(
                    builder: (BuildContext context) {
                      return Scaffold(
                        body: Column(
                          children: <Widget>[
                            TextButton(
                              onPressed: () {
                                const SnackBar snackBar = SnackBar(
                                  content: Text(snackBarContent),
                                  behavior: SnackBarBehavior.floating,
                                );
                                ScaffoldMessenger.of(context).showSnackBar(snackBar);
                              },
                              child: const Text('Show SnackBar'),
                            ),
                            TextButton(
                              onPressed: () {
                                Navigator.pop(context, null);
                              },
                              child: const Text('Pop route'),
2246
                            ),
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
                          ],
                        ),
                      );
                    },
                  ),
                );
              },
              child: const Text('Push route'),
            ),
          ),
        ),
2258
      ),
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
    ));

    expect(find.text(snackBarContent), findsNothing);
    await tester.tap(find.text('Push route'));
    await tester.pumpAndSettle();
    expect(find.text(snackBarContent), findsNothing);
    expect(find.text('Pop route'), findsOneWidget);

    // Show SnackBar on second page
    await tester.tap(find.text('Show SnackBar'));
    await tester.pump();
    expect(find.text(snackBarContent), findsOneWidget);
    // Pop the second page, the SnackBar completes a hero animation to the next route.
    // If we have not handled the nested Scaffolds properly, this will throw an
    // exception as duplicate SnackBars on the first route would have a common hero tag.
    await tester.tap(find.text('Pop route'));
    await tester.pump();
    // There are SnackBars two during the execution of the hero animation.
    expect(find.text(snackBarContent), findsNWidgets(2));
    await tester.pumpAndSettle();
    expect(find.text(snackBarContent), findsOneWidget);
    // Allow the SnackBar to animate out
    await tester.pump(const Duration(seconds: 4));
    await tester.pumpAndSettle();
    expect(find.text(snackBarContent), findsNothing);
  });
2285 2286
}

2287
class _GeometryListener extends StatefulWidget {
2288
  @override
2289
  _GeometryListenerState createState() => _GeometryListenerState();
2290 2291
}

2292
class _GeometryListenerState extends State<_GeometryListener> {
2293 2294
  @override
  Widget build(BuildContext context) {
2295
    return CustomPaint(
2296
      painter: cache,
2297 2298 2299 2300
    );
  }

  int numNotifications = 0;
2301 2302
  ValueListenable<ScaffoldGeometry>? geometryListenable;
  late _GeometryCachePainter cache;
2303 2304 2305 2306 2307 2308 2309 2310 2311

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

    if (geometryListenable != null)
2312
      geometryListenable!.removeListener(onGeometryChanged);
2313

2314
    geometryListenable = newListenable;
2315 2316
    geometryListenable!.addListener(onGeometryChanged);
    cache = _GeometryCachePainter(geometryListenable!);
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
  }

  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.
2327 2328
class _GeometryCachePainter extends CustomPainter {
  _GeometryCachePainter(this.geometryListenable) : super(repaint: geometryListenable);
2329 2330 2331

  final ValueListenable<ScaffoldGeometry> geometryListenable;

2332
  late ScaffoldGeometry value;
2333 2334 2335 2336 2337 2338
  @override
  void paint(Canvas canvas, Size size) {
    value = geometryListenable.value;
  }

  @override
2339
  bool shouldRepaint(_GeometryCachePainter oldDelegate) {
2340 2341
    return true;
  }
2342
}
2343

2344 2345
class _CustomPageRoute<T> extends PageRoute<T> {
  _CustomPageRoute({
2346
    required this.builder,
2347 2348 2349
    RouteSettings settings = const RouteSettings(),
    this.maintainState = true,
    bool fullscreenDialog = false,
2350 2351 2352 2353 2354 2355 2356 2357 2358
  }) : assert(builder != null),
       super(settings: settings, fullscreenDialog: fullscreenDialog);

  final WidgetBuilder builder;

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

  @override
2359
  Color? get barrierColor => null;
2360 2361

  @override
2362
  String? get barrierLabel => null;
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376

  @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;
  }
}