bottom_navigation_bar_test.dart 89 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 7
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
8
library;
9

10 11
import 'dart:ui';

12
import 'package:flutter/material.dart';
13
import 'package:flutter/rendering.dart';
14
import 'package:flutter_test/flutter_test.dart';
15
import 'package:vector_math/vector_math_64.dart' show Vector3;
16

17
import '../rendering/mock_canvas.dart';
18
import '../widgets/semantics_tester.dart';
19
import 'feedback_tester.dart';
20

21 22
void main() {
  testWidgets('BottomNavigationBar callback test', (WidgetTester tester) async {
23
    late int mutatedIndex;
24 25

    await tester.pumpWidget(
26 27 28
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
29
            items: const <BottomNavigationBarItem>[
30 31
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
32
                label: 'AC',
33
              ),
34 35
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
36
                label: 'Alarm',
37
              ),
38 39 40
            ],
            onTap: (int index) {
              mutatedIndex = index;
41 42 43
            },
          ),
        ),
44
      ),
45 46 47 48 49 50 51 52 53
    );

    await tester.tap(find.text('Alarm'));

    expect(mutatedIndex, 1);
  });

  testWidgets('BottomNavigationBar content test', (WidgetTester tester) async {
    await tester.pumpWidget(
54 55 56
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
57
            items: const <BottomNavigationBarItem>[
58 59
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
60
                label: 'AC',
61
              ),
62 63
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
64
                label: 'Alarm',
65
              ),
66
            ],
67 68
          ),
        ),
69
      ),
70 71
    );

72
    final RenderBox box = tester.renderObject(find.byType(BottomNavigationBar));
73
    expect(box.size.height, kBottomNavigationBarHeight);
74 75 76 77
    expect(find.text('AC'), findsOneWidget);
    expect(find.text('Alarm'), findsOneWidget);
  });

78
  testWidgets('Fixed BottomNavigationBar defaults', (WidgetTester tester) async {
79
    const Color primaryColor = Color(0xFF000001);
80
    const Color unselectedWidgetColor = Color(0xFF000002);
81 82 83

    await tester.pumpWidget(
      MaterialApp(
84 85
        theme: ThemeData.light().copyWith(
          colorScheme: const ColorScheme.light().copyWith(primary: primaryColor),
86
          unselectedWidgetColor: unselectedWidgetColor,
87 88 89 90 91 92 93
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
94
                label: 'AC',
95 96 97
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
98
                label: 'Alarm',
99
              ),
100 101 102
            ],
          ),
        ),
103
      ),
104 105 106 107
    );

    const double selectedFontSize = 14.0;
    const double unselectedFontSize = 12.0;
108 109
    final TextStyle selectedFontStyle = tester.renderObject<RenderParagraph>(find.text('AC')).text.style!;
    final TextStyle unselectedFontStyle = tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!;
110 111 112 113
    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);
    expect(selectedFontStyle.color, equals(primaryColor));
    expect(selectedFontStyle.fontSize, selectedFontSize);
114
    expect(selectedFontStyle.fontWeight, equals(FontWeight.w400));
115
    expect(selectedFontStyle.height, isNull);
116
    expect(unselectedFontStyle.color, equals(unselectedWidgetColor));
117
    expect(unselectedFontStyle.fontWeight, equals(FontWeight.w400));
118
    expect(unselectedFontStyle.height, isNull);
119 120 121 122 123
    // Unselected label has a font size of 14 but is scaled down to be font size 12.
    expect(
      tester.firstWidget<Transform>(find.ancestor(of: find.text('Alarm'), matching: find.byType(Transform))).transform,
      equals(Matrix4.diagonal3(Vector3.all(unselectedFontSize / selectedFontSize))),
    );
124 125
    expect(selectedIcon.color, equals(primaryColor));
    expect(selectedIcon.fontSize, equals(24.0));
126
    expect(unselectedIcon.color, equals(unselectedWidgetColor));
127
    expect(unselectedIcon.fontSize, equals(24.0));
128 129 130 131 132 133 134 135 136 137 138 139
    // There should not be any [Opacity] or [FadeTransition] widgets
    // since showUnselectedLabels and showSelectedLabels are true.
    final Finder findOpacity = find.descendant(
      of: find.byType(BottomNavigationBar),
      matching: find.byType(Opacity),
    );
    final Finder findFadeTransition = find.descendant(
      of: find.byType(BottomNavigationBar),
      matching: find.byType(FadeTransition),
    );
    expect(findOpacity, findsNothing);
    expect(findFadeTransition, findsNothing);
140 141 142
    expect(_getMaterial(tester).elevation, equals(8.0));
  });

143
  testWidgets('Custom selected and unselected font styles', (WidgetTester tester) async {
144 145
    const TextStyle selectedTextStyle = TextStyle(fontWeight: FontWeight.w200, fontSize: 18.0);
    const TextStyle unselectedTextStyle = TextStyle(fontWeight: FontWeight.w600, fontSize: 12.0);
146 147 148 149 150 151 152 153 154 155 156

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              selectedLabelStyle: selectedTextStyle,
              unselectedLabelStyle: unselectedTextStyle,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
157
                  label: 'AC',
158 159 160
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
161
                  label: 'Alarm',
162 163 164 165
                ),
              ],
            ),
          ),
166
        ),
167 168
    );

169 170
    final TextStyle selectedFontStyle = tester.renderObject<RenderParagraph>(find.text('AC')).text.style!;
    final TextStyle unselectedFontStyle = tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!;
171 172 173 174
    expect(selectedFontStyle.fontSize, equals(selectedTextStyle.fontSize));
    expect(selectedFontStyle.fontWeight, equals(selectedTextStyle.fontWeight));
    expect(
      tester.firstWidget<Transform>(find.ancestor(of: find.text('Alarm'), matching: find.byType(Transform))).transform,
175
      equals(Matrix4.diagonal3(Vector3.all(unselectedTextStyle.fontSize! / selectedTextStyle.fontSize!))),
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    );
    expect(unselectedFontStyle.fontWeight, equals(unselectedTextStyle.fontWeight));
  });

  testWidgets('font size on text styles overrides font size params', (WidgetTester tester) async {
    const TextStyle selectedTextStyle = TextStyle(fontSize: 18.0);
    const TextStyle unselectedTextStyle = TextStyle(fontSize: 12.0);
    const double selectedFontSize = 17.0;
    const double unselectedFontSize = 11.0;

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              selectedLabelStyle: selectedTextStyle,
              unselectedLabelStyle: unselectedTextStyle,
              selectedFontSize: selectedFontSize,
              unselectedFontSize: unselectedFontSize,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
198
                  label: 'AC',
199 200 201
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
202
                  label: 'Alarm',
203 204 205 206
                ),
              ],
            ),
          ),
207
        ),
208 209
    );

210
    final TextStyle selectedFontStyle = tester.renderObject<RenderParagraph>(find.text('AC')).text.style!;
211 212 213
    expect(selectedFontStyle.fontSize, equals(selectedTextStyle.fontSize));
    expect(
      tester.firstWidget<Transform>(find.ancestor(of: find.text('Alarm'), matching: find.byType(Transform))).transform,
214
      equals(Matrix4.diagonal3(Vector3.all(unselectedTextStyle.fontSize! / selectedTextStyle.fontSize!))),
215 216 217 218
    );
  });

  testWidgets('Custom selected and unselected icon themes', (WidgetTester tester) async {
219 220
    const IconThemeData selectedIconTheme = IconThemeData(size: 36, color: Color(0x00000001));
    const IconThemeData unselectedIconTheme = IconThemeData(size: 18, color: Color(0x00000002));
221 222 223 224 225 226 227 228 229 230 231

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedIconTheme: selectedIconTheme,
            unselectedIconTheme: unselectedIconTheme,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
232
                label: 'AC',
233 234 235
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
236
                label: 'Alarm',
237 238 239 240
              ),
            ],
          ),
        ),
241
      ),
242 243 244 245 246 247 248 249 250 251 252
    );

    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);
    expect(selectedIcon.color, equals(selectedIconTheme.color));
    expect(selectedIcon.fontSize, equals(selectedIconTheme.size));
    expect(unselectedIcon.color, equals(unselectedIconTheme.color));
    expect(unselectedIcon.fontSize, equals(unselectedIconTheme.size));
  });

  testWidgets('color on icon theme overrides selected and unselected item colors', (WidgetTester tester) async {
253 254 255 256
    const IconThemeData selectedIconTheme = IconThemeData(size: 36, color: Color(0x00000001));
    const IconThemeData unselectedIconTheme = IconThemeData(size: 18, color: Color(0x00000002));
    const Color selectedItemColor = Color(0x00000003);
    const Color unselectedItemColor = Color(0x00000004);
257 258 259 260 261 262 263 264 265 266 267 268 269

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedIconTheme: selectedIconTheme,
            unselectedIconTheme: unselectedIconTheme,
            selectedItemColor: selectedItemColor,
            unselectedItemColor: unselectedItemColor,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
270
                label: 'AC',
271 272 273
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
274
                label: 'Alarm',
275 276 277 278
              ),
            ],
          ),
        ),
279
      ),
280 281
    );

282 283
    final TextStyle selectedFontStyle = tester.renderObject<RenderParagraph>(find.text('AC')).text.style!;
    final TextStyle unselectedFontStyle = tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);
    expect(selectedIcon.color, equals(selectedIconTheme.color));
    expect(unselectedIcon.color, equals(unselectedIconTheme.color));
    expect(selectedFontStyle.color, equals(selectedItemColor));
    expect(unselectedFontStyle.color, equals(unselectedItemColor));
  });

  testWidgets('Padding is calculated properly on items - all labels', (WidgetTester tester) async {
    const double selectedFontSize = 16.0;
    const double selectedIconSize = 36.0;
    const double unselectedIconSize = 20.0;
    const IconThemeData selectedIconTheme = IconThemeData(size: selectedIconSize);
    const IconThemeData unselectedIconTheme = IconThemeData(size: unselectedIconSize);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            showSelectedLabels: true,
            showUnselectedLabels: true,
            selectedFontSize: selectedFontSize,
            selectedIconTheme: selectedIconTheme,
            unselectedIconTheme: unselectedIconTheme,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
312
                label: 'AC',
313 314 315
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
316
                label: 'Alarm',
317 318 319 320
              ),
            ],
          ),
        ),
321
      ),
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    );

    final EdgeInsets selectedItemPadding = _itemPadding(tester, Icons.ac_unit);
    expect(selectedItemPadding.top, equals(selectedFontSize / 2.0));
    expect(selectedItemPadding.bottom, equals(selectedFontSize / 2.0));
    final EdgeInsets unselectedItemPadding = _itemPadding(tester, Icons.access_alarm);
    const double expectedUnselectedPadding = (selectedIconSize - unselectedIconSize) / 2.0 + selectedFontSize / 2.0;
    expect(unselectedItemPadding.top, equals(expectedUnselectedPadding));
    expect(unselectedItemPadding.bottom, equals(expectedUnselectedPadding));
  });

  testWidgets('Padding is calculated properly on items - selected labels only', (WidgetTester tester) async {
    const double selectedFontSize = 16.0;
    const double selectedIconSize = 36.0;
    const double unselectedIconSize = 20.0;
    const IconThemeData selectedIconTheme = IconThemeData(size: selectedIconSize);
    const IconThemeData unselectedIconTheme = IconThemeData(size: unselectedIconSize);

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              showSelectedLabels: true,
              showUnselectedLabels: false,
              selectedFontSize: selectedFontSize,
              selectedIconTheme: selectedIconTheme,
              unselectedIconTheme: unselectedIconTheme,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
353
                  label: 'AC',
354 355 356
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
357
                  label: 'Alarm',
358 359 360 361
                ),
              ],
            ),
          ),
362
        ),
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    );

    final EdgeInsets selectedItemPadding = _itemPadding(tester, Icons.ac_unit);
    expect(selectedItemPadding.top, equals(selectedFontSize / 2.0));
    expect(selectedItemPadding.bottom, equals(selectedFontSize / 2.0));
    final EdgeInsets unselectedItemPadding = _itemPadding(tester, Icons.access_alarm);
    expect(unselectedItemPadding.top, equals((selectedIconSize - unselectedIconSize) / 2.0 + selectedFontSize));
    expect(unselectedItemPadding.bottom, equals((selectedIconSize - unselectedIconSize) / 2.0));
  });

  testWidgets('Padding is calculated properly on items - no labels', (WidgetTester tester) async {
    const double selectedFontSize = 16.0;
    const double selectedIconSize = 36.0;
    const double unselectedIconSize = 20.0;
    const IconThemeData selectedIconTheme = IconThemeData(size: selectedIconSize);
    const IconThemeData unselectedIconTheme = IconThemeData(size: unselectedIconSize);

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              showSelectedLabels: false,
              showUnselectedLabels: false,
              selectedFontSize: selectedFontSize,
              selectedIconTheme: selectedIconTheme,
              unselectedIconTheme: unselectedIconTheme,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
393
                  label: 'AC',
394 395 396
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
397
                  label: 'Alarm',
398 399 400 401
                ),
              ],
            ),
          ),
402
        ),
403 404 405 406 407 408 409 410 411 412
    );

    final EdgeInsets selectedItemPadding = _itemPadding(tester, Icons.ac_unit);
    expect(selectedItemPadding.top, equals(selectedFontSize));
    expect(selectedItemPadding.bottom, equals(0.0));
    final EdgeInsets unselectedItemPadding = _itemPadding(tester, Icons.access_alarm);
    expect(unselectedItemPadding.top, equals((selectedIconSize - unselectedIconSize) / 2.0 + selectedFontSize));
    expect(unselectedItemPadding.bottom, equals((selectedIconSize - unselectedIconSize) / 2.0));
  });

413 414 415 416 417 418 419 420 421
  testWidgets('Shifting BottomNavigationBar defaults', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
422
                label: 'AC',
423 424 425
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
426
                label: 'Alarm',
427
              ),
428 429 430
            ],
          ),
        ),
431
      ),
432 433 434
    );

    const double selectedFontSize = 14.0;
435 436
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.fontSize, selectedFontSize);
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.color, equals(Colors.white));
437 438 439 440 441
    expect(_getOpacity(tester, 'Alarm'), equals(0.0));
    expect(_getMaterial(tester).elevation, equals(8.0));
  });

  testWidgets('Fixed BottomNavigationBar custom font size, color', (WidgetTester tester) async {
442 443 444 445
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedColor = Color(0xFF0004FF);
    const Color unselectedColor = Color(0xFFE5FF00);
446 447 448 449 450 451 452
    const double selectedFontSize = 18.0;
    const double unselectedFontSize = 14.0;

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
453
          unselectedWidgetColor: unselectedWidgetColor,
454 455 456 457 458 459 460 461 462 463 464
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedFontSize: selectedFontSize,
            unselectedFontSize: unselectedFontSize,
            selectedItemColor: selectedColor,
            unselectedItemColor: unselectedColor,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
465
                label: 'AC',
466 467 468
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
469
                label: 'Alarm',
470
              ),
471 472 473
            ],
          ),
        ),
474
      ),
475 476
    );

477 478 479
    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

480
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.fontSize, selectedFontSize);
481
    // Unselected label has a font size of 18 but is scaled down to be font size 14.
482
    expect(tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!.fontSize, selectedFontSize);
483 484 485 486
    expect(
      tester.firstWidget<Transform>(find.ancestor(of: find.text('Alarm'), matching: find.byType(Transform))).transform,
      equals(Matrix4.diagonal3(Vector3.all(unselectedFontSize / selectedFontSize))),
    );
487 488
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.color, equals(selectedColor));
    expect(tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!.color, equals(unselectedColor));
489 490
    expect(selectedIcon.color, equals(selectedColor));
    expect(unselectedIcon.color, equals(unselectedColor));
491 492 493 494 495 496 497 498 499 500 501 502
    // There should not be any [Opacity] or [FadeTransition] widgets
    // since showUnselectedLabels and showSelectedLabels are true.
    final Finder findOpacity = find.descendant(
      of: find.byType(BottomNavigationBar),
      matching: find.byType(Opacity),
    );
    final Finder findFadeTransition = find.descendant(
      of: find.byType(BottomNavigationBar),
      matching: find.byType(FadeTransition),
    );
    expect(findOpacity, findsNothing);
    expect(findFadeTransition, findsNothing);
503 504 505 506
  });


  testWidgets('Shifting BottomNavigationBar custom font size, color', (WidgetTester tester) async {
507 508 509 510
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedColor = Color(0xFF0004FF);
    const Color unselectedColor = Color(0xFFE5FF00);
511 512 513 514 515 516 517
    const double selectedFontSize = 18.0;
    const double unselectedFontSize = 14.0;

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
518
          unselectedWidgetColor: unselectedWidgetColor,
519 520 521 522 523 524 525 526 527 528 529
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            selectedFontSize: selectedFontSize,
            unselectedFontSize: unselectedFontSize,
            selectedItemColor: selectedColor,
            unselectedItemColor: unselectedColor,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
530
                label: 'AC',
531 532 533
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
534
                label: 'Alarm',
535
              ),
536 537 538
            ],
          ),
        ),
539
      ),
540 541
    );

542 543 544
    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

545 546
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.fontSize, selectedFontSize);
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.color, equals(selectedColor));
547
    expect(_getOpacity(tester, 'Alarm'), equals(0.0));
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830

    expect(selectedIcon.color, equals(selectedColor));
    expect(unselectedIcon.color, equals(unselectedColor));
  });

  testWidgets('label style color should override itemColor only for the label for BottomNavigationBarType.fixed', (WidgetTester tester) async {
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedColor = Color(0xFF0004FF);
    const Color unselectedColor = Color(0xFFE5FF00);
    const Color selectedLabelColor = Color(0xFFFF9900);
    const Color unselectedLabelColor = Color(0xFF92F74E);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
          unselectedWidgetColor: unselectedWidgetColor,
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedLabelStyle: const TextStyle(color: selectedLabelColor),
            unselectedLabelStyle: const TextStyle(color: unselectedLabelColor),
            selectedItemColor: selectedColor,
            unselectedItemColor: unselectedColor,
            useLegacyColorScheme: false,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'AC',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
                label: 'Alarm',
              ),
            ],
          ),
        ),
      ),
    );

    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

    expect(selectedIcon.color, equals(selectedColor));
    expect(unselectedIcon.color, equals(unselectedColor));
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.color, equals(selectedLabelColor));
    expect(tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!.color, equals(unselectedLabelColor));
  });

  testWidgets('label style color should override itemColor only for the label for BottomNavigationBarType.shifting', (WidgetTester tester) async {
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedColor = Color(0xFF0004FF);
    const Color unselectedColor = Color(0xFFE5FF00);
    const Color selectedLabelColor = Color(0xFFFF9900);
    const Color unselectedLabelColor = Color(0xFF92F74E);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
          unselectedWidgetColor: unselectedWidgetColor,
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            selectedLabelStyle: const TextStyle(color: selectedLabelColor),
            unselectedLabelStyle: const TextStyle(color: unselectedLabelColor),
            selectedItemColor: selectedColor,
            unselectedItemColor: unselectedColor,
            useLegacyColorScheme: false,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'AC',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
                label: 'Alarm',
              ),
            ],
          ),
        ),
      ),
    );

    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

    expect(selectedIcon.color, equals(selectedColor));
    expect(unselectedIcon.color, equals(unselectedColor));
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.color, equals(selectedLabelColor));
    expect(tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!.color, equals(unselectedLabelColor));
  });

  testWidgets('iconTheme color should override itemColor for BottomNavigationBarType.fixed', (WidgetTester tester) async {
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedColor = Color(0xFF0004FF);
    const Color unselectedColor = Color(0xFFE5FF00);
    const Color selectedLabelColor = Color(0xFFFF9900);
    const Color unselectedLabelColor = Color(0xFF92F74E);
    const Color selectedIconThemeColor = Color(0xFF1E7723);
    const Color unselectedIconThemeColor = Color(0xFF009688);
    const IconThemeData selectedIconTheme = IconThemeData(size: 20, color: selectedIconThemeColor);
    const IconThemeData unselectedIconTheme = IconThemeData(size: 18, color: unselectedIconThemeColor);
    const TextStyle selectedTextStyle = TextStyle(fontSize: 18.0, color: selectedLabelColor);
    const TextStyle unselectedTextStyle = TextStyle(fontSize: 18.0, color: unselectedLabelColor);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
          unselectedWidgetColor: unselectedWidgetColor,
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedLabelStyle: selectedTextStyle,
            unselectedLabelStyle: unselectedTextStyle,
            selectedIconTheme: selectedIconTheme,
            unselectedIconTheme: unselectedIconTheme,
            selectedItemColor: selectedColor,
            unselectedItemColor: unselectedColor,
            useLegacyColorScheme: false,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'AC',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
                label: 'Alarm',
              ),
            ],
          ),
        ),
      ),
    );

    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

    expect(selectedIcon.color, equals(selectedIconThemeColor));
    expect(unselectedIcon.color, equals(unselectedIconThemeColor));
  });

  testWidgets('iconTheme color should override itemColor for BottomNavigationBarType.shifted', (WidgetTester tester) async {
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedLabelColor = Color(0xFFFF9900);
    const Color unselectedLabelColor = Color(0xFF92F74E);
    const Color selectedIconThemeColor = Color(0xFF1E7723);
    const Color unselectedIconThemeColor = Color(0xFF009688);
    const IconThemeData selectedIconTheme = IconThemeData(size: 20, color: selectedIconThemeColor);
    const IconThemeData unselectedIconTheme = IconThemeData(size: 18, color: unselectedIconThemeColor);
    const TextStyle selectedTextStyle = TextStyle(fontSize: 18.0, color: selectedLabelColor);
    const TextStyle unselectedTextStyle = TextStyle(fontSize: 18.0, color: unselectedLabelColor);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
          unselectedWidgetColor: unselectedWidgetColor,
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            selectedLabelStyle: selectedTextStyle,
            unselectedLabelStyle: unselectedTextStyle,
            selectedIconTheme: selectedIconTheme,
            unselectedIconTheme: unselectedIconTheme,
            useLegacyColorScheme: false,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'AC',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
                label: 'Alarm',
              ),
            ],
          ),
        ),
      ),
    );

    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

    expect(selectedIcon.color, equals(selectedIconThemeColor));
    expect(unselectedIcon.color, equals(unselectedIconThemeColor));
  });

  testWidgets('iconTheme color should override itemColor color for BottomNavigationBarType.fixed', (WidgetTester tester) async {
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedIconThemeColor = Color(0xFF1E7723);
    const Color unselectedIconThemeColor = Color(0xFF009688);
    const IconThemeData selectedIconTheme = IconThemeData(size: 20, color: selectedIconThemeColor);
    const IconThemeData unselectedIconTheme = IconThemeData(size: 18, color: unselectedIconThemeColor);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
          unselectedWidgetColor: unselectedWidgetColor,
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedIconTheme: selectedIconTheme,
            unselectedIconTheme: unselectedIconTheme,
            useLegacyColorScheme: false,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'AC',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
                label: 'Alarm',
              ),
            ],
          ),
        ),
      ),
    );

    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

    expect(selectedIcon.color, equals(selectedIconThemeColor));
    expect(unselectedIcon.color, equals(unselectedIconThemeColor));
  });

  testWidgets('iconTheme color should override itemColor for BottomNavigationBarType.shifted', (WidgetTester tester) async {
    const Color primaryColor = Color(0xFF000000);
    const Color unselectedWidgetColor = Color(0xFFD501FF);
    const Color selectedColor = Color(0xFF0004FF);
    const Color unselectedColor = Color(0xFFE5FF00);
    const Color selectedIconThemeColor = Color(0xFF1E7723);
    const Color unselectedIconThemeColor = Color(0xFF009688);
    const IconThemeData selectedIconTheme = IconThemeData(size: 20, color: selectedIconThemeColor);
    const IconThemeData unselectedIconTheme = IconThemeData(size: 18, color: unselectedIconThemeColor);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          primaryColor: primaryColor,
          unselectedWidgetColor: unselectedWidgetColor,
        ),
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            selectedIconTheme: selectedIconTheme,
            unselectedIconTheme: unselectedIconTheme,
            selectedItemColor: selectedColor,
            unselectedItemColor: unselectedColor,
            useLegacyColorScheme: false,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'AC',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
                label: 'Alarm',
              ),
            ],
          ),
        ),
      ),
    );

    final TextStyle selectedIcon = _iconStyle(tester, Icons.ac_unit);
    final TextStyle unselectedIcon = _iconStyle(tester, Icons.access_alarm);

    expect(selectedIcon.color, equals(selectedIconThemeColor));
    expect(unselectedIcon.color, equals(unselectedIconThemeColor));
831 832 833 834 835 836 837 838 839 840 841 842
  });

  testWidgets('Fixed BottomNavigationBar can hide unselected labels', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            showUnselectedLabels: false,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
843
                label: 'AC',
844 845 846
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
847
                label: 'Alarm',
848
              ),
849 850 851
            ],
          ),
        ),
852
      ),
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    );

    expect(_getOpacity(tester, 'AC'), equals(1.0));
    expect(_getOpacity(tester, 'Alarm'), equals(0.0));
  });

  testWidgets('Fixed BottomNavigationBar can update background color', (WidgetTester tester) async {
    const Color color = Colors.yellow;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            backgroundColor: color,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
871
                label: 'AC',
872 873 874
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
875
                label: 'Alarm',
876
              ),
877 878 879
            ],
          ),
        ),
880
      ),
881 882 883 884 885
    );

    expect(_getMaterial(tester).color, equals(color));
  });

Chris Bracken's avatar
Chris Bracken committed
886
  testWidgets('Shifting BottomNavigationBar background color is overridden by item color', (WidgetTester tester) async {
887 888 889 890 891 892 893 894 895 896 897 898
    const Color itemColor = Colors.yellow;
    const Color backgroundColor = Colors.blue;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            backgroundColor: backgroundColor,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
899
                label: 'AC',
900 901 902 903
                backgroundColor: itemColor,
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
904
                label: 'Alarm',
905
              ),
906 907 908
            ],
          ),
        ),
909
      ),
910 911 912 913 914 915 916 917 918 919 920 921 922 923
    );

    expect(_getMaterial(tester).color, equals(itemColor));
  });

  testWidgets('Specifying both selectedItemColor and fixedColor asserts', (WidgetTester tester) async {
    expect(
      () {
        return BottomNavigationBar(
          selectedItemColor: Colors.black,
          fixedColor: Colors.black,
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.ac_unit),
924
              label: 'AC',
925 926 927
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm),
928
              label: 'Alarm',
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
            ),
          ],
        );
      },
      throwsAssertionError,
    );
  });

  testWidgets('Fixed BottomNavigationBar uses fixedColor when selectedItemColor not provided', (WidgetTester tester) async {
    const Color fixedColor = Colors.black;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            fixedColor: fixedColor,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
949
                label: 'AC',
950 951 952
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
953
                label: 'Alarm',
954
              ),
955 956 957
            ],
          ),
        ),
958
      ),
959 960
    );

961
    expect(tester.renderObject<RenderParagraph>(find.text('AC')).text.style!.color, equals(fixedColor));
962 963 964 965 966 967 968 969 970 971 972 973 974 975
  });

  testWidgets('setting selectedFontSize to zero hides all labels', (WidgetTester tester) async {
    const double customElevation = 3.0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            elevation: customElevation,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
976
                label: 'AC',
977 978 979
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
980
                label: 'Alarm',
981
              ),
982 983 984
            ],
          ),
        ),
985
      ),
986 987 988 989 990
    );

    expect(_getMaterial(tester).elevation, equals(customElevation));
  });

991 992
  testWidgets('BottomNavigationBar adds bottom padding to height', (WidgetTester tester) async {
    await tester.pumpWidget(
993 994
      MaterialApp(
        home: MediaQuery(
995
          data: const MediaQueryData(viewPadding: EdgeInsets.only(bottom: 40.0)),
996 997
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
998
              items: const <BottomNavigationBarItem>[
999 1000
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
1001
                  label: 'AC',
1002
                ),
1003 1004
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
1005
                  label: 'Alarm',
1006
                ),
1007
              ],
1008 1009 1010
            ),
          ),
        ),
1011
      ),
1012 1013
    );

1014 1015 1016 1017 1018 1019 1020 1021
    const double expectedHeight = kBottomNavigationBarHeight + 40.0;
    expect(tester.getSize(find.byType(BottomNavigationBar)).height, expectedHeight);
  });

  testWidgets('BottomNavigationBar adds bottom padding to height with a custom font size', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: MediaQuery(
1022
          data: const MediaQueryData(viewPadding: EdgeInsets.only(bottom: 40.0)),
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              selectedFontSize: 8,
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
                  label: 'AC',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
                  label: 'Alarm',
                ),
              ],
            ),
          ),
        ),
      ),
    );

    const double expectedHeight = kBottomNavigationBarHeight + 40.0;
1043 1044 1045
    expect(tester.getSize(find.byType(BottomNavigationBar)).height, expectedHeight);
  });

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 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  testWidgets('BottomNavigationBar height will not change when toggle keyboard', (WidgetTester tester) async {

    final Widget child = Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        selectedFontSize: 8,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.ac_unit),
            label: 'AC',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.access_alarm),
            label: 'Alarm',
          ),
        ],
      ),
    );

    // Test the bar height is correct when not showing the keyboard.
    await tester.pumpWidget(
      MaterialApp(
        home: MediaQuery(
          data: const MediaQueryData(
            viewPadding: EdgeInsets.only(bottom: 40.0),
            padding: EdgeInsets.only(bottom: 40.0),
          ),
          child: child,
        ),
      ),
    );

    // Expect the height is the correct.
    const double expectedHeight = kBottomNavigationBarHeight + 40.0;
    expect(tester.getSize(find.byType(BottomNavigationBar)).height, expectedHeight);

    // Now we show the keyboard.
    await tester.pumpWidget(
      MaterialApp(
        home: MediaQuery(
          data: const MediaQueryData(
            viewPadding: EdgeInsets.only(bottom: 40.0),
            viewInsets: EdgeInsets.only(bottom: 336.0),
          ),
          child: child,
        ),
      ),
    );

    // Expect the height is the same.
    expect(tester.getSize(find.byType(BottomNavigationBar)).height, expectedHeight);
  });

1098 1099
  testWidgets('BottomNavigationBar action size test', (WidgetTester tester) async {
    await tester.pumpWidget(
1100 1101 1102
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1103
            type: BottomNavigationBarType.shifting,
1104
            items: const <BottomNavigationBarItem>[
1105 1106
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
1107
                label: 'AC',
1108
              ),
1109 1110
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
1111
                label: 'Alarm',
1112 1113 1114 1115
              ),
            ],
          ),
        ),
1116
      ),
1117 1118 1119 1120
    );

    Iterable<RenderBox> actions = tester.renderObjectList(find.byType(InkResponse));
    expect(actions.length, 2);
1121 1122
    expect(actions.elementAt(0).size.width, 480.0);
    expect(actions.elementAt(1).size.width, 320.0);
1123 1124

    await tester.pumpWidget(
1125 1126 1127
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1128 1129
            currentIndex: 1,
            type: BottomNavigationBarType.shifting,
1130
            items: const <BottomNavigationBarItem>[
1131 1132
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
1133
                label: 'AC',
1134
              ),
1135 1136
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
1137
                label: 'Alarm',
1138 1139 1140 1141
              ),
            ],
          ),
        ),
1142
      ),
1143 1144 1145 1146 1147 1148
    );

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

    actions = tester.renderObjectList(find.byType(InkResponse));
    expect(actions.length, 2);
1149 1150
    expect(actions.elementAt(0).size.width, 320.0);
    expect(actions.elementAt(1).size.width, 480.0);
1151 1152 1153 1154
  });

  testWidgets('BottomNavigationBar multiple taps test', (WidgetTester tester) async {
    await tester.pumpWidget(
1155 1156 1157
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1158
            type: BottomNavigationBarType.shifting,
1159
            items: const <BottomNavigationBarItem>[
1160 1161
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
1162
                label: 'AC',
1163
              ),
1164 1165
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
1166
                label: 'Alarm',
1167
              ),
1168 1169
              BottomNavigationBarItem(
                icon: Icon(Icons.access_time),
1170
                label: 'Time',
1171
              ),
1172 1173
              BottomNavigationBarItem(
                icon: Icon(Icons.add),
1174
                label: 'Add',
1175 1176 1177 1178
              ),
            ],
          ),
        ),
1179
      ),
1180 1181 1182 1183 1184 1185 1186
    );

    // We want to make sure that the last label does not get displaced,
    // irrespective of how many taps happen on the first N - 1 labels and how
    // they grow.

    Iterable<RenderBox> actions = tester.renderObjectList(find.byType(InkResponse));
1187
    final Offset originalOrigin = actions.elementAt(3).localToGlobal(Offset.zero);
1188 1189 1190 1191 1192 1193

    await tester.tap(find.text('AC'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    actions = tester.renderObjectList(find.byType(InkResponse));
1194
    expect(actions.elementAt(3).localToGlobal(Offset.zero), equals(originalOrigin));
1195 1196 1197 1198 1199 1200

    await tester.tap(find.text('Alarm'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    actions = tester.renderObjectList(find.byType(InkResponse));
1201
    expect(actions.elementAt(3).localToGlobal(Offset.zero), equals(originalOrigin));
1202 1203 1204 1205 1206 1207

    await tester.tap(find.text('Time'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    actions = tester.renderObjectList(find.byType(InkResponse));
1208
    expect(actions.elementAt(3).localToGlobal(Offset.zero), equals(originalOrigin));
1209
  });
1210 1211 1212

  testWidgets('BottomNavigationBar inherits shadowed app theme for shifting navbar', (WidgetTester tester) async {
    await tester.pumpWidget(
1213 1214 1215 1216 1217 1218
      MaterialApp(
        theme: ThemeData(brightness: Brightness.light),
        home: Theme(
          data: ThemeData(brightness: Brightness.dark),
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
1219
              type: BottomNavigationBarType.shifting,
1220
              items: const <BottomNavigationBarItem>[
1221 1222
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
1223
                  label: 'AC',
1224
                ),
1225 1226
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
1227
                  label: 'Alarm',
1228
                ),
1229 1230
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_time),
1231
                  label: 'Time',
1232
                ),
1233 1234
                BottomNavigationBarItem(
                  icon: Icon(Icons.add),
1235
                  label: 'Add',
1236 1237 1238 1239 1240
                ),
              ],
            ),
          ),
        ),
1241
      ),
1242 1243 1244 1245
    );

    await tester.tap(find.text('Alarm'));
    await tester.pump(const Duration(seconds: 1));
1246
    expect(Theme.of(tester.element(find.text('Alarm'))).brightness, equals(Brightness.dark));
1247 1248 1249 1250
  });

  testWidgets('BottomNavigationBar inherits shadowed app theme for fixed navbar', (WidgetTester tester) async {
    await tester.pumpWidget(
1251 1252 1253 1254 1255 1256
      MaterialApp(
        theme: ThemeData(brightness: Brightness.light),
        home: Theme(
          data: ThemeData(brightness: Brightness.dark),
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
1257
              type: BottomNavigationBarType.fixed,
1258
              items: const <BottomNavigationBarItem>[
1259 1260
                BottomNavigationBarItem(
                  icon: Icon(Icons.ac_unit),
1261
                  label: 'AC',
1262
                ),
1263 1264
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm),
1265
                  label: 'Alarm',
1266
                ),
1267 1268
                BottomNavigationBarItem(
                  icon: Icon(Icons.access_time),
1269
                  label: 'Time',
1270
                ),
1271 1272
                BottomNavigationBarItem(
                  icon: Icon(Icons.add),
1273
                  label: 'Add',
1274 1275 1276 1277 1278
                ),
              ],
            ),
          ),
        ),
1279
      ),
1280 1281 1282 1283
    );

    await tester.tap(find.text('Alarm'));
    await tester.pump(const Duration(seconds: 1));
1284
    expect(Theme.of(tester.element(find.text('Alarm'))).brightness, equals(Brightness.dark));
1285
  });
1286 1287

  testWidgets('BottomNavigationBar iconSize test', (WidgetTester tester) async {
1288
    late double builderIconSize;
1289
    await tester.pumpWidget(
1290 1291 1292
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1293 1294
            iconSize: 12.0,
            items: <BottomNavigationBarItem>[
xster's avatar
xster committed
1295
              const BottomNavigationBarItem(
1296
                label: 'A',
1297
                icon: Icon(Icons.ac_unit),
1298
              ),
1299
              BottomNavigationBarItem(
1300
                label: 'B',
1301
                icon: Builder(
1302
                  builder: (BuildContext context) {
1303
                    builderIconSize = IconTheme.of(context).size!;
1304
                    return SizedBox(
1305 1306 1307 1308 1309
                      width: builderIconSize,
                      height: builderIconSize,
                    );
                  },
                ),
1310
              ),
1311 1312
            ],
          ),
1313 1314 1315 1316
        ),
      ),
    );

1317
    final RenderBox box = tester.renderObject(find.byType(Icon));
1318 1319 1320 1321 1322
    expect(box.size.width, equals(12.0));
    expect(box.size.height, equals(12.0));
    expect(builderIconSize, 12.0);
  });

1323 1324
  testWidgets('BottomNavigationBar responds to textScaleFactor', (WidgetTester tester) async {
    await tester.pumpWidget(
1325 1326 1327
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1328
            type: BottomNavigationBarType.fixed,
1329
            items: const <BottomNavigationBarItem>[
1330
              BottomNavigationBarItem(
1331
                label: 'A',
1332
                icon: Icon(Icons.ac_unit),
1333
              ),
1334
              BottomNavigationBarItem(
1335
                label: 'B',
1336
                icon: Icon(Icons.battery_alert),
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
              ),
            ],
          ),
        ),
      ),
    );

    final RenderBox defaultBox = tester.renderObject(find.byType(BottomNavigationBar));
    expect(defaultBox.size.height, equals(kBottomNavigationBarHeight));

    await tester.pumpWidget(
1348 1349 1350
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1351
            type: BottomNavigationBarType.shifting,
1352
            items: const <BottomNavigationBarItem>[
1353
              BottomNavigationBarItem(
1354
                label: 'A',
1355
                icon: Icon(Icons.ac_unit),
1356
              ),
1357
              BottomNavigationBarItem(
1358
                label: 'B',
1359
                icon: Icon(Icons.battery_alert),
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
              ),
            ],
          ),
        ),
      ),
    );

    final RenderBox shiftingBox = tester.renderObject(find.byType(BottomNavigationBar));
    expect(shiftingBox.size.height, equals(kBottomNavigationBarHeight));

    await tester.pumpWidget(
1371 1372
      MaterialApp(
        home: MediaQuery(
1373
          data: const MediaQueryData(textScaleFactor: 2.0),
1374 1375
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
1376
              items: const <BottomNavigationBarItem>[
1377
                BottomNavigationBarItem(
1378
                  label: 'A',
1379
                  icon: Icon(Icons.ac_unit),
1380
                ),
1381
                BottomNavigationBarItem(
1382
                  label: 'B',
1383
                  icon: Icon(Icons.battery_alert),
1384 1385 1386 1387 1388 1389 1390 1391 1392
                ),
              ],
            ),
          ),
        ),
      ),
    );

    final RenderBox box = tester.renderObject(find.byType(BottomNavigationBar));
1393
    expect(box.size.height, equals(56.0));
1394
  });
1395

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 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 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
  testWidgets('BottomNavigationBar does not grow with textScaleFactor when labels are provided', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                label: 'A',
                icon: Icon(Icons.ac_unit),
              ),
              BottomNavigationBarItem(
                label: 'B',
                icon: Icon(Icons.battery_alert),
              ),
            ],
          ),
        ),
      ),
    );

    final RenderBox defaultBox = tester.renderObject(find.byType(BottomNavigationBar));
    expect(defaultBox.size.height, equals(kBottomNavigationBarHeight));

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                label: 'A',
                icon: Icon(Icons.ac_unit),
              ),
              BottomNavigationBarItem(
                label: 'B',
                icon: Icon(Icons.battery_alert),
              ),
            ],
          ),
        ),
      ),
    );

    final RenderBox shiftingBox = tester.renderObject(find.byType(BottomNavigationBar));
    expect(shiftingBox.size.height, equals(kBottomNavigationBarHeight));

    await tester.pumpWidget(
      MaterialApp(
        home: MediaQuery(
          data: const MediaQueryData(textScaleFactor: 2.0),
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  label: 'A',
                  icon: Icon(Icons.ac_unit),
                ),
                BottomNavigationBarItem(
                  label: 'B',
                  icon: Icon(Icons.battery_alert),
                ),
              ],
            ),
          ),
        ),
      ),
    );

    final RenderBox box = tester.renderObject(find.byType(BottomNavigationBar));
    expect(box.size.height, equals(kBottomNavigationBarHeight));
  });

  testWidgets('BottomNavigationBar shows tool tips with text scaling on long press when labels are provided', (WidgetTester tester) async {
    const String label = 'Foo';

1472
    Widget buildApp({ required double textScaleFactor }) {
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
      return MediaQuery(
        data: MediaQueryData(textScaleFactor: textScaleFactor),
        child: Localizations(
          locale: const Locale('en', 'US'),
          delegates: const <LocalizationsDelegate<dynamic>>[
            DefaultMaterialLocalizations.delegate,
            DefaultWidgetsLocalizations.delegate,
          ],
          child: Directionality(
            textDirection: TextDirection.ltr,
            child: Navigator(
              onGenerateRoute: (RouteSettings settings) {
                return MaterialPageRoute<void>(
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
                  builder: (BuildContext context) {
                    return Scaffold(
                      bottomNavigationBar: BottomNavigationBar(
                        items: const <BottomNavigationBarItem>[
                          BottomNavigationBarItem(
                            label: label,
                            icon: Icon(Icons.ac_unit),
                            tooltip: label,
                          ),
                          BottomNavigationBarItem(
                            label: 'B',
                            icon: Icon(Icons.battery_alert),
                          ),
                        ],
                      ),
                    );
                  },
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
                );
              },
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp(textScaleFactor: 1.0));
    expect(find.text(label), findsOneWidget);
    await tester.longPress(find.text(label));
    expect(find.text(label), findsNWidgets(2));
    expect(tester.getSize(find.text(label).last), equals(const Size(42.0, 14.0)));
    await tester.pumpAndSettle(const Duration(seconds: 2));

    await tester.pumpWidget(buildApp(textScaleFactor: 4.0));
    expect(find.text(label), findsOneWidget);
    await tester.longPress(find.text(label));
    expect(tester.getSize(find.text(label).last), equals(const Size(168.0, 56.0)));
  });

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
  testWidgets('Different behaviour of tool tip in BottomNavigationBarItem', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                label: 'A',
                tooltip: 'A tooltip',
                icon: Icon(Icons.ac_unit),
              ),
              BottomNavigationBarItem(
                label: 'B',
                icon: Icon(Icons.battery_alert),
              ),
              BottomNavigationBarItem(
                label: 'C',
                icon: Icon(Icons.cake),
                tooltip: '',
              ),
            ],
          ),
        ),
      ),
    );

    expect(find.text('A'), findsOneWidget);
    await tester.longPress(find.text('A'));
    expect(find.byTooltip('A tooltip'), findsOneWidget);

    expect(find.text('B'), findsOneWidget);
    await tester.longPress(find.text('B'));
1556
    expect(find.byTooltip('B'), findsNothing);
1557 1558 1559 1560 1561 1562

    expect(find.text('C'), findsOneWidget);
    await tester.longPress(find.text('C'));
    expect(find.byTooltip('C'), findsNothing);
  });

1563 1564 1565
  testWidgets('BottomNavigationBar limits width of tiles with long labels', (WidgetTester tester) async {
    final String longTextA = List<String>.generate(100, (int index) => 'A').toString();
    final String longTextB = List<String>.generate(100, (int index) => 'B').toString();
1566 1567

    await tester.pumpWidget(
1568 1569 1570
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1571
            items: <BottomNavigationBarItem>[
1572
              BottomNavigationBarItem(
1573
                label: longTextA,
1574 1575
                icon: const Icon(Icons.ac_unit),
              ),
1576
              BottomNavigationBarItem(
1577
                label: longTextB,
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
                icon: const Icon(Icons.battery_alert),
              ),
            ],
          ),
        ),
      ),
    );

    final RenderBox box = tester.renderObject(find.byType(BottomNavigationBar));
    expect(box.size.height, equals(kBottomNavigationBarHeight));

1589
    final RenderBox itemBoxA = tester.renderObject(find.text(longTextA));
1590
    expect(itemBoxA.size, equals(const Size(400.0, 14.0)));
1591
    final RenderBox itemBoxB = tester.renderObject(find.text(longTextB));
1592
    expect(itemBoxB.size, equals(const Size(400.0, 14.0)));
1593
  });
1594 1595 1596 1597 1598

  testWidgets('BottomNavigationBar paints circles', (WidgetTester tester) async {
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.ltr,
1599
        bottomNavigationBar: BottomNavigationBar(
1600
          items: const <BottomNavigationBarItem>[
1601
            BottomNavigationBarItem(
1602
              label: 'A',
1603
              icon: Icon(Icons.ac_unit),
1604
            ),
1605
            BottomNavigationBarItem(
1606
              label: 'B',
1607
              icon: Icon(Icons.battery_alert),
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
            ),
          ],
        ),
      ),
    );

    final RenderBox box = tester.renderObject(find.byType(BottomNavigationBar));
    expect(box, isNot(paints..circle()));

    await tester.tap(find.text('A'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 20));
    expect(box, paints..circle(x: 200.0));

    await tester.tap(find.text('B'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 20));
1625
    expect(box, paints..circle(x: 200.0)..translate(x: 400.0)..circle(x: 200.0));
1626 1627 1628 1629 1630

    // Now we flip the directionality and verify that the circles switch positions.
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
1631
        bottomNavigationBar: BottomNavigationBar(
1632
          items: const <BottomNavigationBarItem>[
1633
            BottomNavigationBarItem(
1634
              label: 'A',
1635
              icon: Icon(Icons.ac_unit),
1636
            ),
1637
            BottomNavigationBarItem(
1638
              label: 'B',
1639
              icon: Icon(Icons.battery_alert),
1640 1641 1642 1643 1644 1645
            ),
          ],
        ),
      ),
    );

1646
    expect(box, paints..translate()..save()..translate(x: 400.0)..circle(x: 200.0)..restore()..circle(x: 200.0));
1647 1648 1649 1650

    await tester.tap(find.text('A'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 20));
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
    expect(
        box,
        paints
          ..translate(x: 0.0, y: 0.0)
          ..save()
          ..translate(x: 400.0)
          ..circle(x: 200.0)
          ..restore()
          ..circle(x: 200.0)
          ..translate(x: 400.0)
1661
          ..circle(x: 200.0),
1662
    );
1663
  });
1664

1665
  testWidgets('BottomNavigationBar inactiveIcon shown', (WidgetTester tester) async {
1666 1667
    const Key filled = Key('filled');
    const Key stroked = Key('stroked');
1668 1669 1670 1671 1672
    int selectedItem = 0;

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.ltr,
1673
        bottomNavigationBar: BottomNavigationBar(
1674 1675
          currentIndex: selectedItem,
          items:  const <BottomNavigationBarItem>[
1676 1677 1678
            BottomNavigationBarItem(
              activeIcon: Icon(Icons.favorite, key: filled),
              icon: Icon(Icons.favorite_border, key: stroked),
1679
              label: 'Favorite',
1680
            ),
1681 1682
            BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm),
1683
              label: 'Alarm',
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
            ),
          ],
        ),
      ),
    );

    expect(find.byKey(filled), findsOneWidget);
    expect(find.byKey(stroked), findsNothing);
    selectedItem = 1;

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.ltr,
1697
        bottomNavigationBar: BottomNavigationBar(
1698 1699
          currentIndex: selectedItem,
          items:  const <BottomNavigationBarItem>[
1700 1701 1702
            BottomNavigationBarItem(
              activeIcon: Icon(Icons.favorite, key: filled),
              icon: Icon(Icons.favorite_border, key: stroked),
1703
              label: 'Favorite',
1704
            ),
1705 1706
            BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm),
1707
              label: 'Alarm',
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
            ),
          ],
        ),
      ),
    );

    expect(find.byKey(filled), findsNothing);
    expect(find.byKey(stroked), findsOneWidget);
  });

1718
  testWidgets('BottomNavigationBar.fixed semantics', (WidgetTester tester) async {
1719 1720 1721
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.ltr,
1722
        bottomNavigationBar: BottomNavigationBar(
1723
          items: const <BottomNavigationBarItem>[
1724 1725
            BottomNavigationBarItem(
              icon: Icon(Icons.ac_unit),
1726
              label: 'AC',
1727
            ),
1728 1729
            BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm),
1730
              label: 'Alarm',
1731
            ),
1732 1733
            BottomNavigationBarItem(
              icon: Icon(Icons.hot_tub),
1734
              label: 'Hot Tub',
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
    expect(
      tester.getSemantics(find.text('AC')),
      matchesSemantics(
        label: 'AC\nTab 1 of 3',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        isSelected: true,
        hasTapAction: true,
      ),
    );
    expect(
      tester.getSemantics(find.text('Alarm')),
      matchesSemantics(
        label: 'Alarm\nTab 2 of 3',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        hasTapAction: true,
      ),
    );
    expect(
      tester.getSemantics(find.text('Hot Tub')),
      matchesSemantics(
        label: 'Hot Tub\nTab 3 of 3',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        hasTapAction: true,
      ),
1768
    );
1769 1770 1771 1772 1773 1774
  });

  testWidgets('BottomNavigationBar.shifting semantics', (WidgetTester tester) async {
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.ltr,
1775
        bottomNavigationBar: BottomNavigationBar(
1776 1777
          type: BottomNavigationBarType.shifting,
          items: const <BottomNavigationBarItem>[
1778 1779
            BottomNavigationBarItem(
              icon: Icon(Icons.ac_unit),
1780
              label: 'AC',
1781
            ),
1782 1783
            BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm),
1784
              label: 'Alarm',
1785
            ),
1786 1787
            BottomNavigationBarItem(
              icon: Icon(Icons.hot_tub),
1788
              label: 'Hot Tub',
1789 1790 1791 1792 1793 1794
            ),
          ],
        ),
      ),
    );

1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
    expect(
      tester.getSemantics(find.text('AC')),
      matchesSemantics(
        label: 'AC\nTab 1 of 3',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        isSelected: true,
        hasTapAction: true,
      ),
    );
    expect(
      tester.getSemantics(find.text('Alarm')),
      matchesSemantics(
        label: 'Alarm\nTab 2 of 3',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        hasTapAction: true,
      ),
    );
    expect(
      tester.getSemantics(find.text('Hot Tub')),
      matchesSemantics(
        label: 'Hot Tub\nTab 3 of 3',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        hasTapAction: true,
      ),
1822
    );
1823 1824
  });

1825 1826 1827 1828
  testWidgets('BottomNavigationBar handles items.length changes', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/10322

    Widget buildFrame(int itemCount) {
1829 1830 1831
      return MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
1832
            type: BottomNavigationBarType.fixed,
1833 1834
            items: List<BottomNavigationBarItem>.generate(itemCount, (int itemIndex) {
              return BottomNavigationBarItem(
1835
                icon: const Icon(Icons.android),
1836
                label: 'item $itemIndex',
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
              );
            }),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(3));
    expect(find.text('item 0'), findsOneWidget);
    expect(find.text('item 1'), findsOneWidget);
    expect(find.text('item 2'), findsOneWidget);
    expect(find.text('item 3'), findsNothing);

    await tester.pumpWidget(buildFrame(4));
    expect(find.text('item 0'), findsOneWidget);
    expect(find.text('item 1'), findsOneWidget);
    expect(find.text('item 2'), findsOneWidget);
    expect(find.text('item 3'), findsOneWidget);

    await tester.pumpWidget(buildFrame(2));
    expect(find.text('item 0'), findsOneWidget);
    expect(find.text('item 1'), findsOneWidget);
    expect(find.text('item 2'), findsNothing);
    expect(find.text('item 3'), findsNothing);
  });
1862 1863 1864 1865

  testWidgets('BottomNavigationBar change backgroundColor test', (WidgetTester tester) async {
    // Regression test for: https://github.com/flutter/flutter/issues/19653

1866
    Color backgroundColor = Colors.red;
1867 1868

    await tester.pumpWidget(
1869 1870
      MaterialApp(
        home: StatefulBuilder(
1871
          builder: (BuildContext context, StateSetter setState) {
1872 1873
            return Scaffold(
              body: Center(
1874
                child: ElevatedButton(
1875 1876 1877
                  child: const Text('green'),
                  onPressed: () {
                    setState(() {
1878
                      backgroundColor = Colors.green;
1879 1880 1881 1882
                    });
                  },
                ),
              ),
1883
              bottomNavigationBar: BottomNavigationBar(
1884 1885
                type: BottomNavigationBarType.shifting,
                items: <BottomNavigationBarItem>[
1886
                  BottomNavigationBarItem(
1887
                    label: 'Page 1',
1888
                    backgroundColor: backgroundColor,
1889 1890
                    icon: const Icon(Icons.dashboard),
                  ),
1891
                  BottomNavigationBarItem(
1892
                    label: 'Page 2',
1893
                    backgroundColor: backgroundColor,
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
                    icon: const Icon(Icons.menu),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );

    final Finder backgroundMaterial = find.descendant(
      of: find.byType(BottomNavigationBar),
      matching: find.byWidgetPredicate((Widget w) {
1907
        if (w is Material) {
1908
          return w.type == MaterialType.canvas;
1909
        }
1910 1911 1912 1913
        return false;
      }),
    );

1914
    expect(backgroundColor, Colors.red);
1915 1916 1917
    expect(tester.widget<Material>(backgroundMaterial).color, Colors.red);
    await tester.tap(find.text('green'));
    await tester.pumpAndSettle();
1918
    expect(backgroundColor, Colors.green);
1919 1920
    expect(tester.widget<Material>(backgroundMaterial).color, Colors.green);
  });
1921

1922
  group('BottomNavigationBar shifting backgroundColor with transition', () {
1923
    // Regression test for: https://github.com/flutter/flutter/issues/22226
1924
    Widget runTest() {
1925
      int currentIndex = 0;
1926
      return MaterialApp(
1927 1928 1929 1930 1931 1932
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              bottomNavigationBar: RepaintBoundary(
                child: BottomNavigationBar(
                  type: BottomNavigationBarType.shifting,
1933
                  currentIndex: currentIndex,
1934 1935
                  onTap: (int index) {
                    setState(() {
1936
                      currentIndex = index;
1937 1938 1939 1940
                    });
                  },
                  items: const <BottomNavigationBarItem>[
                    BottomNavigationBarItem(
1941
                      label: 'Red',
1942 1943 1944 1945
                      backgroundColor: Colors.red,
                      icon: Icon(Icons.dashboard),
                    ),
                    BottomNavigationBarItem(
1946
                      label: 'Green',
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
                      backgroundColor: Colors.green,
                      icon: Icon(Icons.menu),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      );
    }
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
    for (int pump = 1; pump < 9; pump++) {
      testWidgets('pump $pump', (WidgetTester tester) async {
        await tester.pumpWidget(runTest());
        await tester.tap(find.text('Green'));

        for (int i = 0; i < pump; i++) {
          await tester.pump(const Duration(milliseconds: 30));
        }
        await expectLater(
          find.byType(BottomNavigationBar),
          matchesGoldenFile('bottom_navigation_bar.shifting_transition.${pump - 1}.png'),
        );
1970
      });
1971 1972
    }
  });
1973

1974
  testWidgets('BottomNavigationBar item label should not be nullable', (WidgetTester tester) async {
1975 1976
    expect(() {
      MaterialApp(
1977 1978 1979 1980 1981 1982
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.shifting,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
1983
                label: 'AC',
1984 1985 1986 1987 1988 1989 1990 1991
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.access_alarm),
              ),
            ],
          ),
        ),
      );
Dan Field's avatar
Dan Field committed
1992
    }, throwsAssertionError);
1993
  });
1994

1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
  testWidgets(
    'BottomNavigationBar [showSelectedLabels]=false and [showUnselectedLabels]=false '
    'for shifting navbar, expect that there is no rendered text',
    (WidgetTester tester) async {
      final Widget widget = MaterialApp(
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              bottomNavigationBar: BottomNavigationBar(
                showSelectedLabels: false,
                showUnselectedLabels: false,
                type: BottomNavigationBarType.shifting,
                items: const <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
2009
                    label: 'Red',
2010 2011 2012 2013
                    backgroundColor: Colors.red,
                    icon: Icon(Icons.dashboard),
                  ),
                  BottomNavigationBarItem(
2014
                    label: 'Green',
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
                    backgroundColor: Colors.green,
                    icon: Icon(Icons.menu),
                  ),
                ],
              ),
            );
          },
        ),
      );
      await tester.pumpWidget(widget);
      expect(find.text('Red'), findsOneWidget);
      expect(find.text('Green'), findsOneWidget);
2027 2028
      expect(tester.widget<Visibility>(find.byType(Visibility).first).visible, false);
      expect(tester.widget<Visibility>(find.byType(Visibility).last).visible, false);
2029 2030
    },
  );
2031 2032 2033 2034 2035 2036 2037

  testWidgets(
    'BottomNavigationBar [showSelectedLabels]=false and [showUnselectedLabels]=false '
    'for fixed navbar, expect that there is no rendered text',
    (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
2038 2039 2040 2041 2042 2043
          home: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Scaffold(
                bottomNavigationBar: BottomNavigationBar(
                  showSelectedLabels: false,
                  showUnselectedLabels: false,
2044
                  type: BottomNavigationBarType.fixed,
2045 2046
                  items: const <BottomNavigationBarItem>[
                    BottomNavigationBarItem(
2047
                      label: 'Red',
2048 2049 2050 2051
                      backgroundColor: Colors.red,
                      icon: Icon(Icons.dashboard),
                    ),
                    BottomNavigationBarItem(
2052
                      label: 'Green',
2053 2054 2055 2056 2057 2058 2059 2060
                      backgroundColor: Colors.green,
                      icon: Icon(Icons.menu),
                    ),
                  ],
                ),
              );
            },
          ),
2061 2062 2063 2064
        ),
      );
      expect(find.text('Red'), findsOneWidget);
      expect(find.text('Green'), findsOneWidget);
2065 2066
      expect(tester.widget<Visibility>(find.byType(Visibility).first).visible, false);
      expect(tester.widget<Visibility>(find.byType(Visibility).last).visible, false);
2067 2068
    },
  );
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079

  testWidgets('BottomNavigationBar.fixed [showSelectedLabels]=false and [showUnselectedLabels]=false semantics', (WidgetTester tester) async {
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.ltr,
        bottomNavigationBar: BottomNavigationBar(
          showSelectedLabels: false,
          showUnselectedLabels: false,
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.ac_unit),
2080
              label: 'Red',
2081 2082 2083
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm),
2084
              label: 'Green',
2085 2086 2087 2088 2089 2090
            ),
          ],
        ),
      ),
    );

2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
    expect(
      tester.getSemantics(find.text('Red')),
      matchesSemantics(
        label: 'Red\nTab 1 of 2',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        isSelected: true,
        hasTapAction: true,
      ),
    );
    expect(
      tester.getSemantics(find.text('Green')),
      matchesSemantics(
        label: 'Green\nTab 2 of 2',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        hasTapAction: true,
      ),
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
    );
  });

  testWidgets('BottomNavigationBar.shifting [showSelectedLabels]=false and [showUnselectedLabels]=false semantics', (WidgetTester tester) async {
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.ltr,
        bottomNavigationBar: BottomNavigationBar(
          showSelectedLabels: false,
          showUnselectedLabels: false,
          type: BottomNavigationBarType.shifting,
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.ac_unit),
2123
              label: 'Red',
2124 2125 2126
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm),
2127
              label: 'Green',
2128 2129 2130 2131 2132 2133
            ),
          ],
        ),
      ),
    );

2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
    expect(
      tester.getSemantics(find.text('Red')),
      matchesSemantics(
        label: 'Red\nTab 1 of 2',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        isSelected: true,
        hasTapAction: true,
      ),
    );
    expect(
      tester.getSemantics(find.text('Green')),
      matchesSemantics(
        label: 'Green\nTab 2 of 2',
        textDirection: TextDirection.ltr,
        isFocusable: true,
        hasTapAction: true,
      ),
2152 2153 2154
    );
  });

2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
  testWidgets('BottomNavigationBar changes mouse cursor when the tile is hovered over', (WidgetTester tester) async {
    // Test BottomNavigationBar() constructor
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: MouseRegion(
            cursor: SystemMouseCursors.forbidden,
            child: BottomNavigationBar(
              mouseCursor: SystemMouseCursors.text,
              items: const <BottomNavigationBarItem>[
2165 2166
                BottomNavigationBarItem(icon: Icon(Icons.ac_unit), label: 'AC'),
                BottomNavigationBarItem(icon: Icon(Icons.access_alarm), label: 'Alarm'),
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
              ],
            ),
          ),
        ),
      ),
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: tester.getCenter(find.text('AC')));

2177
    await tester.pumpAndSettle();
2178

2179
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
2180 2181 2182 2183 2184 2185 2186 2187 2188

    // Test default cursor
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: MouseRegion(
            cursor: SystemMouseCursors.forbidden,
            child: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
2189 2190
                BottomNavigationBarItem(icon: Icon(Icons.ac_unit), label: 'AC'),
                BottomNavigationBarItem(icon: Icon(Icons.access_alarm), label: 'Alarm'),
2191 2192 2193 2194 2195 2196 2197
              ],
            ),
          ),
        ),
      ),
    );

2198
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
2199
  });
2200

2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
  group('feedback', () {
    late FeedbackTester feedback;

    setUp(() {
      feedback = FeedbackTester();
    });

    tearDown(() {
      feedback.dispose();
    });

    Widget feedbackBoilerplate({bool? enableFeedback, bool? enableFeedbackTheme}) {
      return MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBarTheme(
            data: BottomNavigationBarThemeData(
              enableFeedback: enableFeedbackTheme,
            ),
            child: BottomNavigationBar(
              enableFeedback: enableFeedback,
              items: const <BottomNavigationBarItem>[
2222 2223
                BottomNavigationBarItem(icon: Icon(Icons.ac_unit), label: 'AC'),
                BottomNavigationBarItem(icon: Icon(Icons.access_alarm), label: 'Alarm'),
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 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
              ],
            ),
          ),
        ),
      );
    }

    testWidgets('BottomNavigationBar with enabled feedback', (WidgetTester tester) async {
      const bool enableFeedback = true;

      await tester.pumpWidget(feedbackBoilerplate(enableFeedback: enableFeedback));

      await tester.tap(find.byType(InkResponse).first);
      await tester.pumpAndSettle();
      expect(feedback.clickSoundCount, 1);
      expect(feedback.hapticCount, 0);
    });

    testWidgets('BottomNavigationBar with disabled feedback', (WidgetTester tester) async {
      const bool enableFeedback = false;

      await tester.pumpWidget(feedbackBoilerplate(enableFeedback: enableFeedback));

      await tester.tap(find.byType(InkResponse).first);
      await tester.pumpAndSettle();
      expect(feedback.clickSoundCount, 0);
      expect(feedback.hapticCount, 0);
    });

    testWidgets('BottomNavigationBar with enabled feedback by default', (WidgetTester tester) async {
      await tester.pumpWidget(feedbackBoilerplate());

      await tester.tap(find.byType(InkResponse).first);
      await tester.pumpAndSettle();
      expect(feedback.clickSoundCount, 1);
      expect(feedback.hapticCount, 0);
    });

    testWidgets('BottomNavigationBar with disabled feedback using BottomNavigationBarTheme', (WidgetTester tester) async {
      const bool enableFeedbackTheme = false;

      await tester.pumpWidget(feedbackBoilerplate(enableFeedbackTheme: enableFeedbackTheme));

      await tester.tap(find.byType(InkResponse).first);
      await tester.pumpAndSettle();
      expect(feedback.clickSoundCount, 0);
      expect(feedback.hapticCount, 0);
    });

    testWidgets('BottomNavigationBar.enableFeedback overrides BottomNavigationBarTheme.enableFeedback', (WidgetTester tester) async {
      const bool enableFeedbackTheme = false;
      const bool enableFeedback = true;

      await tester.pumpWidget(feedbackBoilerplate(
        enableFeedbackTheme: enableFeedbackTheme,
2279
        enableFeedback: enableFeedback,
2280 2281 2282 2283 2284 2285 2286 2287 2288
      ));

      await tester.tap(find.byType(InkResponse).first);
      await tester.pumpAndSettle();
      expect(feedback.clickSoundCount, 1);
      expect(feedback.hapticCount, 0);
    });
  });

2289
  testWidgets('BottomNavigationBar excludes semantics', (WidgetTester tester) async {
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
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                label: 'A',
                icon: Icon(Icons.ac_unit),
              ),
              BottomNavigationBarItem(
                label: 'B',
                icon: Icon(Icons.battery_alert),
              ),
            ],
          ),
        ),
      ),
    );

    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(
                              flags: <SemanticsFlag>[
                                SemanticsFlag.isSelected,
2329
                                SemanticsFlag.isFocusable,
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
                              ],
                              actions: <SemanticsAction>[SemanticsAction.tap],
                              label: 'A\nTab 1 of 2',
                              textDirection: TextDirection.ltr,
                            ),
                            TestSemantics(
                              flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
                              actions: <SemanticsAction>[SemanticsAction.tap],
                              label: 'B\nTab 2 of 2',
                              textDirection: TextDirection.ltr,
                            ),
                          ],
                        ),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
        ignoreId: true,
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    semantics.dispose();
  });
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

  testWidgets('BottomNavigationBar default layout', (WidgetTester tester) async {
    final Key icon0 = UniqueKey();
    final Key icon1 = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            return Scaffold(
              bottomNavigationBar: BottomNavigationBar(
                items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                    icon: SizedBox(key: icon0, width: 200, height: 10),
2373
                    label: 'Title0',
2374 2375 2376
                  ),
                  BottomNavigationBarItem(
                    icon: SizedBox(key: icon1, width: 200, height: 10),
2377
                    label: 'Title1',
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
    expect(tester.getSize(find.byType(BottomNavigationBar)), const Size(800, kBottomNavigationBarHeight));
    expect(tester.getRect(find.byType(BottomNavigationBar)), const Rect.fromLTRB(0, 600 - kBottomNavigationBarHeight, 800, 600));

    // The height of the navigation bar is kBottomNavigationBarHeight = 56
    // The top of the navigation bar is 600 - 56 = 544
    // The top and bottom of the selected item is defined by its centered icon/label column:
2392
    //   top = 544 + ((56 - (10 + 10)) / 2) = 562
2393
    //   bottom = top + 10 + 10 = 582
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
    expect(tester.getRect(find.byKey(icon0)).top, 560.0);
    expect(tester.getRect(find.text('Title0')).bottom, 584.0);

    // The items are padded horizontally according to
    // MainAxisAlignment.spaceAround. Left/right padding is:
    // 800 - (200 * 2) / 4 = 100
    // The layout of the unselected item's label is slightly different; not
    // checking that here.
    expect(tester.getRect(find.text('Title0')), const Rect.fromLTRB(158.0, 570.0, 242.0, 584.0));
    expect(tester.getRect(find.byKey(icon0)), const Rect.fromLTRB(100.0, 560.0, 300.0, 570.0));
    expect(tester.getRect(find.byKey(icon1)), const Rect.fromLTRB(500.0, 560.0, 700.0, 570.0));
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
  });

  testWidgets('BottomNavigationBar centered landscape layout', (WidgetTester tester) async {
    final Key icon0 = UniqueKey();
    final Key icon1 = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            return Scaffold(
              bottomNavigationBar: BottomNavigationBar(
                landscapeLayout: BottomNavigationBarLandscapeLayout.centered,
                items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                    icon: SizedBox(key: icon0, width: 200, height: 10),
2421
                    label: 'Title0',
2422 2423 2424
                  ),
                  BottomNavigationBarItem(
                    icon: SizedBox(key: icon1, width: 200, height: 10),
2425
                    label: 'Title1',
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );

    expect(tester.getSize(find.byType(BottomNavigationBar)), const Size(800, kBottomNavigationBarHeight));
    expect(tester.getRect(find.byType(BottomNavigationBar)), const Rect.fromLTRB(0, 600 - kBottomNavigationBarHeight, 800, 600));

2438
    // The items are laid out as in the default case, within width = 600
2439
    // (the "portrait" width) and the result is centered with the
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
    // landscape width = 800.
    // So item 0's left edges are:
    // ((800 - 600) / 2) + ((600 - 400) / 4) = 150.
    // Item 1's right edge is:
    // 800 - 150 = 650
    // The layout of the unselected item's label is slightly different; not
    // checking that here.
    expect(tester.getRect(find.text('Title0')), const Rect.fromLTRB(208.0, 570.0, 292.0, 584.0));
    expect(tester.getRect(find.byKey(icon0)), const Rect.fromLTRB(150.0, 560.0, 350.0, 570.0));
    expect(tester.getRect(find.byKey(icon1)), const Rect.fromLTRB(450.0, 560.0, 650.0, 570.0));
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
  });

  testWidgets('BottomNavigationBar linear landscape layout', (WidgetTester tester) async {
    final Key icon0 = UniqueKey();
    final Key icon1 = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Builder(
          builder: (BuildContext context) {
            return Scaffold(
              bottomNavigationBar: BottomNavigationBar(
                landscapeLayout: BottomNavigationBarLandscapeLayout.linear,
                items: <BottomNavigationBarItem>[
                  BottomNavigationBarItem(
                    icon: SizedBox(key: icon0, width: 100, height: 20),
2466
                    label: 'Title0',
2467 2468 2469
                  ),
                  BottomNavigationBarItem(
                    icon: SizedBox(key: icon1, width: 100, height: 20),
2470
                    label: 'Title1',
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );

    expect(tester.getSize(find.byType(BottomNavigationBar)), const Size(800, kBottomNavigationBarHeight));
    expect(tester.getRect(find.byType(BottomNavigationBar)), const Rect.fromLTRB(0, 600 - kBottomNavigationBarHeight, 800, 600));

    // The items are laid out as in the default case except each
2484 2485 2486 2487 2488 2489
    // item's icon/label is arranged in a row, with 8 pixels in
    // between the icon and label.  The layout of the unselected
    // item's label is slightly different; not checking that here.
    expect(tester.getRect(find.text('Title0')), const Rect.fromLTRB(212.0, 565.0, 296.0, 579.0));
    expect(tester.getRect(find.byKey(icon0)), const Rect.fromLTRB(104.0, 562.0, 204.0, 582.0));
    expect(tester.getRect(find.byKey(icon1)), const Rect.fromLTRB(504.0, 562.0, 604.0, 582.0));
2490
  });
2491 2492
}

2493
Widget boilerplate({ Widget? bottomNavigationBar, required TextDirection textDirection }) {
2494
  assert(textDirection != null);
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
  return MaterialApp(
    home: Localizations(
      locale: const Locale('en', 'US'),
      delegates: const <LocalizationsDelegate<dynamic>>[
        DefaultMaterialLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
      ],
      child: Directionality(
        textDirection: textDirection,
        child: MediaQuery(
          data: const MediaQueryData(),
          child: Material(
            child: Scaffold(
              bottomNavigationBar: bottomNavigationBar,
            ),
2510
          ),
2511 2512 2513 2514
        ),
      ),
    ),
  );
2515
}
2516 2517 2518 2519 2520 2521

double _getOpacity(WidgetTester tester, String textValue) {
  final FadeTransition opacityWidget = tester.widget<FadeTransition>(
      find.ancestor(
        of: find.text(textValue),
        matching: find.byType(FadeTransition),
2522
      ).first,
2523 2524 2525 2526 2527 2528 2529 2530 2531
  );
  return opacityWidget.opacity.value;
}

Material _getMaterial(WidgetTester tester) {
  return tester.firstWidget<Material>(
    find.descendant(of: find.byType(BottomNavigationBar), matching: find.byType(Material)),
  );
}
2532 2533 2534 2535 2536

TextStyle _iconStyle(WidgetTester tester, IconData icon) {
  final RichText iconRichText = tester.widget<RichText>(
      find.descendant(of: find.byIcon(icon), matching: find.byType(RichText)),
  );
2537
  return iconRichText.text.style!;
2538 2539 2540 2541 2542 2543
}

EdgeInsets _itemPadding(WidgetTester tester, IconData icon) {
  return tester.widget<Padding>(
      find.descendant(
        of: find.ancestor(of: find.byIcon(icon), matching: find.byType(InkResponse)),
2544
        matching: find.byType(Padding),
2545 2546 2547
      ).first,
    ).padding.resolve(TextDirection.ltr);
}