app_bar_test.dart 93.9 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.

Dan Field's avatar
Dan Field committed
5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8
import 'package:flutter/services.dart';
9 10
import 'package:flutter_test/flutter_test.dart';

11
import '../rendering/mock_canvas.dart';
12 13
import '../widgets/semantics_tester.dart';

14
Widget buildSliverAppBarApp({
15 16 17 18
  bool floating = false,
  bool pinned = false,
  double? collapsedHeight,
  double? expandedHeight,
19
  bool snap = false,
20
  double toolbarHeight = kToolbarHeight,
21
}) {
22 23 24 25 26 27 28 29
  return MaterialApp(
    home: Scaffold(
      body: DefaultTabController(
        length: 3,
        child: CustomScrollView(
          primary: true,
          slivers: <Widget>[
            SliverAppBar(
30
              backwardsCompatibility: false,
31 32 33 34 35 36 37 38 39 40
              title: const Text('AppBar Title'),
              floating: floating,
              pinned: pinned,
              collapsedHeight: collapsedHeight,
              expandedHeight: expandedHeight,
              toolbarHeight: toolbarHeight,
              snap: snap,
              bottom: TabBar(
                tabs: <String>['A','B','C'].map<Widget>((String t) => Tab(text: 'TAB $t')).toList(),
              ),
41
            ),
42 43 44 45 46 47 48
            SliverToBoxAdapter(
              child: Container(
                height: 1200.0,
                color: Colors.orange[400],
              ),
            ),
          ],
49
        ),
50
      ),
51 52 53 54 55
    ),
  );
}

ScrollController primaryScrollController(WidgetTester tester) {
56
  return PrimaryScrollController.of(tester.element(find.byType(CustomScrollView)))!;
57 58
}

59 60 61
double appBarHeight(WidgetTester tester) => tester.getSize(find.byType(AppBar, skipOffstage: false)).height;
double appBarTop(WidgetTester tester) => tester.getTopLeft(find.byType(AppBar, skipOffstage: false)).dy;
double appBarBottom(WidgetTester tester) => tester.getBottomLeft(find.byType(AppBar, skipOffstage: false)).dy;
62

63
double tabBarHeight(WidgetTester tester) => tester.getSize(find.byType(TabBar, skipOffstage: false)).height;
64

65
void main() {
66 67 68 69
  setUp(() {
    debugResetSemanticsIdCounter();
  });

70 71
  testWidgets('AppBar centers title on iOS', (WidgetTester tester) async {
    await tester.pumpWidget(
72 73 74 75
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
76
            backwardsCompatibility: false,
77
            title: const Text('X'),
78 79 80
          ),
        ),
      ),
81 82
    );

83
    final Finder title = find.text('X');
84
    Offset center = tester.getCenter(title);
85
    Size size = tester.getSize(title);
86
    expect(center.dx, lessThan(400 - size.width / 2.0));
87

Dan Field's avatar
Dan Field committed
88 89 90
    for (final TargetPlatform platform in <TargetPlatform>[TargetPlatform.iOS, TargetPlatform.macOS]) {
      // Clear the widget tree to avoid animating between platforms.
      await tester.pumpWidget(Container(key: UniqueKey()));
91

Dan Field's avatar
Dan Field committed
92 93 94 95 96
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData(platform: platform),
          home: Scaffold(
            appBar: AppBar(
97
              backwardsCompatibility: false,
Dan Field's avatar
Dan Field committed
98 99
              title: const Text('X'),
            ),
100 101
          ),
        ),
Dan Field's avatar
Dan Field committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
      );

      center = tester.getCenter(title);
      size = tester.getSize(title);
      expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}');
      expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${describeEnum(platform)}');

      // One action is still centered.

      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData(platform: platform),
          home: Scaffold(
            appBar: AppBar(
116
              backwardsCompatibility: false,
Dan Field's avatar
Dan Field committed
117 118 119 120 121
              title: const Text('X'),
              actions: const <Widget>[
                Icon(Icons.thumb_up),
              ],
            ),
122 123
          ),
        ),
Dan Field's avatar
Dan Field committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
      );

      center = tester.getCenter(title);
      size = tester.getSize(title);
      expect(center.dx, greaterThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}');
      expect(center.dx, lessThan(400 + size.width / 2.0), reason: 'on ${describeEnum(platform)}');

      // Two actions is left aligned again.

      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData(platform: platform),
          home: Scaffold(
            appBar: AppBar(
138
              backwardsCompatibility: false,
Dan Field's avatar
Dan Field committed
139 140 141 142 143 144
              title: const Text('X'),
              actions: const <Widget>[
                Icon(Icons.thumb_up),
                Icon(Icons.thumb_up),
              ],
            ),
145 146
          ),
        ),
Dan Field's avatar
Dan Field committed
147
      );
148

Dan Field's avatar
Dan Field committed
149 150 151 152
      center = tester.getCenter(title);
      size = tester.getSize(title);
      expect(center.dx, lessThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}');
    }
153
  });
154 155 156

  testWidgets('AppBar centerTitle:true centers on Android', (WidgetTester tester) async {
    await tester.pumpWidget(
157 158 159 160
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
161
            backwardsCompatibility: false,
162
            centerTitle: true,
163
            title: const Text('X'),
164 165
          ),
        ),
166
      ),
167 168
    );

169
    final Finder title = find.text('X');
170
    final Offset center = tester.getCenter(title);
171
    final Size size = tester.getSize(title);
172 173
    expect(center.dx, greaterThan(400 - size.width / 2.0));
    expect(center.dx, lessThan(400 + size.width / 2.0));
174 175
  });

176
  testWidgets('AppBar centerTitle:false title start edge is 16.0 (LTR)', (WidgetTester tester) async {
177
    await tester.pumpWidget(
178 179 180
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
181
            backwardsCompatibility: false,
182
            centerTitle: false,
183
            title: const Placeholder(key: Key('X')),
184 185 186 187 188 189 190
          ),
        ),
      ),
    );

    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopLeft(titleWidget).dx, 16.0);
191
    expect(tester.getTopRight(titleWidget).dx, 800 - 16.0);
192 193 194 195
  });

  testWidgets('AppBar centerTitle:false title start edge is 16.0 (RTL)', (WidgetTester tester) async {
    await tester.pumpWidget(
196 197
      MaterialApp(
        home: Directionality(
198
          textDirection: TextDirection.rtl,
199 200
          child: Scaffold(
            appBar: AppBar(
201
              backwardsCompatibility: false,
202
              centerTitle: false,
203
              title: const Placeholder(key: Key('X')),
204 205 206 207 208 209 210 211
            ),
          ),
        ),
      ),
    );

    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopRight(titleWidget).dx, 800.0 - 16.0);
212
    expect(tester.getTopLeft(titleWidget).dx, 16.0);
213 214 215 216
  });

  testWidgets('AppBar titleSpacing:32 title start edge is 32.0 (LTR)', (WidgetTester tester) async {
    await tester.pumpWidget(
217 218 219
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
220
            backwardsCompatibility: false,
221 222
            centerTitle: false,
            titleSpacing: 32.0,
223
            title: const Placeholder(key: Key('X')),
224 225 226 227 228
          ),
        ),
      ),
    );

229 230
    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopLeft(titleWidget).dx, 32.0);
231
    expect(tester.getTopRight(titleWidget).dx, 800 - 32.0);
232 233
  });

234
  testWidgets('AppBar titleSpacing:32 title start edge is 32.0 (RTL)', (WidgetTester tester) async {
235
    await tester.pumpWidget(
236 237
      MaterialApp(
        home: Directionality(
238
          textDirection: TextDirection.rtl,
239 240
          child: Scaffold(
            appBar: AppBar(
241
              backwardsCompatibility: false,
242
              centerTitle: false,
243
              titleSpacing: 32.0,
244
              title: const Placeholder(key: Key('X')),
245 246 247 248 249 250
            ),
          ),
        ),
      ),
    );

251 252
    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopRight(titleWidget).dx, 800.0 - 32.0);
253
    expect(tester.getTopLeft(titleWidget).dx, 32.0);
254 255
  });

256
  testWidgets(
257
    'AppBar centerTitle:false leading button title left edge is 72.0 (LTR)',
258
    (WidgetTester tester) async {
259 260 261 262
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            appBar: AppBar(
263
              backwardsCompatibility: false,
264 265 266 267 268
              centerTitle: false,
              title: const Text('X'),
            ),
            // A drawer causes a leading hamburger.
            drawer: const Drawer(),
269 270
          ),
        ),
271
      );
272

273 274
      expect(tester.getTopLeft(find.text('X')).dx, 72.0);
    });
275

276 277 278
  testWidgets(
    'AppBar centerTitle:false leading button title left edge is 72.0 (RTL)',
    (WidgetTester tester) async {
279 280 281 282 283 284
      await tester.pumpWidget(
        MaterialApp(
          home: Directionality(
            textDirection: TextDirection.rtl,
            child: Scaffold(
              appBar: AppBar(
285
                backwardsCompatibility: false,
286 287 288 289 290
                centerTitle: false,
                title: const Text('X'),
              ),
              // A drawer causes a leading hamburger.
              drawer: const Drawer(),
291 292 293
            ),
          ),
        ),
294
      );
295

296 297
      expect(tester.getTopRight(find.text('X')).dx, 800.0 - 72.0);
    });
298

Ian Hickson's avatar
Ian Hickson committed
299
  testWidgets('AppBar centerTitle:false title overflow OK', (WidgetTester tester) async {
300 301 302
    // The app bar's title should be constrained to fit within the available space
    // between the leading and actions widgets.

303 304
    final Key titleKey = UniqueKey();
    Widget leading = Container();
305
    List<Widget> actions = <Widget>[];
306 307

    Widget buildApp() {
308 309 310
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
311
            backwardsCompatibility: false,
312 313
            leading: leading,
            centerTitle: false,
314
            title: Container(
315
              key: titleKey,
316
              constraints: BoxConstraints.loose(const Size(1000.0, 1000.0)),
317
            ),
318 319 320
            actions: actions,
          ),
        ),
321 322 323 324 325
      );
    }

    await tester.pumpWidget(buildApp());

326
    final Finder title = find.byKey(titleKey);
327
    expect(tester.getTopLeft(title).dx, 72.0);
328 329 330 331 332
    expect(tester.getSize(title).width, equals(
        800.0 // Screen width.
        - 56.0 // Leading button width.
        - 16.0 // Leading button to title padding.
        - 16.0)); // Title right side padding.
333 334

    actions = <Widget>[
335
      const SizedBox(width: 100.0),
336
      const SizedBox(width: 100.0),
337 338 339
    ];
    await tester.pumpWidget(buildApp());

340
    expect(tester.getTopLeft(title).dx, 72.0);
341
    // The title shrinks by 200.0 to allow for the actions widgets.
342 343 344 345 346 347
    expect(tester.getSize(title).width, equals(
        800.0 // Screen width.
        - 56.0 // Leading button width.
        - 16.0 // Leading button to title padding.
        - 16.0 // Title to actions padding
        - 200.0)); // Actions' width.
348

349
    leading = Container(); // AppBar will constrain the width to 24.0
350
    await tester.pumpWidget(buildApp());
351
    expect(tester.getTopLeft(title).dx, 72.0);
352
    // Adding a leading widget shouldn't effect the title's size
353
    expect(tester.getSize(title).width, equals(800.0 - 56.0 - 16.0 - 16.0 - 200.0));
354 355
  });

356
  testWidgets('AppBar centerTitle:true title overflow OK (LTR)', (WidgetTester tester) async {
357 358
    // The app bar's title should be constrained to fit within the available space
    // between the leading and actions widgets. When it's also centered it may
359
    // also be start or end justified if it doesn't fit in the overall center.
360

361
    final Key titleKey = UniqueKey();
362
    double titleWidth = 700.0;
363 364
    Widget? leading = Container();
    List<Widget> actions = <Widget>[];
365 366

    Widget buildApp() {
367 368 369
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
370
            backwardsCompatibility: false,
371 372
            leading: leading,
            centerTitle: true,
373
            title: Container(
374
              key: titleKey,
375
              constraints: BoxConstraints.loose(Size(titleWidth, 1000.0)),
376
            ),
377 378 379
            actions: actions,
          ),
        ),
380 381 382 383
      );
    }

    // Centering a title with width 700 within the 800 pixel wide test widget
384
    // would mean that its start edge would have to be 50. The material spec says
385
    // that the start edge of the title must be at least 72.
386 387
    await tester.pumpWidget(buildApp());

388
    final Finder title = find.byKey(titleKey);
389
    expect(tester.getTopLeft(title).dx, 72.0);
390 391 392
    expect(tester.getSize(title).width, equals(700.0));

    // Centering a title with width 620 within the 800 pixel wide test widget
393
    // would mean that its start edge would have to be 90. We reserve 72
394 395
    // on the start and the padded actions occupy 96 on the end. That
    // leaves 632, so the title is end justified but its width isn't changed.
396 397 398 399 400

    await tester.pumpWidget(buildApp());
    leading = null;
    titleWidth = 620.0;
    actions = <Widget>[
401
      const SizedBox(width: 48.0),
402
      const SizedBox(width: 48.0),
403 404
    ];
    await tester.pumpWidget(buildApp());
405
    expect(tester.getTopLeft(title).dx, 800 - 620 - 48 - 48);
406 407 408
    expect(tester.getSize(title).width, equals(620.0));
  });

409 410 411 412 413
  testWidgets('AppBar centerTitle:true title overflow OK (RTL)', (WidgetTester tester) async {
    // The app bar's title should be constrained to fit within the available space
    // between the leading and actions widgets. When it's also centered it may
    // also be start or end justified if it doesn't fit in the overall center.

414
    final Key titleKey = UniqueKey();
415
    double titleWidth = 700.0;
416 417
    Widget? leading = Container();
    List<Widget> actions = <Widget>[];
418 419

    Widget buildApp() {
420 421
      return MaterialApp(
        home: Directionality(
422
          textDirection: TextDirection.rtl,
423 424
          child: Scaffold(
            appBar: AppBar(
425
              backwardsCompatibility: false,
426 427
              leading: leading,
              centerTitle: true,
428
              title: Container(
429
                key: titleKey,
430
                constraints: BoxConstraints.loose(Size(titleWidth, 1000.0)),
431 432 433 434 435 436 437 438 439 440
              ),
              actions: actions,
            ),
          ),
        ),
      );
    }

    // Centering a title with width 700 within the 800 pixel wide test widget
    // would mean that its start edge would have to be 50. The material spec says
441
    // that the start edge of the title must be at least 72.
442 443 444 445 446 447 448 449
    await tester.pumpWidget(buildApp());

    final Finder title = find.byKey(titleKey);
    expect(tester.getTopRight(title).dx, 800.0 - 72.0);
    expect(tester.getSize(title).width, equals(700.0));

    // Centering a title with width 620 within the 800 pixel wide test widget
    // would mean that its start edge would have to be 90. We reserve 72
450 451
    // on the start and the padded actions occupy 96 on the end. That
    // leaves 632, so the title is end justified but its width isn't changed.
452 453 454 455 456 457

    await tester.pumpWidget(buildApp());
    leading = null;
    titleWidth = 620.0;
    actions = <Widget>[
      const SizedBox(width: 48.0),
458
      const SizedBox(width: 48.0),
459 460
    ];
    await tester.pumpWidget(buildApp());
461
    expect(tester.getTopRight(title).dx, 620 + 48 + 48);
462 463 464
    expect(tester.getSize(title).width, equals(620.0));
  });

465 466
  testWidgets('AppBar with no Scaffold', (WidgetTester tester) async {
    await tester.pumpWidget(
467 468
      MaterialApp(
        home: SizedBox(
469
          height: kToolbarHeight,
470
          child: AppBar(
471
            backwardsCompatibility: false,
472 473
            leading: const Text('L'),
            title: const Text('No Scaffold'),
474
            actions: const <Widget>[Text('A1'), Text('A2')],
475
          ),
476 477 478 479 480 481 482 483 484 485
        ),
      ),
    );

    expect(find.text('L'), findsOneWidget);
    expect(find.text('No Scaffold'), findsOneWidget);
    expect(find.text('A1'), findsOneWidget);
    expect(find.text('A2'), findsOneWidget);
  });

486 487
  testWidgets('AppBar render at zero size', (WidgetTester tester) async {
    await tester.pumpWidget(
488 489
      MaterialApp(
        home: Center(
490
          child: SizedBox(
491 492
            height: 0.0,
            width: 0.0,
493 494
            child: Scaffold(
              appBar: AppBar(
495
                backwardsCompatibility: false,
496 497
                title: const Text('X'),
              ),
498 499 500 501
            ),
          ),
        ),
      ),
502 503
    );

504
    final Finder title = find.text('X');
505 506
    expect(tester.getSize(title).isEmpty, isTrue);
  });
507 508

  testWidgets('AppBar actions are vertically centered', (WidgetTester tester) async {
509 510 511 512 513
    final UniqueKey appBarKey = UniqueKey();
    final UniqueKey leadingKey = UniqueKey();
    final UniqueKey titleKey = UniqueKey();
    final UniqueKey action0Key = UniqueKey();
    final UniqueKey action1Key = UniqueKey();
514 515

    await tester.pumpWidget(
516 517 518
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
519
            backwardsCompatibility: false,
520
            key: appBarKey,
521 522
            leading: SizedBox(key: leadingKey, height: 50.0),
            title: SizedBox(key: titleKey, height: 40.0),
523
            actions: <Widget>[
524 525
              SizedBox(key: action0Key, height: 20.0),
              SizedBox(key: action1Key, height: 30.0),
526 527 528
            ],
          ),
        ),
529
      ),
530 531 532
    );

    // The vertical center of the widget with key, in global coordinates.
533
    double yCenter(Key key) => tester.getCenter(find.byKey(key)).dy;
534 535 536 537 538 539 540

    expect(yCenter(appBarKey), equals(yCenter(leadingKey)));
    expect(yCenter(appBarKey), equals(yCenter(titleKey)));
    expect(yCenter(appBarKey), equals(yCenter(action0Key)));
    expect(yCenter(appBarKey), equals(yCenter(action1Key)));
  });

541 542
  testWidgets('leading button extends to edge and is square', (WidgetTester tester) async {
    await tester.pumpWidget(
543 544 545 546
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
547
            backwardsCompatibility: false,
548
            title: const Text('X'),
549
          ),
550
          drawer: Column(), // Doesn't really matter. Triggers a hamburger regardless.
551 552
        ),
      ),
553 554
    );

555
    final Finder hamburger = find.byTooltip('Open navigation menu');
556
    expect(tester.getTopLeft(hamburger), Offset.zero);
557
    expect(tester.getSize(hamburger), const Size(56.0, 56.0));
558 559 560 561
  });

  testWidgets('test action is 4dp from edge and 48dp min', (WidgetTester tester) async {
    await tester.pumpWidget(
562 563 564 565
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
566
            backwardsCompatibility: false,
567
            title: const Text('X'),
568
            actions: const <Widget> [
569 570
              IconButton(
                icon: Icon(Icons.share),
571 572 573 574
                onPressed: null,
                tooltip: 'Share',
                iconSize: 20.0,
              ),
575 576
              IconButton(
                icon: Icon(Icons.add),
577 578 579 580 581 582
                onPressed: null,
                tooltip: 'Add',
                iconSize: 60.0,
              ),
            ],
          ),
583 584
        ),
      ),
585 586
    );

587
    final Finder addButton = find.byTooltip('Add');
588
    expect(tester.getTopRight(addButton), const Offset(800.0, 0.0));
589
    // It's still the size it was plus the 2 * 8dp padding from IconButton.
590
    expect(tester.getSize(addButton), const Size(60.0 + 2 * 8.0, 56.0));
591

592
    final Finder shareButton = find.byTooltip('Share');
593
    // The 20dp icon is expanded to fill the IconButton's touch target to 48dp.
594
    expect(tester.getSize(shareButton), const Size(48.0, 56.0));
595 596
  });

597 598 599 600 601 602 603
  testWidgets('SliverAppBar default configuration', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: false,
      expandedHeight: null,
    ));

604
    final ScrollController controller = primaryScrollController(tester);
605
    expect(controller.offset, 0.0);
606
    expect(find.byType(SliverAppBar), findsOneWidget);
607 608 609 610 611 612 613

    final double initialAppBarHeight = appBarHeight(tester);
    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar partially out of view
    controller.jumpTo(50.0);
    await tester.pump();
614
    expect(find.byType(SliverAppBar), findsOneWidget);
615 616 617 618 619 620
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);

    // Scroll the not-pinned appbar out of view
    controller.jumpTo(600.0);
    await tester.pump();
621
    expect(find.byType(SliverAppBar), findsNothing);
622 623 624 625 626 627
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);

    // Scroll the not-pinned appbar back into view
    controller.jumpTo(0.0);
    await tester.pump();
628
    expect(find.byType(SliverAppBar), findsOneWidget);
629 630 631 632 633 634 635 636 637 638 639 640
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });

  testWidgets('SliverAppBar expandedHeight, pinned', (WidgetTester tester) async {

    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: true,
      expandedHeight: 128.0,
    ));

641
    final ScrollController controller = primaryScrollController(tester);
642
    expect(controller.offset, 0.0);
643
    expect(find.byType(SliverAppBar), findsOneWidget);
644 645
    expect(appBarHeight(tester), 128.0);

646
    const double initialAppBarHeight = 128.0;
647 648 649 650 651 652
    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar, collapsing the expanded height. At this
    // point both the toolbar and the tabbar are visible.
    controller.jumpTo(600.0);
    await tester.pump();
653
    expect(find.byType(SliverAppBar), findsOneWidget);
654 655 656 657 658 659 660
    expect(tabBarHeight(tester), initialTabBarHeight);
    expect(appBarHeight(tester), lessThan(initialAppBarHeight));
    expect(appBarHeight(tester), greaterThan(initialTabBarHeight));

    // Scroll the not-pinned appbar back into view
    controller.jumpTo(0.0);
    await tester.pump();
661
    expect(find.byType(SliverAppBar), findsOneWidget);
662 663 664 665 666 667 668 669 670 671 672 673
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });

  testWidgets('SliverAppBar expandedHeight, pinned and floating', (WidgetTester tester) async {

    await tester.pumpWidget(buildSliverAppBarApp(
      floating: true,
      pinned: true,
      expandedHeight: 128.0,
    ));

674
    final ScrollController controller = primaryScrollController(tester);
675
    expect(controller.offset, 0.0);
676
    expect(find.byType(SliverAppBar), findsOneWidget);
677 678
    expect(appBarHeight(tester), 128.0);

679
    const double initialAppBarHeight = 128.0;
680 681
    final double initialTabBarHeight = tabBarHeight(tester);

682
    // Scroll the floating-pinned appbar, collapsing the expanded height. At this
683 684 685
    // point only the tabBar is visible.
    controller.jumpTo(600.0);
    await tester.pump();
686
    expect(find.byType(SliverAppBar), findsOneWidget);
687 688 689 690
    expect(tabBarHeight(tester), initialTabBarHeight);
    expect(appBarHeight(tester), lessThan(initialAppBarHeight));
    expect(appBarHeight(tester), initialTabBarHeight);

691
    // Scroll the floating-pinned appbar back into view
692 693
    controller.jumpTo(0.0);
    await tester.pump();
694
    expect(find.byType(SliverAppBar), findsOneWidget);
695 696 697
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });
698 699 700 701 702 703 704 705

  testWidgets('SliverAppBar expandedHeight, floating with snap:true', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: true,
      pinned: false,
      snap: true,
      expandedHeight: 128.0,
    ));
706
    expect(find.byType(SliverAppBar), findsOneWidget);
707 708 709 710 711 712 713 714
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);
    expect(appBarBottom(tester), 128.0);

    // Scroll to the middle of the list. The (floating) appbar is no longer visible.
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
    position.jumpTo(256.00);
    await tester.pumpAndSettle();
715
    expect(find.byType(SliverAppBar), findsNothing);
716 717 718
    expect(appBarTop(tester), lessThanOrEqualTo(-128.0));

    // Drag the scrollable up and down. The app bar should not snap open, its
719
    // height should just track the drag offset.
Hans Muller's avatar
Hans Muller committed
720
    TestGesture gesture = await tester.startGesture(const Offset(50.0, 256.0));
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
    await gesture.moveBy(const Offset(0.0, 128.0)); // drag the appbar all the way open
    await tester.pump();
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);

    await gesture.moveBy(const Offset(0.0, -50.0));
    await tester.pump();
    expect(appBarBottom(tester), 78.0); // 78 == 128 - 50

    // Trigger the snap open animation: drag down and release
    await gesture.moveBy(const Offset(0.0, 10.0));
    await gesture.up();

    // Now verify that the appbar is animating open
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    double bottom = appBarBottom(tester);
    expect(bottom, greaterThan(88.0)); // 88 = 78 + 10

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarBottom(tester), greaterThan(bottom));

    // The animation finishes when the appbar is full height.
    await tester.pumpAndSettle();
    expect(appBarHeight(tester), 128.0);

    // Now that the app bar is open, perform the same drag scenario
    // in reverse: drag the appbar up and down and then trigger the
    // snap closed animation.
Hans Muller's avatar
Hans Muller committed
751
    gesture = await tester.startGesture(const Offset(50.0, 256.0));
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
    await gesture.moveBy(const Offset(0.0, -128.0)); // drag the appbar closed
    await tester.pump();
    expect(appBarBottom(tester), 0.0);

    await gesture.moveBy(const Offset(0.0, 100.0));
    await tester.pump();
    expect(appBarBottom(tester), 100.0);

    // Trigger the snap close animation: drag upwards and release
    await gesture.moveBy(const Offset(0.0, -10.0));
    await gesture.up();

    // Now verify that the appbar is animating closed
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    bottom = appBarBottom(tester);
    expect(bottom, lessThan(90.0));

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarBottom(tester), lessThan(bottom));

    // The animation finishes when the appbar is off screen.
    await tester.pumpAndSettle();
    expect(appBarTop(tester), lessThanOrEqualTo(0.0));
    expect(appBarBottom(tester), lessThanOrEqualTo(0.0));
  });

  testWidgets('SliverAppBar expandedHeight, floating and pinned with snap:true', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: true,
      pinned: true,
      snap: true,
      expandedHeight: 128.0,
    ));
787
    expect(find.byType(SliverAppBar), findsOneWidget);
788 789 790 791 792 793 794 795 796
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);
    expect(appBarBottom(tester), 128.0);

    // Scroll to the middle of the list. The only the tab bar is visible
    // because this is a pinned appbar.
    final ScrollPosition position = tester.state<ScrollableState>(find.byType(Scrollable)).position;
    position.jumpTo(256.0);
    await tester.pumpAndSettle();
797
    expect(find.byType(SliverAppBar), findsOneWidget);
798 799 800 801
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kTextTabBarHeight);

    // Drag the scrollable up and down. The app bar should not snap open, the
Shi-Hao Hong's avatar
Shi-Hao Hong committed
802
    // bottom of the appbar should just track the drag offset.
Hans Muller's avatar
Hans Muller committed
803
    TestGesture gesture = await tester.startGesture(const Offset(50.0, 200.0));
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
    await gesture.moveBy(const Offset(0.0, 100.0));
    await tester.pump();
    expect(appBarHeight(tester), 100.0);

    await gesture.moveBy(const Offset(0.0, -25.0));
    await tester.pump();
    expect(appBarHeight(tester), 75.0);

    // Trigger the snap animation: drag down and release
    await gesture.moveBy(const Offset(0.0, 10.0));
    await gesture.up();

    // Now verify that the appbar is animating open
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    final double height = appBarHeight(tester);
    expect(height, greaterThan(85.0));
    expect(height, lessThan(128.0));

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarHeight(tester), greaterThan(height));
    expect(appBarHeight(tester), lessThan(128.0));

    // The animation finishes when the appbar is fully expanded
    await tester.pumpAndSettle();
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), 128.0);
    expect(appBarBottom(tester), 128.0);

    // Now that the appbar is fully expanded, Perform the same drag
    // scenario in reverse: drag the appbar up and down and then trigger
    // the snap closed animation.
Hans Muller's avatar
Hans Muller committed
837
    gesture = await tester.startGesture(const Offset(50.0, 256.0));
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    await gesture.moveBy(const Offset(0.0, -128.0));
    await tester.pump();
    expect(appBarBottom(tester), kTextTabBarHeight);

    await gesture.moveBy(const Offset(0.0, 100.0));
    await tester.pump();
    expect(appBarBottom(tester), 100.0);

    // Trigger the snap close animation: drag upwards and release
    await gesture.moveBy(const Offset(0.0, -10.0));
    await gesture.up();

    // Now verify that the appbar is animating closed
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    final double bottom = appBarBottom(tester);
    expect(bottom, lessThan(90.0));

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(appBarBottom(tester), lessThan(bottom));

    // The animation finishes when the appbar shrinks back to its pinned height
    await tester.pumpAndSettle();
    expect(appBarTop(tester), lessThanOrEqualTo(0.0));
    expect(appBarBottom(tester), kTextTabBarHeight);
  });
865

866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  testWidgets('SliverAppBar expandedHeight, collapsedHeight', (WidgetTester tester) async {
    const double expandedAppBarHeight = 400.0;
    const double collapsedAppBarHeight = 200.0;

    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: false,
      collapsedHeight: collapsedAppBarHeight,
      expandedHeight: expandedAppBarHeight,
    ));

    final ScrollController controller = primaryScrollController(tester);
    expect(controller.offset, 0.0);
    expect(find.byType(SliverAppBar), findsOneWidget);
    expect(appBarHeight(tester), expandedAppBarHeight);

    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar partially out of view.
    controller.jumpTo(50.0);
    await tester.pump();
    expect(find.byType(SliverAppBar), findsOneWidget);
    expect(appBarHeight(tester), expandedAppBarHeight - 50.0);
    expect(tabBarHeight(tester), initialTabBarHeight);

    // Scroll the not-pinned appbar out of view, to its collapsed height.
    controller.jumpTo(600.0);
    await tester.pump();
    expect(find.byType(SliverAppBar), findsNothing);
    expect(appBarHeight(tester), collapsedAppBarHeight + initialTabBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);

    // Scroll the not-pinned appbar back into view.
    controller.jumpTo(0.0);
    await tester.pump();
    expect(find.byType(SliverAppBar), findsOneWidget);
    expect(appBarHeight(tester), expandedAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });

906 907
  testWidgets('AppBar uses the specified elevation or defaults to 4.0', (WidgetTester tester) async {
    Widget buildAppBar([double? elevation]) {
908
      return MaterialApp(
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
        home: Scaffold(
          appBar: AppBar(title: const Text('Title'), elevation: elevation),
        ),
      );
    }

    Material getMaterial() => tester.widget<Material>(find.descendant(
      of: find.byType(AppBar),
      matching: find.byType(Material),
    ));

    // Default elevation should be _AppBarState._defaultElevation = 4.0
    await tester.pumpWidget(buildAppBar());
    expect(getMaterial().elevation, 4.0);

    // AppBar should use the specified elevation.
    await tester.pumpWidget(buildAppBar(8.0));
    expect(getMaterial().elevation, 8.0);
  });

  group('SliverAppBar elevation', () {
    Widget buildSliverAppBar(bool forceElevated, {double? elevation, double? themeElevation}) {
      return MaterialApp(
        theme: ThemeData(appBarTheme: AppBarTheme(elevation: themeElevation)),
933 934 935
        home: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
936
              backwardsCompatibility: false,
937 938
              title: const Text('Title'),
              forceElevated: forceElevated,
939
              elevation: elevation,
940 941 942 943 944 945
            ),
          ],
        ),
      );
    }

946 947 948 949 950 951 952 953
    testWidgets('Respects forceElevated parameter', (WidgetTester tester) async {
      // Regression test for https://github.com/flutter/flutter/issues/59158.
      AppBar getAppBar() => tester.widget<AppBar>(find.byType(AppBar));
      Material getMaterial() => tester.widget<Material>(find.byType(Material));

      // When forceElevated is off, SliverAppBar should not be elevated.
      await tester.pumpWidget(buildSliverAppBar(false));
      expect(getMaterial().elevation, 0.0);
954

955 956 957 958 959 960 961 962 963 964
      // Default elevation should be _AppBarState._defaultElevation = 4.0, and
      // the AppBar's elevation should not be specified by SliverAppBar.
      await tester.pumpWidget(buildSliverAppBar(true));
      expect(getMaterial().elevation, 4.0);
      expect(getAppBar().elevation, null);

      // SliverAppBar should use the specified elevation.
      await tester.pumpWidget(buildSliverAppBar(true, elevation: 8.0));
      expect(getMaterial().elevation, 8.0);
    });
965

966 967 968 969 970 971 972 973 974 975 976 977 978
    testWidgets('Uses elevation of AppBarTheme by default', (WidgetTester tester) async {
      // Regression test for https://github.com/flutter/flutter/issues/73525.
      Material getMaterial() => tester.widget<Material>(find.byType(Material));

      await tester.pumpWidget(buildSliverAppBar(false, themeElevation: 12.0));
      expect(getMaterial().elevation, 0.0);

      await tester.pumpWidget(buildSliverAppBar(true, themeElevation: 12.0));
      expect(getMaterial().elevation, 12.0);

      await tester.pumpWidget(buildSliverAppBar(true, elevation: 8.0, themeElevation: 12.0));
      expect(getMaterial().elevation, 8.0);
    });
979 980
  });

981
  testWidgets('AppBar dimensions, with and without bottom, primary', (WidgetTester tester) async {
982
    const MediaQueryData topPadding100 = MediaQueryData(padding: EdgeInsets.only(top: 100.0));
983 984

    await tester.pumpWidget(
985 986 987 988 989 990 991
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
992
        textDirection: TextDirection.ltr,
993
        child: MediaQuery(
994
          data: topPadding100,
995
          child: Scaffold(
996
            primary: false,
997
            appBar: AppBar(),
998
          ),
999 1000
        ),
      ),
1001
    ));
1002 1003 1004 1005
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight);

    await tester.pumpWidget(
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: topPadding100,
            child: Scaffold(
              primary: true,
1018 1019 1020 1021
              appBar: AppBar(
                backwardsCompatibility: false,
                title: const Text('title')
              ),
1022
          ),
1023 1024
        ),
      ),
1025
    ));
1026 1027 1028 1029 1030
    expect(appBarTop(tester), 0.0);
    expect(tester.getTopLeft(find.text('title')).dy, greaterThan(100.0));
    expect(appBarHeight(tester), kToolbarHeight + 100.0);

    await tester.pumpWidget(
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: topPadding100,
            child: Scaffold(
              primary: false,
              appBar: AppBar(
1044
                backwardsCompatibility: false,
1045 1046 1047
                bottom: PreferredSize(
                  preferredSize: const Size.fromHeight(200.0),
                  child: Container(),
1048
              ),
1049 1050 1051 1052
            ),
          ),
        ),
      ),
1053
    ));
1054 1055 1056 1057
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 200.0);

    await tester.pumpWidget(
1058 1059 1060 1061 1062 1063 1064
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
1065
        textDirection: TextDirection.ltr,
1066
        child: MediaQuery(
1067
          data: topPadding100,
1068
          child: Scaffold(
1069
            primary: true,
1070
            appBar: AppBar(
1071
              backwardsCompatibility: false,
1072
              bottom: PreferredSize(
1073
                preferredSize: const Size.fromHeight(200.0),
1074
                child: Container(),
1075
              ),
1076 1077 1078 1079
            ),
          ),
        ),
      ),
1080
    ));
1081 1082 1083 1084
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 100.0 + 200.0);

    await tester.pumpWidget(
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: topPadding100,
            child: AppBar(
1096
              backwardsCompatibility: false,
1097 1098 1099
              primary: false,
              title: const Text('title'),
            ),
1100
          ),
1101 1102 1103 1104 1105 1106
        ),
      ),
    );
    expect(appBarTop(tester), 0.0);
    expect(tester.getTopLeft(find.text('title')).dy, lessThan(100.0));
  });
1107

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
  testWidgets('AppBar in body excludes bottom SafeArea padding', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/26163
    await tester.pumpWidget(
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: const MediaQueryData(padding: EdgeInsets.symmetric(vertical: 100.0)),
          child: Scaffold(
            primary: true,
            body: Column(
              children: <Widget>[
                AppBar(
1126
                  backwardsCompatibility: false,
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
                  title: const Text('title'),
                ),
              ],
            ),
          ),
        ),
      ),
    ));
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 100.0);
  });

1139 1140
  testWidgets('AppBar updates when you add a drawer', (WidgetTester tester) async {
    await tester.pumpWidget(
1141 1142 1143
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(),
1144 1145 1146 1147 1148
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsNothing);
    await tester.pumpWidget(
1149 1150
      MaterialApp(
        home: Scaffold(
1151
          drawer: const Drawer(),
1152
          appBar: AppBar(),
1153 1154 1155 1156 1157
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsOneWidget);
  });
1158

1159 1160
  testWidgets('AppBar does not draw menu for drawer if automaticallyImplyLeading is false', (WidgetTester tester) async {
    await tester.pumpWidget(
1161 1162
      MaterialApp(
        home: Scaffold(
1163
          drawer: const Drawer(),
1164 1165 1166 1167
          appBar: AppBar(
            backwardsCompatibility: false,
            automaticallyImplyLeading: false,
          ),
1168 1169 1170 1171 1172 1173
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsNothing);
  });

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
  testWidgets('AppBar does not update the leading if a route is popped case 1', (WidgetTester tester) async {
    final Page<void> page1 = MaterialPage<void>(
      key: const ValueKey<String>('1'),
      child: Scaffold(
        key: const ValueKey<String>('1'),
        appBar: AppBar(),
      )
    );
    final Page<void> page2 = MaterialPage<void>(
        key: const ValueKey<String>('2'),
        child: Scaffold(
          key: const ValueKey<String>('2'),
          appBar: AppBar(),
        )
    );
    List<Page<void>> pages = <Page<void>>[ page1 ];
    await tester.pumpWidget(
      MaterialApp(
        home: Navigator(
          pages: pages,
          onPopPage: (Route<dynamic> route, dynamic result) => false,
        ),
      ),
    );
    expect(find.byType(BackButton), findsNothing);
    // Update pages
    pages = <Page<void>>[ page2 ];
    await tester.pumpWidget(
      MaterialApp(
        home: Navigator(
          pages: pages,
          onPopPage: (Route<dynamic> route, dynamic result) => false,
        ),
      ),
    );
    expect(find.byType(BackButton), findsNothing);
  });

  testWidgets('AppBar does not update the leading if a route is popped case 2', (WidgetTester tester) async {
    final Page<void> page1 = MaterialPage<void>(
        key: const ValueKey<String>('1'),
        child: Scaffold(
          key: const ValueKey<String>('1'),
          appBar: AppBar(),
        )
    );
    final Page<void> page2 = MaterialPage<void>(
        key: const ValueKey<String>('2'),
        child: Scaffold(
          key: const ValueKey<String>('2'),
          appBar: AppBar(),
        )
    );
    List<Page<void>> pages = <Page<void>>[ page1, page2 ];
    await tester.pumpWidget(
      MaterialApp(
        home: Navigator(
          pages: pages,
          onPopPage: (Route<dynamic> route, dynamic result) => false,
        ),
      ),
    );
    // The page2 should have a back button
    expect(
      find.descendant(
        of: find.byKey(const ValueKey<String>('2')),
        matching: find.byType(BackButton),
      ),
      findsOneWidget
    );
    // Update pages
    pages = <Page<void>>[ page1 ];
    await tester.pumpWidget(
      MaterialApp(
        home: Navigator(
          pages: pages,
          onPopPage: (Route<dynamic> route, dynamic result) => false,
        ),
      ),
    );
    await tester.pump(const Duration(milliseconds: 10));
    // The back button should persist during the pop animation.
    expect(
      find.descendant(
        of: find.byKey(const ValueKey<String>('2')),
        matching: find.byType(BackButton),
      ),
      findsOneWidget
    );
  });

1265 1266 1267 1268 1269 1270 1271
  testWidgets('AppBar ink splash draw on the correct canvas', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/58665
    final Key key = UniqueKey();
    await tester.pumpWidget(
      MaterialApp(
        home: Center(
          child: AppBar(
1272
            backwardsCompatibility: false,
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
            title: const Text('Abc'),
            actions: <Widget>[
              IconButton(
                key: key,
                icon: const Icon(Icons.add_circle),
                tooltip: 'First button',
                onPressed: () {},
              ),
            ],
            flexibleSpace: DecoratedBox(
              decoration: BoxDecoration(
                gradient: LinearGradient(
1285
                  begin: Alignment.topCenter,
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
                  end: const Alignment(-0.04, 1.0),
                  colors: <Color>[Colors.blue.shade500, Colors.blue.shade800],
                ),
              ),
            ),
          ),
        ),
      ),
    );
    final RenderObject painter = tester.renderObject(
      find.descendant(
        of: find.descendant(
          of: find.byType(AppBar),
          matching: find.byType(Stack),
        ),
        matching: find.byType(Material)
      )
    );
    await tester.tap(find.byKey(key));
    expect(painter, paints..save()..translate()..save()..translate()..circle(x: 24.0, y: 28.0));
  });

1308
  testWidgets('AppBar handles loose children 0', (WidgetTester tester) async {
1309
    final GlobalKey key = GlobalKey();
1310
    await tester.pumpWidget(
1311 1312 1313
      MaterialApp(
        home: Center(
          child: AppBar(
1314
            backwardsCompatibility: false,
1315
            leading: Placeholder(key: key),
1316
            title: const Text('Abc'),
1317
            actions: const <Widget>[
1318 1319 1320
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1321 1322 1323 1324 1325
            ],
          ),
        ),
      ),
    );
1326
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), Offset.zero);
1327 1328 1329 1330
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 1', (WidgetTester tester) async {
1331
    final GlobalKey key = GlobalKey();
1332
    await tester.pumpWidget(
1333 1334 1335
      MaterialApp(
        home: Center(
          child: AppBar(
1336
            backwardsCompatibility: false,
1337
            leading: Placeholder(key: key),
1338
            title: const Text('Abc'),
1339
            actions: const <Widget>[
1340 1341 1342
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1343
            ],
1344 1345 1346
            flexibleSpace: DecoratedBox(
              decoration: BoxDecoration(
                gradient: LinearGradient(
1347
                  begin: Alignment.topCenter,
1348
                  end: const Alignment(-0.04, 1.0),
1349 1350 1351 1352 1353 1354 1355 1356
                  colors: <Color>[Colors.blue.shade500, Colors.blue.shade800],
                ),
              ),
            ),
          ),
        ),
      ),
    );
1357
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), Offset.zero);
1358 1359 1360 1361
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 2', (WidgetTester tester) async {
1362
    final GlobalKey key = GlobalKey();
1363
    await tester.pumpWidget(
1364 1365 1366
      MaterialApp(
        home: Center(
          child: AppBar(
1367
            backwardsCompatibility: false,
1368
            leading: Placeholder(key: key),
1369
            title: const Text('Abc'),
1370
            actions: const <Widget>[
1371 1372 1373
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1374
            ],
1375 1376 1377
            flexibleSpace: DecoratedBox(
              decoration: BoxDecoration(
                gradient: LinearGradient(
1378
                  begin: Alignment.topCenter,
1379
                  end: const Alignment(-0.04, 1.0),
1380 1381 1382 1383
                  colors: <Color>[Colors.blue.shade500, Colors.blue.shade800],
                ),
              ),
            ),
1384
            bottom: PreferredSize(
1385
              preferredSize: const Size(0.0, kToolbarHeight),
1386
              child: Container(
1387 1388 1389 1390
                height: 50.0,
                padding: const EdgeInsets.all(4.0),
                child: const Placeholder(
                  strokeWidth: 2.0,
1391
                  color: Color(0xFFFFFFFF),
1392 1393 1394 1395 1396 1397 1398
                ),
              ),
            ),
          ),
        ),
      ),
    );
1399
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), Offset.zero);
1400 1401 1402 1403
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 3', (WidgetTester tester) async {
1404
    final GlobalKey key = GlobalKey();
1405
    await tester.pumpWidget(
1406 1407 1408
      MaterialApp(
        home: Center(
          child: AppBar(
1409
            backwardsCompatibility: false,
1410
            leading: Placeholder(key: key),
1411
            title: const Text('Abc'),
1412
            actions: const <Widget>[
1413 1414 1415
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1416
            ],
1417
            bottom: PreferredSize(
1418
              preferredSize: const Size(0.0, kToolbarHeight),
1419
              child: Container(
1420 1421 1422 1423
                height: 50.0,
                padding: const EdgeInsets.all(4.0),
                child: const Placeholder(
                  strokeWidth: 2.0,
1424
                  color: Color(0xFFFFFFFF),
1425 1426 1427 1428 1429 1430 1431
                ),
              ),
            ),
          ),
        ),
      ),
    );
1432
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), Offset.zero);
1433 1434
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });
Ian Hickson's avatar
Ian Hickson committed
1435 1436

  testWidgets('AppBar positioning of leading and trailing widgets with top padding', (WidgetTester tester) async {
1437
    const MediaQueryData topPadding100 = MediaQueryData(padding: EdgeInsets.only(top: 100));
Ian Hickson's avatar
Ian Hickson committed
1438

1439 1440 1441
    final Key leadingKey = UniqueKey();
    final Key titleKey = UniqueKey();
    final Key trailingKey = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
1442 1443

    await tester.pumpWidget(
1444 1445 1446 1447 1448 1449 1450
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
Ian Hickson's avatar
Ian Hickson committed
1451
        textDirection: TextDirection.rtl,
1452
        child: MediaQuery(
Ian Hickson's avatar
Ian Hickson committed
1453
          data: topPadding100,
1454
          child: Scaffold(
Ian Hickson's avatar
Ian Hickson committed
1455
            primary: false,
1456
            appBar: AppBar(
1457
              backwardsCompatibility: false,
1458 1459 1460
              leading: Placeholder(key: leadingKey), // Forced to 56x56, see _kLeadingWidth in app_bar.dart.
              title: Placeholder(key: titleKey, fallbackHeight: kToolbarHeight),
              actions: <Widget>[ Placeholder(key: trailingKey, fallbackWidth: 10) ],
Ian Hickson's avatar
Ian Hickson committed
1461 1462 1463 1464
            ),
          ),
        ),
      ),
1465
    ));
1466
    expect(tester.getTopLeft(find.byType(AppBar)), Offset.zero);
1467 1468 1469 1470
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(800.0 - 56.0, 100));
    expect(tester.getTopLeft(find.byKey(trailingKey)), const Offset(0.0, 100));

    // Because the topPadding eliminates the vertical space for the
1471
    // NavigationToolbar within the AppBar, the toolbar is constrained
1472 1473 1474 1475 1476 1477
    // with minHeight=maxHeight=0. The _AppBarTitle widget vertically centers
    // the title, so its Y coordinate relative to the toolbar is -kToolbarHeight / 2
    // (-28). The top of the toolbar is at (screen coordinates) y=100, so the
    // top of the title is 100 + -28 = 72. The toolbar clips its contents
    // so the title isn't actually visible.
    expect(tester.getTopLeft(find.byKey(titleKey)), const Offset(10 + NavigationToolbar.kMiddleSpacing, 72));
Ian Hickson's avatar
Ian Hickson committed
1478 1479 1480
  });

  testWidgets('SliverAppBar positioning of leading and trailing widgets with top padding', (WidgetTester tester) async {
1481
    const MediaQueryData topPadding100 = MediaQueryData(padding: EdgeInsets.only(top: 100.0));
Ian Hickson's avatar
Ian Hickson committed
1482

1483 1484 1485
    final Key leadingKey = UniqueKey();
    final Key titleKey = UniqueKey();
    final Key trailingKey = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
1486 1487

    await tester.pumpWidget(
1488 1489 1490 1491 1492 1493 1494
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
Ian Hickson's avatar
Ian Hickson committed
1495
        textDirection: TextDirection.rtl,
1496
        child: MediaQuery(
Ian Hickson's avatar
Ian Hickson committed
1497
          data: topPadding100,
1498
          child: CustomScrollView(
Ian Hickson's avatar
Ian Hickson committed
1499 1500
            primary: true,
            slivers: <Widget>[
1501
              SliverAppBar(
1502
                backwardsCompatibility: false,
1503
                leading: Placeholder(key: leadingKey),
1504
                title: Placeholder(key: titleKey, fallbackHeight: kToolbarHeight),
1505
                actions: <Widget>[ Placeholder(key: trailingKey) ],
Ian Hickson's avatar
Ian Hickson committed
1506 1507 1508 1509 1510
              ),
            ],
          ),
        ),
      ),
1511
    ));
1512
    expect(tester.getTopLeft(find.byType(AppBar)), Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
1513
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(800.0 - 56.0, 100.0));
1514 1515
    expect(tester.getTopLeft(find.byKey(titleKey)), const Offset(416.0, 100.0));
    expect(tester.getTopLeft(find.byKey(trailingKey)), const Offset(0.0, 100.0));
Ian Hickson's avatar
Ian Hickson committed
1516
  });
1517 1518

  testWidgets('SliverAppBar positioning of leading and trailing widgets with bottom padding', (WidgetTester tester) async {
1519
    const MediaQueryData topPadding100 = MediaQueryData(padding: EdgeInsets.only(top: 100.0, bottom: 50.0));
1520

1521 1522 1523
    final Key leadingKey = UniqueKey();
    final Key titleKey = UniqueKey();
    final Key trailingKey = UniqueKey();
1524 1525

    await tester.pumpWidget(
1526 1527 1528 1529 1530 1531 1532
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
1533
        textDirection: TextDirection.rtl,
1534
        child: MediaQuery(
1535
          data: topPadding100,
1536
          child: CustomScrollView(
1537 1538
            primary: true,
            slivers: <Widget>[
1539
              SliverAppBar(
1540
                backwardsCompatibility: false,
1541 1542 1543
                leading: Placeholder(key: leadingKey),
                title: Placeholder(key: titleKey),
                actions: <Widget>[ Placeholder(key: trailingKey) ],
1544 1545 1546 1547 1548
              ),
            ],
          ),
        ),
      ),
1549
    ));
Dan Field's avatar
Dan Field committed
1550 1551 1552
    expect(tester.getRect(find.byType(AppBar)), const Rect.fromLTRB(0.0, 0.0, 800.00, 100.0 + 56.0));
    expect(tester.getRect(find.byKey(leadingKey)), const Rect.fromLTRB(800.0 - 56.0, 100.0, 800.0, 100.0 + 56.0));
    expect(tester.getRect(find.byKey(trailingKey)), const Rect.fromLTRB(0.0, 100.0, 400.0, 100.0 + 56.0));
1553
  });
1554 1555

  testWidgets('SliverAppBar provides correct semantics in LTR', (WidgetTester tester) async {
1556
    final SemanticsTester semantics = SemanticsTester(tester);
1557 1558

    await tester.pumpWidget(
1559 1560 1561
      MaterialApp(
        home: Center(
          child: AppBar(
1562
            backwardsCompatibility: false,
1563 1564 1565
            leading: const Text('Leading'),
            title: const Text('Title'),
            actions: const <Widget>[
1566 1567 1568
              Text('Action 1'),
              Text('Action 2'),
              Text('Action 3'),
1569 1570
            ],
            bottom: const PreferredSize(
1571 1572
              preferredSize: Size(0.0, kToolbarHeight),
              child: Text('Bottom'),
1573 1574 1575 1576 1577 1578 1579
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
1580
      TestSemantics.root(
1581
        children: <TestSemantics>[
1582
          TestSemantics(
1583
            children: <TestSemantics>[
1584
              TestSemantics(
1585
                children: <TestSemantics> [
1586
                  TestSemantics(
1587
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1588
                    children: <TestSemantics>[
1589
                      TestSemantics(
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'Leading',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            flags: <SemanticsFlag>[
                              SemanticsFlag.namesRoute,
                              SemanticsFlag.isHeader,
                            ],
                            label: 'Title',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            label: 'Action 1',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            label: 'Action 2',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            label: 'Action 3',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            label: 'Bottom',
                            textDirection: TextDirection.ltr,
                          ),
1619
                        ],
1620 1621
                      ),
                    ],
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });

  testWidgets('SliverAppBar provides correct semantics in RTL', (WidgetTester tester) async {
1638
    final SemanticsTester semantics = SemanticsTester(tester);
1639 1640

    await tester.pumpWidget(
1641 1642
      MaterialApp(
        home: Semantics(
1643
          textDirection: TextDirection.rtl,
1644
          child: Directionality(
1645
            textDirection: TextDirection.rtl,
1646 1647
            child: Center(
              child: AppBar(
1648
                backwardsCompatibility: false,
1649 1650 1651
                leading: const Text('Leading'),
                title: const Text('Title'),
                actions: const <Widget>[
1652 1653 1654
                  Text('Action 1'),
                  Text('Action 2'),
                  Text('Action 3'),
1655 1656
                ],
                bottom: const PreferredSize(
1657 1658
                  preferredSize: Size(0.0, kToolbarHeight),
                  child: Text('Bottom'),
1659 1660 1661 1662 1663 1664 1665 1666 1667
                ),
              ),
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
1668
      TestSemantics.root(
1669
        children: <TestSemantics>[
1670
          TestSemantics(
1671
            children: <TestSemantics>[
1672
              TestSemantics(
1673
                children: <TestSemantics>[
1674
                  TestSemantics(
1675
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1676
                    children: <TestSemantics>[
1677
                      TestSemantics(
1678
                        textDirection: TextDirection.rtl,
1679
                        children: <TestSemantics>[
1680
                          TestSemantics(
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
                            children: <TestSemantics>[
                              TestSemantics(
                                label: 'Leading',
                                textDirection: TextDirection.rtl,
                              ),
                              TestSemantics(
                                flags: <SemanticsFlag>[
                                  SemanticsFlag.namesRoute,
                                  SemanticsFlag.isHeader,
                                ],
                                label: 'Title',
                                textDirection: TextDirection.rtl,
                              ),
                              TestSemantics(
                                label: 'Action 1',
                                textDirection: TextDirection.rtl,
                              ),
                              TestSemantics(
                                label: 'Action 2',
                                textDirection: TextDirection.rtl,
                              ),
                              TestSemantics(
                                label: 'Action 3',
                                textDirection: TextDirection.rtl,
                              ),
                              TestSemantics(
                                label: 'Bottom',
                                textDirection: TextDirection.rtl,
                              ),
1710
                            ],
1711 1712
                          ),
                        ],
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });
1729

1730 1731 1732 1733 1734 1735 1736
  testWidgets('AppBar excludes header semantics correctly', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(
      MaterialApp(
        home: Center(
          child: AppBar(
1737
            backwardsCompatibility: false,
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
            leading: const Text('Leading'),
            title: const ExcludeSemantics(child: Text('Title')),
            excludeHeaderSemantics: true,
            actions: const <Widget>[
              Text('Action 1'),
            ],
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
      TestSemantics.root(
        children: <TestSemantics>[
          TestSemantics(
            children: <TestSemantics>[
              TestSemantics(
                children: <TestSemantics>[
                  TestSemantics(
1757
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1758 1759
                    children: <TestSemantics>[
                      TestSemantics(
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'Leading',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            label: 'Action 1',
                            textDirection: TextDirection.ltr,
                          ),
                        ],
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });

  testWidgets('SliverAppBar excludes header semantics correctly', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(
      const MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
1795
              backwardsCompatibility: false,
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
              leading: Text('Leading'),
              flexibleSpace: ExcludeSemantics(child: Text('Title')),
              actions: <Widget>[Text('Action 1')],
              excludeHeaderSemantics: true,
            ),
          ],
        ),
      ),
    );

    expect(semantics, hasSemantics(
      TestSemantics.root(
        children: <TestSemantics>[
          TestSemantics(
            textDirection: TextDirection.ltr,
            children: <TestSemantics>[
              TestSemantics(
                children: <TestSemantics>[
                  TestSemantics(
1815
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1816 1817 1818 1819 1820 1821
                    children: <TestSemantics>[
                      TestSemantics(
                        children: <TestSemantics>[
                          TestSemantics(
                            children: <TestSemantics>[
                              TestSemantics(
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
                                children: <TestSemantics>[
                                  TestSemantics(
                                    label: 'Leading',
                                    textDirection: TextDirection.ltr,
                                  ),
                                  TestSemantics(
                                    label: 'Action 1',
                                    textDirection: TextDirection.ltr,
                                  ),
                                ],
                              ),
                              TestSemantics(),
                            ],
                          ),
                          TestSemantics(
                            flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });

  testWidgets('SliverAppBar with flexable space has correct semantics order', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/64922.
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(
      const MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
1866
              backwardsCompatibility: false,
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
              leading: Text('Leading'),
              flexibleSpace: Text('Flexible space'),
              actions: <Widget>[Text('Action 1')],
            ),
          ],
        ),
      ),
    );

    expect(semantics, hasSemantics(
      TestSemantics.root(
        children: <TestSemantics>[
          TestSemantics(
            textDirection: TextDirection.ltr,
            children: <TestSemantics>[
              TestSemantics(
                children: <TestSemantics>[
                  TestSemantics(
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                    children: <TestSemantics>[
                      TestSemantics(
                        children: <TestSemantics>[
                          TestSemantics(
                            children: <TestSemantics>[
                              TestSemantics(
                                children: <TestSemantics>[
                                  TestSemantics(
                                    label: 'Leading',
                                    textDirection: TextDirection.ltr,
                                  ),
                                  TestSemantics(
                                    label: 'Action 1',
                                    textDirection: TextDirection.ltr,
                                  ),
                                ],
1902 1903
                              ),
                              TestSemantics(
1904 1905 1906 1907 1908 1909 1910
                                children: <TestSemantics>[
                                  TestSemantics(
                                    flags: <SemanticsFlag>[SemanticsFlag.isHeader],
                                    label: 'Flexible space',
                                    textDirection: TextDirection.ltr,
                                  ),
                                ],
1911 1912 1913
                              ),
                            ],
                          ),
1914 1915 1916
                          TestSemantics(
                            flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
                          ),
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });

1935
  testWidgets('AppBar draws a light system bar for a dark background', (WidgetTester tester) async {
1936 1937
    final ThemeData darkTheme = ThemeData.dark();
    await tester.pumpWidget(MaterialApp(
1938 1939
      theme: darkTheme,
      home: Scaffold(
1940 1941 1942 1943
        appBar: AppBar(
          backwardsCompatibility: false,
          title: const Text('test')
        ),
1944 1945 1946 1947
      ),
    ));

    expect(darkTheme.primaryColorBrightness, Brightness.dark);
1948
    expect(darkTheme.colorScheme.brightness, Brightness.dark);
1949 1950 1951 1952 1953 1954 1955
    expect(SystemChrome.latestStyle, const SystemUiOverlayStyle(
      statusBarBrightness: Brightness.dark,
      statusBarIconBrightness: Brightness.light,
    ));
  });

  testWidgets('AppBar draws a dark system bar for a light background', (WidgetTester tester) async {
1956
    final ThemeData lightTheme = ThemeData(primaryColor: Colors.white);
1957 1958 1959 1960 1961 1962 1963 1964 1965
    await tester.pumpWidget(
      MaterialApp(
        theme: lightTheme,
        home: Scaffold(
          appBar: AppBar(
            backwardsCompatibility: false,
            title: const Text('test')
          ),
        ),
1966
      ),
1967
    );
1968 1969

    expect(lightTheme.primaryColorBrightness, Brightness.light);
1970
    expect(lightTheme.colorScheme.brightness, Brightness.light);
1971 1972 1973 1974 1975
    expect(SystemChrome.latestStyle, const SystemUiOverlayStyle(
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
    ));
  });
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989

  testWidgets('Changing SliverAppBar snap from true to false', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/17598
    const double appBarHeight = 256.0;
    bool snap = true;

    await tester.pumpWidget(
      MaterialApp(
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              body: CustomScrollView(
                slivers: <Widget>[
                  SliverAppBar(
1990
                    backwardsCompatibility: false,
1991 1992 1993 1994 1995
                    expandedHeight: appBarHeight,
                    pinned: false,
                    floating: true,
                    snap: snap,
                    actions: <Widget>[
1996
                      TextButton(
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
                        child: const Text('snap=false'),
                        onPressed: () {
                          setState(() {
                            snap = false;
                          });
                        },
                      ),
                    ],
                    flexibleSpace: FlexibleSpaceBar(
                      background: Container(
                        height: appBarHeight,
                        color: Colors.orange,
                      ),
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildListDelegate(
                      <Widget>[
                        Container(height: 1200.0, color: Colors.teal),
                      ],
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );

    TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
    await gesture.moveBy(const Offset(0.0, -100.0));
    await gesture.up();

    await tester.tap(find.text('snap=false'));
    await tester.pumpAndSettle();

    gesture = await tester.startGesture(const Offset(50.0, 400.0));
    await gesture.moveBy(const Offset(0.0, -100.0));
    await gesture.up();
    await tester.pump();
  });
2039

2040 2041 2042 2043
  testWidgets('AppBar shape default', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: AppBar(
2044
          backwardsCompatibility: false,
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
          leading: const Text('L'),
          title: const Text('No Scaffold'),
          actions: const <Widget>[Text('A1'), Text('A2')],
        ),
      ),
    );

    final Finder appBarFinder = find.byType(AppBar);
    AppBar getAppBarWidget(Finder finder) => tester.widget<AppBar>(finder);
    expect(getAppBarWidget(appBarFinder).shape, null);

    final Finder materialFinder = find.byType(Material);
    Material getMaterialWidget(Finder finder) => tester.widget<Material>(finder);
    expect(getMaterialWidget(materialFinder).shape, null);
  });

2061
  testWidgets('AppBar with shape', (WidgetTester tester) async {
2062 2063 2064
    const RoundedRectangleBorder roundedRectangleBorder = RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(15.0))
    );
2065 2066 2067
    await tester.pumpWidget(
      MaterialApp(
        home: AppBar(
2068
          backwardsCompatibility: false,
2069 2070 2071
          leading: const Text('L'),
          title: const Text('No Scaffold'),
          actions: const <Widget>[Text('A1'), Text('A2')],
2072
          shape: roundedRectangleBorder,
2073 2074 2075 2076 2077
        ),
      ),
    );

    final Finder appBarFinder = find.byType(AppBar);
2078 2079
    AppBar getAppBarWidget(Finder finder) => tester.widget<AppBar>(finder);
    expect(getAppBarWidget(appBarFinder).shape, roundedRectangleBorder);
2080

2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
    final Finder materialFinder = find.byType(Material);
    Material getMaterialWidget(Finder finder) => tester.widget<Material>(finder);
    expect(getMaterialWidget(materialFinder).shape, roundedRectangleBorder);
  });

  testWidgets('SliverAppBar shape default', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
2092
              backwardsCompatibility: false,
2093 2094 2095 2096 2097 2098 2099 2100
              leading: Text('L'),
              title: Text('No Scaffold'),
              actions: <Widget>[Text('A1'), Text('A2')],
            ),
          ],
        ),
      ),
    );
2101

2102 2103 2104
    final Finder sliverAppBarFinder = find.byType(SliverAppBar);
    SliverAppBar getSliverAppBarWidget(Finder finder) => tester.widget<SliverAppBar>(finder);
    expect(getSliverAppBarWidget(sliverAppBarFinder).shape, null);
2105 2106

    final Finder materialFinder = find.byType(Material);
2107 2108 2109
    Material getMaterialWidget(Finder finder) => tester.widget<Material>(finder);
    expect(getMaterialWidget(materialFinder).shape, null);
  });
2110

2111 2112 2113 2114 2115 2116 2117 2118 2119
  testWidgets('SliverAppBar with shape', (WidgetTester tester) async {
    const RoundedRectangleBorder roundedRectangleBorder = RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(15.0)),
    );
    await tester.pumpWidget(
      const MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
2120
              backwardsCompatibility: false,
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
              leading: Text('L'),
              title: Text('No Scaffold'),
              actions: <Widget>[Text('A1'), Text('A2')],
              shape: roundedRectangleBorder,
            ),
          ],
        ),
      ),
    );

    final Finder sliverAppBarFinder = find.byType(SliverAppBar);
    SliverAppBar getSliverAppBarWidget(Finder finder) => tester.widget<SliverAppBar>(finder);
    expect(getSliverAppBarWidget(sliverAppBarFinder).shape, roundedRectangleBorder);

    final Finder materialFinder = find.byType(Material);
    Material getMaterialWidget(Finder finder) => tester.widget<Material>(finder);
    expect(getMaterialWidget(materialFinder).shape, roundedRectangleBorder);
2138
  });
2139

2140
  testWidgets('AppBars title has upper limit on text scaling, textScaleFactor = 1, 1.34, 2', (WidgetTester tester) async {
2141
    late double textScaleFactor;
2142 2143 2144 2145 2146

    Widget buildFrame() {
      return MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
2147
            return MediaQuery(
2148
              data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
2149 2150
              child: Scaffold(
                appBar: AppBar(
2151
                  backwardsCompatibility: false,
2152 2153
                  centerTitle: false,
                  title: const Text('Jumbo', style: TextStyle(fontSize: 18)),
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
                ),
              ),
            );
          },
        ),
      );
    }

    final Finder appBarTitle = find.text('Jumbo');

    textScaleFactor = 1;
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle).height, 18);

    textScaleFactor = 1.34;
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle).height, 24);

    textScaleFactor = 2;
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle).height, 24);
  });

2177
  testWidgets('AppBars with jumbo titles, textScaleFactor = 3, 3.5, 4', (WidgetTester tester) async {
2178 2179 2180
    double textScaleFactor = 1.0;
    TextDirection textDirection = TextDirection.ltr;
    bool centerTitle = false;
2181 2182 2183 2184 2185 2186 2187

    Widget buildFrame() {
      return MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            return Directionality(
              textDirection: textDirection,
2188 2189 2190 2191
              child: Builder(
                builder: (BuildContext context) {
                  return Scaffold(
                    appBar: AppBar(
2192
                      backwardsCompatibility: false,
2193 2194
                      centerTitle: centerTitle,
                      title: MediaQuery(
2195
                        data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
2196
                        child: const Text('Jumbo'),
2197
                      ),
2198 2199 2200
                    ),
                  );
                },
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
              ),
            );
          },
        ),
      );
    }

    final Finder appBarTitle = find.text('Jumbo');
    final Finder toolbar = find.byType(NavigationToolbar);

    // Overall screen size is 800x600
    // Left or right justified title is padded by 16 on the "start" side.
    // Toolbar height is 56.
2214
    // "Jumbo" title is 100x20.
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

    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle), const Rect.fromLTRB(16, 18, 116, 38));
    expect(tester.getCenter(appBarTitle).dy, tester.getCenter(toolbar).dy);

    textScaleFactor = 3; // "Jumbo" title is 300x60.
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle), const Rect.fromLTRB(16, -2, 316, 58));
    expect(tester.getCenter(appBarTitle).dy, tester.getCenter(toolbar).dy);

    textScaleFactor = 3.5; // "Jumbo" title is 350x70.
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle), const Rect.fromLTRB(16, -7, 366, 63));
    expect(tester.getCenter(appBarTitle).dy, tester.getCenter(toolbar).dy);

    textScaleFactor = 4; // "Jumbo" title is 400x80.
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle), const Rect.fromLTRB(16, -12, 416, 68));
    expect(tester.getCenter(appBarTitle).dy, tester.getCenter(toolbar).dy);

    textDirection = TextDirection.rtl; // Changed to rtl. "Jumbo" title is still 400x80.
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle), const Rect.fromLTRB(800.0 - 400.0 - 16.0, -12, 800.0 - 16.0, 68));
    expect(tester.getCenter(appBarTitle).dy, tester.getCenter(toolbar).dy);

    centerTitle = true; // Changed to true. "Jumbo" title is still 400x80.
    await tester.pumpWidget(buildFrame());
    expect(tester.getRect(appBarTitle), const Rect.fromLTRB(200, -12, 800.0 - 200.0, 68));
    expect(tester.getCenter(appBarTitle).dy, tester.getCenter(toolbar).dy);
  });
2245

2246
  testWidgets('SliverAppBar configures the delegate properly', (WidgetTester tester) async {
2247
    Future<void> buildAndVerifyDelegate({ required bool pinned, required bool floating, required bool snap }) async {
2248 2249 2250 2251 2252
      await tester.pumpWidget(
        MaterialApp(
          home: CustomScrollView(
            slivers: <Widget>[
              SliverAppBar(
2253
                backwardsCompatibility: false,
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
                title: const Text('Jumbo'),
                pinned: pinned,
                floating: floating,
                snap: snap,
              ),
            ],
          ),
        ),
      );

      final SliverPersistentHeaderDelegate delegate = tester
        .widget<SliverPersistentHeader>(find.byType(SliverPersistentHeader))
        .delegate;

      // Ensure we have a non-null vsync when it's needed.
      if (!floating || (delegate.snapConfiguration == null && delegate.showOnScreenConfiguration == null))
        expect(delegate.vsync, isNotNull);

      expect(delegate.showOnScreenConfiguration != null, snap && floating);
    }

    await buildAndVerifyDelegate(pinned: false, floating: true, snap: false);
    await buildAndVerifyDelegate(pinned: false, floating: true, snap: true);

    await buildAndVerifyDelegate(pinned: true, floating: true, snap: false);
    await buildAndVerifyDelegate(pinned: true, floating: true, snap: true);
  });

2282 2283 2284 2285 2286
  testWidgets('AppBar respects toolbarHeight', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
2287
            backwardsCompatibility: false,
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
            title: const Text('Title'),
            toolbarHeight: 48,
          ),
          body: Container(),
        ),
      )
    );

    expect(appBarHeight(tester), 48);
  });

  testWidgets('SliverAppBar default collapsedHeight with respect to toolbarHeight', (WidgetTester tester) async {
    const double toolbarHeight = 100.0;

    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: false,
      toolbarHeight: toolbarHeight,
    ));

    final ScrollController controller = primaryScrollController(tester);
    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar out of view, to its collapsed height.
    controller.jumpTo(300.0);
    await tester.pump();
    expect(find.byType(SliverAppBar), findsNothing);
    // By default, the collapsedHeight is toolbarHeight + bottom.preferredSize.height,
    // in this case initialTabBarHeight.
    expect(appBarHeight(tester), toolbarHeight + initialTabBarHeight);
  });

  testWidgets('SliverAppBar collapsedHeight with toolbarHeight', (WidgetTester tester) async {
    const double toolbarHeight = 100.0;
    const double collapsedHeight = 150.0;

    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: false,
      toolbarHeight: toolbarHeight,
      collapsedHeight: collapsedHeight
    ));

    final ScrollController controller = primaryScrollController(tester);
    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar out of view, to its collapsed height.
    controller.jumpTo(300.0);
    await tester.pump();
    expect(find.byType(SliverAppBar), findsNothing);
    expect(appBarHeight(tester), collapsedHeight + initialTabBarHeight);
  });

2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
  testWidgets('SliverAppBar collapsedHeight', (WidgetTester tester) async {
    const double collapsedHeight = 56.0;

    await tester.pumpWidget(buildSliverAppBarApp(
        floating: false,
        pinned: false,
        collapsedHeight: collapsedHeight
    ));

    final ScrollController controller = primaryScrollController(tester);
    final double initialTabBarHeight = tabBarHeight(tester);

    // Scroll the not-pinned appbar out of view, to its collapsed height.
    controller.jumpTo(300.0);
    await tester.pump();
    expect(find.byType(SliverAppBar), findsNothing);
    expect(appBarHeight(tester), collapsedHeight + initialTabBarHeight);
  });

2360 2361 2362 2363 2364
  testWidgets('AppBar respects leadingWidth', (WidgetTester tester) async {
    const Key key = Key('leading');
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        appBar: AppBar(
2365
          backwardsCompatibility: false,
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
          leading: const Placeholder(key: key),
          leadingWidth: 100,
          title: const Text('Title'),
        ),
      ),
    ));

    // By default toolbarHeight is 56.0.
    expect(tester.getRect(find.byKey(key)), const Rect.fromLTRB(0, 0, 100, 56));
  });

  testWidgets('SliverAppBar respects leadingWidth', (WidgetTester tester) async {
    const Key key = Key('leading');
    await tester.pumpWidget( const MaterialApp(
      home: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
2383
            backwardsCompatibility: false,
2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
            leading: Placeholder(key: key),
            leadingWidth: 100,
            title: Text('Title'),
          ),
        ],
      )
    ));

    // By default toolbarHeight is 56.0.
    expect(tester.getRect(find.byKey(key)), const Rect.fromLTRB(0, 0, 100, 56));
  });
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411

  testWidgets("AppBar with EndDrawer doesn't have leading", (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        endDrawer: const Drawer(),
      ),
    ));

    final Finder endDrawerFinder = find.byTooltip('Open navigation menu');
    await tester.tap(endDrawerFinder);
    await tester.pump();

    final Finder appBarFinder = find.byType(NavigationToolbar);
    NavigationToolbar getAppBarWidget(Finder finder) => tester.widget<NavigationToolbar>(finder);
    expect(getAppBarWidget(appBarFinder).leading, null);
  });
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434

  testWidgets('AppBar.titleSpacing defaults to NavigationToolbar.kMiddleSpacing', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Title'),
        ),
      ),
    ));

    final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
    expect(navToolBar.middleSpacing, NavigationToolbar.kMiddleSpacing);
  });

  testWidgets('SliverAppBar.titleSpacing defaults to NavigationToolbar.kMiddleSpacing', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: false,
    ));

    final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
    expect(navToolBar.middleSpacing, NavigationToolbar.kMiddleSpacing);
  });
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520

  testWidgets('AppBar foregroundColor and backgroundColor', (WidgetTester tester) async {
    const Color foregroundColor = Color(0xff00ff00);
    const Color backgroundColor = Color(0xff00ffff);
    final Key leadingIconKey = UniqueKey();
    final Key actionIconKey = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            backwardsCompatibility: false,
            foregroundColor: foregroundColor,
            backgroundColor: backgroundColor,
            leading: Icon(Icons.add_circle, key: leadingIconKey),
            title: const Text('title'),
            actions: <Widget>[Icon(Icons.add_circle, key: actionIconKey), const Text('action')],
          ),
        ),
      ),
    );

    final Material appBarMaterial = tester.widget<Material>(
      find.descendant(
        of: find.byType(AppBar),
        matching: find.byType(Material),
      ),
    );
    expect(appBarMaterial.color, backgroundColor);

    final TextStyle titleTextStyle = tester.widget<DefaultTextStyle>(
      find.ancestor(of: find.text('title'), matching: find.byType(DefaultTextStyle)).first,
    ).style;
    expect(titleTextStyle.color, foregroundColor);

    final IconThemeData leadingIconTheme = tester.widget<IconTheme>(
      find.ancestor(of: find.byKey(leadingIconKey), matching: find.byType(IconTheme)).first,
    ).data;
    expect(leadingIconTheme.color, foregroundColor);

    final IconThemeData actionIconTheme = tester.widget<IconTheme>(
      find.ancestor(of: find.byKey(actionIconKey), matching: find.byType(IconTheme)).first,
    ).data;
    expect(actionIconTheme.color, foregroundColor);
  });


  testWidgets('AppBarTheme.backwardsCompatibility', (WidgetTester tester) async {
    const Color foregroundColor = Color(0xff00ff00);
    final Key leadingIconKey = UniqueKey();
    final Key actionIconKey = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData.light().copyWith(
          appBarTheme: const AppBarTheme(
            backwardsCompatibility: false,
          ),
        ),
        home: Scaffold(
          appBar: AppBar(
            foregroundColor: foregroundColor, // only applies if backwardsCompatibility is false
            leading: Icon(Icons.add_circle, key: leadingIconKey),
            title: const Text('title'),
            actions: <Widget>[Icon(Icons.add_circle, key: actionIconKey), const Text('action')],
          ),
        ),
      ),
    );

    final TextStyle titleTextStyle = tester.widget<DefaultTextStyle>(
      find.ancestor(of: find.text('title'), matching: find.byType(DefaultTextStyle)).first,
    ).style;
    expect(titleTextStyle.color, foregroundColor);

    final IconThemeData leadingIconTheme = tester.widget<IconTheme>(
      find.ancestor(of: find.byKey(leadingIconKey), matching: find.byType(IconTheme)).first,
    ).data;
    expect(leadingIconTheme.color, foregroundColor);

    final IconThemeData actionIconTheme = tester.widget<IconTheme>(
      find.ancestor(of: find.byKey(actionIconKey), matching: find.byType(IconTheme)).first,
    ).data;
    expect(actionIconTheme.color, foregroundColor);
  });

2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
  testWidgets('SliverAppBar.backgroundColor MaterialStateColor scrolledUnder', (WidgetTester tester) async {
    const double collapsedHeight = kToolbarHeight;
    const double expandedHeight = 200.0;
    const Color scrolledColor = Color(0xff00ff00);
    const Color defaultColor = Color(0xff0000ff);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: CustomScrollView(
            slivers: <Widget>[
              SliverAppBar(
                backwardsCompatibility: false,
                elevation: 0,
                backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
                  return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
                }),
                expandedHeight: expandedHeight,
                pinned: true,
              ),
              SliverList(
                delegate: SliverChildListDelegate(
                    <Widget>[
                      Container(height: 1200.0, color: Colors.teal),
                    ],
                ),
              ),
            ],
          ),
        ),
      ),
    );

    Finder findAppBarMaterial() {
      return find.descendant(of: find.byType(AppBar), matching: find.byType(Material));
    }

    Color? getAppBarBackgroundColor() {
      return tester.widget<Material>(findAppBarMaterial()).color;
    }

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);

    TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
    await gesture.moveBy(const Offset(0.0, -expandedHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), scrolledColor);
    expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight);

    gesture = await tester.startGesture(const Offset(50.0, 300.0));
    await gesture.moveBy(const Offset(0.0, expandedHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
  });

  testWidgets('SliverAppBar.backgroundColor with FlexibleSpace MaterialStateColor scrolledUnder', (WidgetTester tester) async {
    const double collapsedHeight = kToolbarHeight;
    const double expandedHeight = 200.0;
    const Color scrolledColor = Color(0xff00ff00);
    const Color defaultColor = Color(0xff0000ff);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: CustomScrollView(
            slivers: <Widget>[
              SliverAppBar(
                backwardsCompatibility: false,
                elevation: 0,
                backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
                  return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
                }),
                expandedHeight: expandedHeight,
                pinned: true,
                flexibleSpace: const FlexibleSpaceBar(
                  title: Text('SliverAppBar'),
                ),
              ),
              SliverList(
                delegate: SliverChildListDelegate(
                    <Widget>[
                      Container(height: 1200.0, color: Colors.teal),
                    ],
                ),
              ),
            ],
          ),
        ),
      ),
    );

    Finder findAppBarMaterial() {
      // There are 2 Material widgets below AppBar. The second is only added if
      // flexibleSpace is non-null.
      return find.descendant(of: find.byType(AppBar), matching: find.byType(Material)).first;
    }

    Color? getAppBarBackgroundColor() {
      return tester.widget<Material>(findAppBarMaterial()).color;
    }

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);

    TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
    await gesture.moveBy(const Offset(0.0, -expandedHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), scrolledColor);
    expect(tester.getSize(findAppBarMaterial()).height, collapsedHeight);

    gesture = await tester.startGesture(const Offset(50.0, 300.0));
    await gesture.moveBy(const Offset(0.0, expandedHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, expandedHeight);
  });

  testWidgets('AppBar.backgroundColor MaterialStateColor scrolledUnder', (WidgetTester tester) async {
    const Color scrolledColor = Color(0xff00ff00);
    const Color defaultColor = Color(0xff0000ff);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            backwardsCompatibility: false,
            elevation: 0,
            backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
              return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
            }),
            title: const Text('AppBar'),
          ),
          body: ListView(
            children: <Widget>[
              Container(height: 1200.0, color: Colors.teal),
            ],
          ),
        ),
      ),
    );

    Finder findAppBarMaterial() {
      return find.descendant(of: find.byType(AppBar), matching: find.byType(Material));
    }

    Color? getAppBarBackgroundColor() {
      return tester.widget<Material>(findAppBarMaterial()).color;
    }

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);

    TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
    await gesture.moveBy(const Offset(0.0, -kToolbarHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), scrolledColor);
    expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);

    gesture = await tester.startGesture(const Offset(50.0, 300.0));
    await gesture.moveBy(const Offset(0.0, kToolbarHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
  });

  testWidgets('AppBar.backgroundColor with FlexibleSpace MaterialStateColor scrolledUnder', (WidgetTester tester) async {
    const Color scrolledColor = Color(0xff00ff00);
    const Color defaultColor = Color(0xff0000ff);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            backwardsCompatibility: false,
            elevation: 0,
            backgroundColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
              return states.contains(MaterialState.scrolledUnder) ? scrolledColor : defaultColor;
            }),
            title: const Text('AppBar'),
            flexibleSpace: const FlexibleSpaceBar(
              title: Text('FlexibleSpace'),
            ),
          ),
          body: ListView(
            children: <Widget>[
              Container(height: 1200.0, color: Colors.teal),
            ],
          ),
        ),
      ),
    );

    Finder findAppBarMaterial() {
      // There are 2 Material widgets below AppBar. The second is only added if
      // flexibleSpace is non-null.
      return find.descendant(of: find.byType(AppBar), matching: find.byType(Material)).first;
    }

    Color? getAppBarBackgroundColor() {
      return tester.widget<Material>(findAppBarMaterial()).color;
    }

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);

    TestGesture gesture = await tester.startGesture(const Offset(50.0, 400.0));
    await gesture.moveBy(const Offset(0.0, -kToolbarHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), scrolledColor);
    expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);

    gesture = await tester.startGesture(const Offset(50.0, 300.0));
    await gesture.moveBy(const Offset(0.0, kToolbarHeight));
    await gesture.up();
    await tester.pumpAndSettle();

    expect(getAppBarBackgroundColor(), defaultColor);
    expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
  });
2756
}