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

5 6
// @dart = 2.8

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

13
import '../rendering/mock_canvas.dart';
14 15
import '../widgets/semantics_tester.dart';

16 17 18 19 20 21
Widget buildSliverAppBarApp({
  bool floating,
  bool pinned,
  double collapsedHeight,
  double expandedHeight,
  bool snap = false,
22
  double toolbarHeight = kToolbarHeight,
23
}) {
24
  return Localizations(
25
    locale: const Locale('en', 'US'),
26
    delegates: const <LocalizationsDelegate<dynamic>>[
27 28 29
      DefaultMaterialLocalizations.delegate,
      DefaultWidgetsLocalizations.delegate,
    ],
30
    child: Directionality(
31
      textDirection: TextDirection.ltr,
32
      child: MediaQuery(
33
        data: const MediaQueryData(),
34 35
        child: Scaffold(
          body: DefaultTabController(
36
            length: 3,
37
            child: CustomScrollView(
38 39
              primary: true,
              slivers: <Widget>[
40
                SliverAppBar(
41 42 43
                  title: const Text('AppBar Title'),
                  floating: floating,
                  pinned: pinned,
44
                  collapsedHeight: collapsedHeight,
45
                  expandedHeight: expandedHeight,
46
                  toolbarHeight: toolbarHeight,
47
                  snap: snap,
48
                  bottom: TabBar(
49
                    tabs: <String>['A','B','C'].map<Widget>((String t) => Tab(text: 'TAB $t')).toList(),
50
                  ),
51
                ),
52 53
                SliverToBoxAdapter(
                  child: Container(
54 55 56
                    height: 1200.0,
                    color: Colors.orange[400],
                  ),
57
                ),
58 59
              ],
            ),
60
          ),
61
        ),
62
      ),
63 64 65 66 67 68 69 70
    ),
  );
}

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

71 72 73
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;
74

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

77
void main() {
78 79 80 81
  setUp(() {
    debugResetSemanticsIdCounter();
  });

82 83
  testWidgets('AppBar centers title on iOS', (WidgetTester tester) async {
    await tester.pumpWidget(
84 85 86 87
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
88
            title: const Text('X'),
89 90 91
          ),
        ),
      ),
92 93
    );

94
    final Finder title = find.text('X');
95
    Offset center = tester.getCenter(title);
96
    Size size = tester.getSize(title);
97
    expect(center.dx, lessThan(400 - size.width / 2.0));
98

Dan Field's avatar
Dan Field committed
99 100 101
    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()));
102

Dan Field's avatar
Dan Field committed
103 104 105 106 107 108 109
      await tester.pumpWidget(
        MaterialApp(
          theme: ThemeData(platform: platform),
          home: Scaffold(
            appBar: AppBar(
              title: const Text('X'),
            ),
110 111
          ),
        ),
Dan Field's avatar
Dan Field committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      );

      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(
              title: const Text('X'),
              actions: const <Widget>[
                Icon(Icons.thumb_up),
              ],
            ),
131 132
          ),
        ),
Dan Field's avatar
Dan Field committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      );

      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(
              title: const Text('X'),
              actions: const <Widget>[
                Icon(Icons.thumb_up),
                Icon(Icons.thumb_up),
              ],
            ),
153 154
          ),
        ),
Dan Field's avatar
Dan Field committed
155
      );
156

Dan Field's avatar
Dan Field committed
157 158 159 160
      center = tester.getCenter(title);
      size = tester.getSize(title);
      expect(center.dx, lessThan(400 - size.width / 2.0), reason: 'on ${describeEnum(platform)}');
    }
161
  });
162 163 164

  testWidgets('AppBar centerTitle:true centers on Android', (WidgetTester tester) async {
    await tester.pumpWidget(
165 166 167 168
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
169
            centerTitle: true,
170
            title: const Text('X'),
171 172
          ),
        ),
173
      ),
174 175
    );

176
    final Finder title = find.text('X');
177
    final Offset center = tester.getCenter(title);
178
    final Size size = tester.getSize(title);
179 180
    expect(center.dx, greaterThan(400 - size.width / 2.0));
    expect(center.dx, lessThan(400 + size.width / 2.0));
181 182
  });

183
  testWidgets('AppBar centerTitle:false title start edge is 16.0 (LTR)', (WidgetTester tester) async {
184
    await tester.pumpWidget(
185 186 187
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
188
            centerTitle: false,
189
            title: const Placeholder(key: Key('X')),
190 191 192 193 194 195 196
          ),
        ),
      ),
    );

    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopLeft(titleWidget).dx, 16.0);
197
    expect(tester.getTopRight(titleWidget).dx, 800 - 16.0);
198 199 200 201
  });

  testWidgets('AppBar centerTitle:false title start edge is 16.0 (RTL)', (WidgetTester tester) async {
    await tester.pumpWidget(
202 203
      MaterialApp(
        home: Directionality(
204
          textDirection: TextDirection.rtl,
205 206
          child: Scaffold(
            appBar: AppBar(
207
              centerTitle: false,
208
              title: const Placeholder(key: Key('X')),
209 210 211 212 213 214 215 216
            ),
          ),
        ),
      ),
    );

    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopRight(titleWidget).dx, 800.0 - 16.0);
217
    expect(tester.getTopLeft(titleWidget).dx, 16.0);
218 219 220 221
  });

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

233 234
    final Finder titleWidget = find.byKey(const Key('X'));
    expect(tester.getTopLeft(titleWidget).dx, 32.0);
235
    expect(tester.getTopRight(titleWidget).dx, 800 - 32.0);
236 237
  });

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

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

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

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

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

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

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

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

    Widget buildApp() {
309 310 311
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
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
    Widget leading = Container();
364 365 366
    List<Widget> actions;

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

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

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

    // Centering a title with width 620 within the 800 pixel wide test widget
392
    // would mean that its start edge would have to be 90. We reserve 72
393 394
    // 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.
395 396 397 398 399

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

408 409 410 411 412
  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.

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

    Widget buildApp() {
419 420
      return MaterialApp(
        home: Directionality(
421
          textDirection: TextDirection.rtl,
422 423
          child: Scaffold(
            appBar: AppBar(
424 425
              leading: leading,
              centerTitle: true,
426
              title: Container(
427
                key: titleKey,
428
                constraints: BoxConstraints.loose(Size(titleWidth, 1000.0)),
429 430 431 432 433 434 435 436 437 438
              ),
              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
439
    // that the start edge of the title must be at least 72.
440 441 442 443 444 445 446 447
    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
448 449
    // 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.
450 451 452 453 454 455

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

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

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

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

500
    final Finder title = find.text('X');
501 502
    expect(tester.getSize(title).isEmpty, isTrue);
  });
503 504

  testWidgets('AppBar actions are vertically centered', (WidgetTester tester) async {
505 506 507 508 509
    final UniqueKey appBarKey = UniqueKey();
    final UniqueKey leadingKey = UniqueKey();
    final UniqueKey titleKey = UniqueKey();
    final UniqueKey action0Key = UniqueKey();
    final UniqueKey action1Key = UniqueKey();
510 511

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

    // The vertical center of the widget with key, in global coordinates.
528
    double yCenter(Key key) => tester.getCenter(find.byKey(key)).dy;
529 530 531 532 533 534 535

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

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

549
    final Finder hamburger = find.byTooltip('Open navigation menu');
550
    expect(tester.getTopLeft(hamburger), const Offset(0.0, 0.0));
551
    expect(tester.getSize(hamburger), const Size(56.0, 56.0));
552 553 554 555
  });

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

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

585
    final Finder shareButton = find.byTooltip('Share');
586
    // The 20dp icon is expanded to fill the IconButton's touch target to 48dp.
587
    expect(tester.getSize(shareButton), const Size(48.0, 56.0));
588 589
  });

590 591 592 593 594 595 596
  testWidgets('SliverAppBar default configuration', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: false,
      pinned: false,
      expandedHeight: null,
    ));

597
    final ScrollController controller = primaryScrollController(tester);
598
    expect(controller.offset, 0.0);
599
    expect(find.byType(SliverAppBar), findsOneWidget);
600 601 602 603 604 605 606

    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();
607
    expect(find.byType(SliverAppBar), findsOneWidget);
608 609 610 611 612 613
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);

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

    // Scroll the not-pinned appbar back into view
    controller.jumpTo(0.0);
    await tester.pump();
621
    expect(find.byType(SliverAppBar), findsOneWidget);
622 623 624 625 626 627 628 629 630 631 632 633
    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,
    ));

634
    final ScrollController controller = primaryScrollController(tester);
635
    expect(controller.offset, 0.0);
636
    expect(find.byType(SliverAppBar), findsOneWidget);
637 638
    expect(appBarHeight(tester), 128.0);

639
    const double initialAppBarHeight = 128.0;
640 641 642 643 644 645
    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();
646
    expect(find.byType(SliverAppBar), findsOneWidget);
647 648 649 650 651 652 653
    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();
654
    expect(find.byType(SliverAppBar), findsOneWidget);
655 656 657 658 659 660 661 662 663 664 665 666
    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,
    ));

667
    final ScrollController controller = primaryScrollController(tester);
668
    expect(controller.offset, 0.0);
669
    expect(find.byType(SliverAppBar), findsOneWidget);
670 671
    expect(appBarHeight(tester), 128.0);

672
    const double initialAppBarHeight = 128.0;
673 674
    final double initialTabBarHeight = tabBarHeight(tester);

675
    // Scroll the floating-pinned appbar, collapsing the expanded height. At this
676 677 678
    // point only the tabBar is visible.
    controller.jumpTo(600.0);
    await tester.pump();
679
    expect(find.byType(SliverAppBar), findsOneWidget);
680 681 682 683
    expect(tabBarHeight(tester), initialTabBarHeight);
    expect(appBarHeight(tester), lessThan(initialAppBarHeight));
    expect(appBarHeight(tester), initialTabBarHeight);

684
    // Scroll the floating-pinned appbar back into view
685 686
    controller.jumpTo(0.0);
    await tester.pump();
687
    expect(find.byType(SliverAppBar), findsOneWidget);
688 689 690
    expect(appBarHeight(tester), initialAppBarHeight);
    expect(tabBarHeight(tester), initialTabBarHeight);
  });
691 692 693 694 695 696 697 698

  testWidgets('SliverAppBar expandedHeight, floating with snap:true', (WidgetTester tester) async {
    await tester.pumpWidget(buildSliverAppBarApp(
      floating: true,
      pinned: false,
      snap: true,
      expandedHeight: 128.0,
    ));
699
    expect(find.byType(SliverAppBar), findsOneWidget);
700 701 702 703 704 705 706 707
    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();
708
    expect(find.byType(SliverAppBar), findsNothing);
709 710 711
    expect(appBarTop(tester), lessThanOrEqualTo(-128.0));

    // Drag the scrollable up and down. The app bar should not snap open, its
712
    // height should just track the drag offset.
Hans Muller's avatar
Hans Muller committed
713
    TestGesture gesture = await tester.startGesture(const Offset(50.0, 256.0));
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    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
744
    gesture = await tester.startGesture(const Offset(50.0, 256.0));
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    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,
    ));
780
    expect(find.byType(SliverAppBar), findsOneWidget);
781 782 783 784 785 786 787 788 789
    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();
790
    expect(find.byType(SliverAppBar), findsOneWidget);
791 792 793 794
    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
795
    // bottom of the appbar should just track the drag offset.
Hans Muller's avatar
Hans Muller committed
796
    TestGesture gesture = await tester.startGesture(const Offset(50.0, 200.0));
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
    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
830
    gesture = await tester.startGesture(const Offset(50.0, 256.0));
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
    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);
  });
858

859 860 861 862 863 864 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
  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);
  });

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
  testWidgets('SliverAppBar rebuilds when forceElevated changes', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/59158.
    Widget buildSliverAppBar(bool forceElevated) {
      return MaterialApp(
        home: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              title: const Text('Title'),
              forceElevated: forceElevated,
            ),
          ],
        ),
      );
    }

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

    await tester.pumpWidget(buildSliverAppBar(false));
    expect(getAppBarWidget(appBarFinder).elevation, 0.0);

    await tester.pumpWidget(buildSliverAppBar(true));
    expect(getAppBarWidget(appBarFinder).elevation, 4.0);
  });

924
  testWidgets('AppBar dimensions, with and without bottom, primary', (WidgetTester tester) async {
925
    const MediaQueryData topPadding100 = MediaQueryData(padding: EdgeInsets.only(top: 100.0));
926 927

    await tester.pumpWidget(
928 929 930 931 932 933 934
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
935
        textDirection: TextDirection.ltr,
936
        child: MediaQuery(
937
          data: topPadding100,
938
          child: Scaffold(
939
            primary: false,
940
            appBar: AppBar(),
941
          ),
942 943
        ),
      ),
944
    ));
945 946 947 948
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight);

    await tester.pumpWidget(
949 950 951 952 953 954 955 956 957 958 959 960
      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,
961
              appBar: AppBar(title: const Text('title')),
962
          ),
963 964
        ),
      ),
965
    ));
966 967 968 969 970
    expect(appBarTop(tester), 0.0);
    expect(tester.getTopLeft(find.text('title')).dy, greaterThan(100.0));
    expect(appBarHeight(tester), kToolbarHeight + 100.0);

    await tester.pumpWidget(
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
      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(
                bottom: PreferredSize(
                  preferredSize: const Size.fromHeight(200.0),
                  child: Container(),
987
              ),
988 989 990 991
            ),
          ),
        ),
      ),
992
    ));
993 994 995 996
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 200.0);

    await tester.pumpWidget(
997 998 999 1000 1001 1002 1003
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
1004
        textDirection: TextDirection.ltr,
1005
        child: MediaQuery(
1006
          data: topPadding100,
1007
          child: Scaffold(
1008
            primary: true,
1009 1010
            appBar: AppBar(
              bottom: PreferredSize(
1011
                preferredSize: const Size.fromHeight(200.0),
1012
                child: Container(),
1013
              ),
1014 1015 1016 1017
            ),
          ),
        ),
      ),
1018
    ));
1019 1020 1021 1022
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 100.0 + 200.0);

    await tester.pumpWidget(
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
      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(
              primary: false,
              title: const Text('title'),
            ),
1037
          ),
1038 1039 1040 1041 1042 1043
        ),
      ),
    );
    expect(appBarTop(tester), 0.0);
    expect(tester.getTopLeft(find.text('title')).dy, lessThan(100.0));
  });
1044

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  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(
                  title: const Text('title'),
                ),
              ],
            ),
          ),
        ),
      ),
    ));
    expect(appBarTop(tester), 0.0);
    expect(appBarHeight(tester), kToolbarHeight + 100.0);
  });

1075 1076
  testWidgets('AppBar updates when you add a drawer', (WidgetTester tester) async {
    await tester.pumpWidget(
1077 1078 1079
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(),
1080 1081 1082 1083 1084
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsNothing);
    await tester.pumpWidget(
1085 1086
      MaterialApp(
        home: Scaffold(
1087
          drawer: const Drawer(),
1088
          appBar: AppBar(),
1089 1090 1091 1092 1093
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsOneWidget);
  });
1094

1095 1096
  testWidgets('AppBar does not draw menu for drawer if automaticallyImplyLeading is false', (WidgetTester tester) async {
    await tester.pumpWidget(
1097 1098
      MaterialApp(
        home: Scaffold(
1099
          drawer: const Drawer(),
1100
          appBar: AppBar(automaticallyImplyLeading: false),
1101 1102 1103 1104 1105 1106
        ),
      ),
    );
    expect(find.byIcon(Icons.menu), findsNothing);
  });

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
  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(
            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(
                  begin: const Alignment(0.0, -1.0),
                  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));
  });

1149
  testWidgets('AppBar handles loose children 0', (WidgetTester tester) async {
1150
    final GlobalKey key = GlobalKey();
1151
    await tester.pumpWidget(
1152 1153 1154 1155
      MaterialApp(
        home: Center(
          child: AppBar(
            leading: Placeholder(key: key),
1156
            title: const Text('Abc'),
1157
            actions: const <Widget>[
1158 1159 1160
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
            ],
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 1', (WidgetTester tester) async {
1171
    final GlobalKey key = GlobalKey();
1172
    await tester.pumpWidget(
1173 1174 1175 1176
      MaterialApp(
        home: Center(
          child: AppBar(
            leading: Placeholder(key: key),
1177
            title: const Text('Abc'),
1178
            actions: const <Widget>[
1179 1180 1181
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1182
            ],
1183 1184 1185
            flexibleSpace: DecoratedBox(
              decoration: BoxDecoration(
                gradient: LinearGradient(
1186 1187
                  begin: const Alignment(0.0, -1.0),
                  end: const Alignment(-0.04, 1.0),
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
                  colors: <Color>[Colors.blue.shade500, Colors.blue.shade800],
                ),
              ),
            ),
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 2', (WidgetTester tester) async {
1201
    final GlobalKey key = GlobalKey();
1202
    await tester.pumpWidget(
1203 1204 1205 1206
      MaterialApp(
        home: Center(
          child: AppBar(
            leading: Placeholder(key: key),
1207
            title: const Text('Abc'),
1208
            actions: const <Widget>[
1209 1210 1211
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1212
            ],
1213 1214 1215
            flexibleSpace: DecoratedBox(
              decoration: BoxDecoration(
                gradient: LinearGradient(
1216 1217
                  begin: const Alignment(0.0, -1.0),
                  end: const Alignment(-0.04, 1.0),
1218 1219 1220 1221
                  colors: <Color>[Colors.blue.shade500, Colors.blue.shade800],
                ),
              ),
            ),
1222
            bottom: PreferredSize(
1223
              preferredSize: const Size(0.0, kToolbarHeight),
1224
              child: Container(
1225 1226 1227 1228
                height: 50.0,
                padding: const EdgeInsets.all(4.0),
                child: const Placeholder(
                  strokeWidth: 2.0,
1229
                  color: Color(0xFFFFFFFF),
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
                ),
              ),
            ),
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });

  testWidgets('AppBar handles loose children 3', (WidgetTester tester) async {
1242
    final GlobalKey key = GlobalKey();
1243
    await tester.pumpWidget(
1244 1245 1246 1247
      MaterialApp(
        home: Center(
          child: AppBar(
            leading: Placeholder(key: key),
1248
            title: const Text('Abc'),
1249
            actions: const <Widget>[
1250 1251 1252
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
              Placeholder(fallbackWidth: 10.0),
1253
            ],
1254
            bottom: PreferredSize(
1255
              preferredSize: const Size(0.0, kToolbarHeight),
1256
              child: Container(
1257 1258 1259 1260
                height: 50.0,
                padding: const EdgeInsets.all(4.0),
                child: const Placeholder(
                  strokeWidth: 2.0,
1261
                  color: Color(0xFFFFFFFF),
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
                ),
              ),
            ),
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byKey(key)).localToGlobal(Offset.zero), const Offset(0.0, 0.0));
    expect(tester.renderObject<RenderBox>(find.byKey(key)).size, const Size(56.0, 56.0));
  });
Ian Hickson's avatar
Ian Hickson committed
1272 1273

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

1276 1277 1278
    final Key leadingKey = UniqueKey();
    final Key titleKey = UniqueKey();
    final Key trailingKey = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
1279 1280

    await tester.pumpWidget(
1281 1282 1283 1284 1285 1286 1287
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
Ian Hickson's avatar
Ian Hickson committed
1288
        textDirection: TextDirection.rtl,
1289
        child: MediaQuery(
Ian Hickson's avatar
Ian Hickson committed
1290
          data: topPadding100,
1291
          child: Scaffold(
Ian Hickson's avatar
Ian Hickson committed
1292
            primary: false,
1293
            appBar: AppBar(
1294 1295 1296
              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
1297 1298 1299 1300
            ),
          ),
        ),
      ),
1301
    ));
1302 1303 1304 1305 1306
    expect(tester.getTopLeft(find.byType(AppBar)), const Offset(0, 0));
    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
1307
    // NavigationToolbar within the AppBar, the toolbar is constrained
1308 1309 1310 1311 1312 1313
    // 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
1314 1315 1316
  });

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

1319 1320 1321
    final Key leadingKey = UniqueKey();
    final Key titleKey = UniqueKey();
    final Key trailingKey = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
1322 1323

    await tester.pumpWidget(
1324 1325 1326 1327 1328 1329 1330
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
Ian Hickson's avatar
Ian Hickson committed
1331
        textDirection: TextDirection.rtl,
1332
        child: MediaQuery(
Ian Hickson's avatar
Ian Hickson committed
1333
          data: topPadding100,
1334
          child: CustomScrollView(
Ian Hickson's avatar
Ian Hickson committed
1335 1336
            primary: true,
            slivers: <Widget>[
1337 1338
              SliverAppBar(
                leading: Placeholder(key: leadingKey),
1339
                title: Placeholder(key: titleKey, fallbackHeight: kToolbarHeight),
1340
                actions: <Widget>[ Placeholder(key: trailingKey) ],
Ian Hickson's avatar
Ian Hickson committed
1341 1342 1343 1344 1345
              ),
            ],
          ),
        ),
      ),
1346
    ));
Ian Hickson's avatar
Ian Hickson committed
1347 1348
    expect(tester.getTopLeft(find.byType(AppBar)), const Offset(0.0, 0.0));
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(800.0 - 56.0, 100.0));
1349 1350
    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
1351
  });
1352 1353

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

1356 1357 1358
    final Key leadingKey = UniqueKey();
    final Key titleKey = UniqueKey();
    final Key trailingKey = UniqueKey();
1359 1360

    await tester.pumpWidget(
1361 1362 1363 1364 1365 1366 1367
      Localizations(
        locale: const Locale('en', 'US'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        child: Directionality(
1368
        textDirection: TextDirection.rtl,
1369
        child: MediaQuery(
1370
          data: topPadding100,
1371
          child: CustomScrollView(
1372 1373
            primary: true,
            slivers: <Widget>[
1374 1375 1376 1377
              SliverAppBar(
                leading: Placeholder(key: leadingKey),
                title: Placeholder(key: titleKey),
                actions: <Widget>[ Placeholder(key: trailingKey) ],
1378 1379 1380 1381 1382
              ),
            ],
          ),
        ),
      ),
1383
    ));
Dan Field's avatar
Dan Field committed
1384 1385 1386
    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));
1387
  });
1388 1389

  testWidgets('SliverAppBar provides correct semantics in LTR', (WidgetTester tester) async {
1390
    final SemanticsTester semantics = SemanticsTester(tester);
1391 1392

    await tester.pumpWidget(
1393 1394 1395
      MaterialApp(
        home: Center(
          child: AppBar(
1396 1397 1398
            leading: const Text('Leading'),
            title: const Text('Title'),
            actions: const <Widget>[
1399 1400 1401
              Text('Action 1'),
              Text('Action 2'),
              Text('Action 3'),
1402 1403
            ],
            bottom: const PreferredSize(
1404 1405
              preferredSize: Size(0.0, kToolbarHeight),
              child: Text('Bottom'),
1406 1407 1408 1409 1410 1411 1412
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
1413
      TestSemantics.root(
1414
        children: <TestSemantics>[
1415
          TestSemantics(
1416
            children: <TestSemantics>[
1417
              TestSemantics(
1418
                children: <TestSemantics> [
1419
                  TestSemantics(
1420
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1421
                    children: <TestSemantics>[
1422
                      TestSemantics(
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
                        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,
                          ),
1452
                        ],
1453 1454
                      ),
                    ],
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });

  testWidgets('SliverAppBar provides correct semantics in RTL', (WidgetTester tester) async {
1471
    final SemanticsTester semantics = SemanticsTester(tester);
1472 1473

    await tester.pumpWidget(
1474 1475
      MaterialApp(
        home: Semantics(
1476
          textDirection: TextDirection.rtl,
1477
          child: Directionality(
1478
            textDirection: TextDirection.rtl,
1479 1480
            child: Center(
              child: AppBar(
1481 1482 1483
                leading: const Text('Leading'),
                title: const Text('Title'),
                actions: const <Widget>[
1484 1485 1486
                  Text('Action 1'),
                  Text('Action 2'),
                  Text('Action 3'),
1487 1488
                ],
                bottom: const PreferredSize(
1489 1490
                  preferredSize: Size(0.0, kToolbarHeight),
                  child: Text('Bottom'),
1491 1492 1493 1494 1495 1496 1497 1498 1499
                ),
              ),
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
1500
      TestSemantics.root(
1501
        children: <TestSemantics>[
1502
          TestSemantics(
1503
            children: <TestSemantics>[
1504
              TestSemantics(
1505
                children: <TestSemantics>[
1506
                  TestSemantics(
1507
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1508
                    children: <TestSemantics>[
1509
                      TestSemantics(
1510
                        textDirection: TextDirection.rtl,
1511
                        children: <TestSemantics>[
1512
                          TestSemantics(
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
                            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,
                              ),
1542
                            ],
1543 1544
                          ),
                        ],
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
  testWidgets('AppBar excludes header semantics correctly', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(
      MaterialApp(
        home: Center(
          child: AppBar(
            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(
1588
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1589 1590
                    children: <TestSemantics>[
                      TestSemantics(
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'Leading',
                            textDirection: TextDirection.ltr,
                          ),
                          TestSemantics(
                            label: 'Action 1',
                            textDirection: TextDirection.ltr,
                          ),
                        ],
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      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(
              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(
1645
                    flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1646 1647 1648 1649 1650 1651
                    children: <TestSemantics>[
                      TestSemantics(
                        children: <TestSemantics>[
                          TestSemantics(
                            children: <TestSemantics>[
                              TestSemantics(
1652 1653 1654 1655 1656 1657
                                label: 'Leading',
                                textDirection: TextDirection.ltr,
                              ),
                              TestSemantics(
                                label: 'Action 1',
                                textDirection: TextDirection.ltr,
1658 1659 1660
                              ),
                            ],
                          ),
1661 1662 1663
                          TestSemantics(
                            flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
                          ),
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
      ignoreId: true,
    ));

    semantics.dispose();
  });

1682
  testWidgets('AppBar draws a light system bar for a dark background', (WidgetTester tester) async {
1683 1684
    final ThemeData darkTheme = ThemeData.dark();
    await tester.pumpWidget(MaterialApp(
1685 1686
      theme: darkTheme,
      home: Scaffold(
1687
        appBar: AppBar(title: const Text('test')),
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
      ),
    ));

    expect(darkTheme.primaryColorBrightness, Brightness.dark);
    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 {
1699 1700
    final ThemeData lightTheme = ThemeData(primaryColor: Colors.white);
    await tester.pumpWidget(MaterialApp(
1701 1702
      theme: lightTheme,
      home: Scaffold(
1703
        appBar: AppBar(title: const Text('test')),
1704 1705 1706 1707 1708 1709 1710 1711 1712
      ),
    ));

    expect(lightTheme.primaryColorBrightness, Brightness.light);
    expect(SystemChrome.latestStyle, const SystemUiOverlayStyle(
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
    ));
  });
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731

  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(
                    expandedHeight: appBarHeight,
                    pinned: false,
                    floating: true,
                    snap: snap,
                    actions: <Widget>[
1732
                      TextButton(
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
                        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();
  });
1775

1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
  testWidgets('AppBar shape default', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: AppBar(
          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);
  });

1796
  testWidgets('AppBar with shape', (WidgetTester tester) async {
1797 1798 1799
    const RoundedRectangleBorder roundedRectangleBorder = RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(15.0))
    );
1800 1801 1802 1803 1804 1805
    await tester.pumpWidget(
      MaterialApp(
        home: AppBar(
          leading: const Text('L'),
          title: const Text('No Scaffold'),
          actions: const <Widget>[Text('A1'), Text('A2')],
1806
          shape: roundedRectangleBorder,
1807 1808 1809 1810 1811
        ),
      ),
    );

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

1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
    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(
              leading: Text('L'),
              title: Text('No Scaffold'),
              actions: <Widget>[Text('A1'), Text('A2')],
            ),
          ],
        ),
      ),
    );
1834

1835 1836 1837
    final Finder sliverAppBarFinder = find.byType(SliverAppBar);
    SliverAppBar getSliverAppBarWidget(Finder finder) => tester.widget<SliverAppBar>(finder);
    expect(getSliverAppBarWidget(sliverAppBarFinder).shape, null);
1838 1839

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

1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
  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(
              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);
1870
  });
1871

1872 1873 1874 1875 1876 1877 1878
  testWidgets('AppBars title has upper limit on text scaling, textScaleFactor = 1, 1.34, 2', (WidgetTester tester) async {
    double textScaleFactor;

    Widget buildFrame() {
      return MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
1879 1880 1881 1882 1883 1884
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
              child: Scaffold(
                appBar: AppBar(
                  centerTitle: false,
                  title: const Text('Jumbo', style: TextStyle(fontSize: 18)),
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
                ),
              ),
            );
          },
        ),
      );
    }

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

1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
  testWidgets('AppBars with jumbo titles, textScaleFactor = 3, 3.5, 4', (WidgetTester tester) async {
    double textScaleFactor;
    TextDirection textDirection;
    bool centerTitle;

    Widget buildFrame() {
      return MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            return Directionality(
              textDirection: textDirection,
1919 1920 1921 1922 1923 1924 1925 1926
              child: Builder(
                builder: (BuildContext context) {
                  return Scaffold(
                    appBar: AppBar(
                      centerTitle: centerTitle,
                      title: MediaQuery(
                        data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
                        child: const Text('Jumbo'),
1927
                      ),
1928 1929 1930
                    ),
                  );
                },
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
              ),
            );
          },
        ),
      );
    }

    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.

    textScaleFactor = 1; // "Jumbo" title is 100x20.
    textDirection = TextDirection.ltr;
    centerTitle = false;
    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);
  });
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 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 2039 2040 2041 2042 2043 2044 2045

  testWidgets('AppBar respects toolbarHeight', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            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);
  });

  test('SliverApp toolbarHeight cannot be null', () {
    try{
       SliverAppBar(
        toolbarHeight: null,
      );
    } on AssertionError catch (error) {
      expect(error.toString(), contains('toolbarHeight != null'));
      expect(error.toString(), contains('is not true'));
    }
  });
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079

  testWidgets('AppBar respects leadingWidth', (WidgetTester tester) async {
    const Key key = Key('leading');
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          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(
            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));
  });
2080
}