app_bar_theme_test.dart 27.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6 7 8 9 10 11 12 13 14 15 16 17
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('AppBarTheme copyWith, ==, hashCode basics', () {
    expect(const AppBarTheme(), const AppBarTheme().copyWith());
    expect(const AppBarTheme().hashCode, const AppBarTheme().copyWith().hashCode);
  });

  testWidgets('Passing no AppBarTheme returns defaults', (WidgetTester tester) async {
18
    final ThemeData theme = ThemeData();
19 20
    await tester.pumpWidget(
      MaterialApp(
21
        theme: theme,
22 23 24
        home: Scaffold(
          appBar: AppBar(
            actions: <Widget>[
25
              IconButton(icon: const Icon(Icons.share), onPressed: () { }),
26 27 28 29 30
            ],
          ),
        ),
      ),
    );
31 32 33

    final Material widget = _getAppBarMaterial(tester);
    final IconTheme iconTheme = _getAppBarIconTheme(tester);
34 35
    final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
    final RichText actionIconText = _getAppBarIconRichText(tester);
36 37
    final DefaultTextStyle text = _getAppBarText(tester);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    if (theme.useMaterial3) {
      expect(SystemChrome.latestStyle!.statusBarBrightness, Brightness.light);
      expect(widget.color, theme.colorScheme.surface);
      expect(widget.elevation, 0);
      expect(widget.shadowColor, null);
      expect(widget.surfaceTintColor, theme.colorScheme.surfaceTint);
      expect(widget.shape, null);
      expect(iconTheme.data, IconThemeData(color: theme.colorScheme.onSurface, size: 24));
      expect(actionsIconTheme.data, IconThemeData(color: theme.colorScheme.onSurfaceVariant, size: 24));
      expect(actionIconText.text.style!.color, Colors.black);
      expect(text.style, Typography.material2021().englishLike.bodyText2!.merge(Typography.material2021().black.bodyText2).copyWith(color: theme.colorScheme.onSurface));
      expect(tester.getSize(find.byType(AppBar)).height, kToolbarHeight);
      expect(tester.getSize(find.byType(AppBar)).width, 800);
    } else {
      expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.light.statusBarBrightness);
      expect(widget.color, Colors.blue);
      expect(widget.elevation, 4.0);
      expect(widget.shadowColor, Colors.black);
      expect(widget.surfaceTintColor, null);
      expect(widget.shape, null);
      expect(iconTheme.data, const IconThemeData(color: Colors.white));
      expect(actionsIconTheme.data, const IconThemeData(color: Colors.white));
      expect(actionIconText.text.style!.color, Colors.white);
      expect(text.style, Typography.material2014().englishLike.bodyText2!.merge(Typography.material2014().white.bodyText2));
      expect(tester.getSize(find.byType(AppBar)).height, kToolbarHeight);
      expect(tester.getSize(find.byType(AppBar)).width, 800);
    }
65 66 67 68 69
  });

  testWidgets('AppBar uses values from AppBarTheme', (WidgetTester tester) async {
    final AppBarTheme appBarTheme = _appBarTheme();

70 71 72 73 74 75 76
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(appBarTheme: appBarTheme),
        home: Scaffold(
          appBar: AppBar(
            title: const Text('App Bar Title'),
            actions: <Widget>[
77
              IconButton(icon: const Icon(Icons.share), onPressed: () { }),
78 79 80 81 82
            ],
          ),
        ),
      ),
    );
83 84 85

    final Material widget = _getAppBarMaterial(tester);
    final IconTheme iconTheme = _getAppBarIconTheme(tester);
86 87
    final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
    final RichText actionIconText = _getAppBarIconRichText(tester);
88 89
    final DefaultTextStyle text = _getAppBarText(tester);

90
    expect(SystemChrome.latestStyle!.statusBarBrightness, appBarTheme.brightness);
91
    expect(widget.color, appBarTheme.backgroundColor);
92
    expect(widget.elevation, appBarTheme.elevation);
93
    expect(widget.shadowColor, appBarTheme.shadowColor);
94
    expect(widget.surfaceTintColor, appBarTheme.surfaceTintColor);
95
    expect(widget.shape, const StadiumBorder());
96
    expect(iconTheme.data, appBarTheme.iconTheme);
97
    expect(actionsIconTheme.data, appBarTheme.actionsIconTheme);
98
    expect(actionIconText.text.style!.color, appBarTheme.actionsIconTheme!.color);
99
    expect(text.style, appBarTheme.toolbarTextStyle);
100 101
    expect(tester.getSize(find.byType(AppBar)).height, appBarTheme.toolbarHeight);
    expect(tester.getSize(find.byType(AppBar)).width, 800);
102 103
  });

104 105 106 107 108 109 110 111
  testWidgets('SliverAppBar allows AppBar to determine backwardsCompatibility', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/77016
    const AppBarTheme appBarTheme = AppBarTheme(
      backwardsCompatibility: false,
      backgroundColor: Colors.lightBlue,
      foregroundColor: Colors.black,
    );

112
    Widget buildWithBackwardsCompatibility([bool? enabled]) => MaterialApp(
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      theme: ThemeData(appBarTheme: appBarTheme),
      home: Scaffold(body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            title: const Text('App Bar Title'),
            backwardsCompatibility: enabled,
            actions: <Widget>[
              IconButton(icon: const Icon(Icons.share), onPressed: () { }),
            ],
          ),
        ],
      )),
    );

    // Backwards compatibility enabled, AppBar should be built with true.
128
    await tester.pumpWidget(buildWithBackwardsCompatibility(true));
129 130 131 132
    AppBar appBar = tester.widget<AppBar>(find.byType(AppBar));
    expect(appBar.backwardsCompatibility, true);

    // Backwards compatibility disabled, AppBar should be built with false.
133
    await tester.pumpWidget(buildWithBackwardsCompatibility(false));
134 135 136 137
    appBar = tester.widget<AppBar>(find.byType(AppBar));
    expect(appBar.backwardsCompatibility, false);

    // Backwards compatibility unspecified, AppBar should be built with null.
138
    await tester.pumpWidget(buildWithBackwardsCompatibility());
139 140 141 142 143 144 145 146 147 148
    appBar = tester.widget<AppBar>(find.byType(AppBar));
    expect(appBar.backwardsCompatibility, null);

    // AppBar should use the backwardsCompatibility of AppBarTheme.
    // Since backwardsCompatibility is false, the text color should match the
    // foreground color of the AppBarTheme.
    final DefaultTextStyle text = _getAppBarText(tester);
    expect(text.style.color, appBarTheme.foregroundColor);
  });

149 150
  testWidgets('AppBar widget properties take priority over theme', (WidgetTester tester) async {
    const Brightness brightness = Brightness.dark;
151
    const SystemUiOverlayStyle systemOverlayStyle = SystemUiOverlayStyle.light;
152 153
    const Color color = Colors.orange;
    const double elevation = 3.0;
154 155
    const Color shadowColor = Colors.purple;
    const Color surfaceTintColor = Colors.brown;
156
    const ShapeBorder shape = RoundedRectangleBorder();
157
    const IconThemeData iconThemeData = IconThemeData(color: Colors.green);
158
    const IconThemeData actionsIconThemeData = IconThemeData(color: Colors.lightBlue);
159 160 161 162 163
    const TextStyle toolbarTextStyle = TextStyle(color: Colors.pink);
    const TextStyle titleTextStyle = TextStyle(color: Colors.orange);

    await tester.pumpWidget(
      MaterialApp(
164 165 166
        theme: ThemeData.from(colorScheme: const ColorScheme.light()).copyWith(
          appBarTheme: _appBarTheme(),
        ),
167 168 169 170 171 172 173
        home: Scaffold(
          appBar: AppBar(
            backgroundColor: color,
            brightness: brightness,
            systemOverlayStyle: systemOverlayStyle,
            elevation: elevation,
            shadowColor: shadowColor,
174
            surfaceTintColor: surfaceTintColor,
175
            shape: shape,
176 177 178 179 180 181 182 183 184 185 186
            iconTheme: iconThemeData,
            actionsIconTheme: actionsIconThemeData,
            toolbarTextStyle: toolbarTextStyle,
            titleTextStyle: titleTextStyle,
            actions: <Widget>[
              IconButton(icon: const Icon(Icons.share), onPressed: () { }),
            ],
          ),
        ),
      ),
    );
187 188 189

    final Material widget = _getAppBarMaterial(tester);
    final IconTheme iconTheme = _getAppBarIconTheme(tester);
190 191
    final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
    final RichText actionIconText = _getAppBarIconRichText(tester);
192 193
    final DefaultTextStyle text = _getAppBarText(tester);

194
    expect(SystemChrome.latestStyle!.statusBarBrightness, brightness);
195 196
    expect(widget.color, color);
    expect(widget.elevation, elevation);
197
    expect(widget.shadowColor, shadowColor);
198
    expect(widget.surfaceTintColor, surfaceTintColor);
199
    expect(widget.shape, shape);
200
    expect(iconTheme.data, iconThemeData);
201
    expect(actionsIconTheme.data, actionsIconThemeData);
202
    expect(actionIconText.text.style!.color, actionsIconThemeData.color);
203
    expect(text.style, toolbarTextStyle);
204 205
  });

206 207 208 209 210 211
  testWidgets('AppBar icon color takes priority over everything', (WidgetTester tester) async {
    const Color color = Colors.lime;
    const IconThemeData iconThemeData = IconThemeData(color: Colors.green);
    const IconThemeData actionsIconThemeData = IconThemeData(color: Colors.lightBlue);

    await tester.pumpWidget(MaterialApp(
212
      theme: ThemeData.from(colorScheme: const ColorScheme.light()),
213 214 215 216
      home: Scaffold(appBar: AppBar(
        iconTheme: iconThemeData,
        actionsIconTheme: actionsIconThemeData,
        actions: <Widget>[
217
          IconButton(icon: const Icon(Icons.share), color: color, onPressed: () { }),
218 219 220 221 222
        ],
      )),
    ));

    final RichText actionIconText = _getAppBarIconRichText(tester);
223
    expect(actionIconText.text.style!.color, color);
224 225
  });

226 227 228
  testWidgets('AppBarTheme properties take priority over ThemeData properties', (WidgetTester tester) async {
    final AppBarTheme appBarTheme = _appBarTheme();

229 230 231 232 233 234 235 236 237 238 239 240 241
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData.from(colorScheme: const ColorScheme.light())
          .copyWith(appBarTheme: _appBarTheme()),
        home: Scaffold(
          appBar: AppBar(
            actions: <Widget>[
              IconButton(icon: const Icon(Icons.share), onPressed: () { }),
            ],
          ),
        ),
      ),
    );
242 243 244

    final Material widget = _getAppBarMaterial(tester);
    final IconTheme iconTheme = _getAppBarIconTheme(tester);
245 246
    final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
    final RichText actionIconText = _getAppBarIconRichText(tester);
247 248
    final DefaultTextStyle text = _getAppBarText(tester);

249
    expect(SystemChrome.latestStyle!.statusBarBrightness, appBarTheme.brightness);
250
    expect(widget.color, appBarTheme.backgroundColor);
251
    expect(widget.elevation, appBarTheme.elevation);
252
    expect(widget.shadowColor, appBarTheme.shadowColor);
253
    expect(widget.surfaceTintColor, appBarTheme.surfaceTintColor);
254
    expect(iconTheme.data, appBarTheme.iconTheme);
255
    expect(actionsIconTheme.data, appBarTheme.actionsIconTheme);
256
    expect(actionIconText.text.style!.color, appBarTheme.actionsIconTheme!.color);
257
    expect(text.style, appBarTheme.toolbarTextStyle);
258 259
  });

260
  testWidgets('ThemeData colorScheme is used when no AppBarTheme is set', (WidgetTester tester) async {
261 262
    final ThemeData lightTheme = ThemeData.from(colorScheme: const ColorScheme.light());
    final ThemeData darkTheme = ThemeData.from(colorScheme: const ColorScheme.dark());
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    Widget buildFrame(ThemeData appTheme) {
      return MaterialApp(
        theme: appTheme,
        home: Builder(
          builder: (BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                actions: <Widget>[
                  IconButton(icon: const Icon(Icons.share), onPressed: () { }),
                ],
              ),
            );
          },
        ),
      );
    }

280 281 282 283 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 312 313 314 315 316 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 353 354 355 356 357 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 393
    if (lightTheme.useMaterial3) {
      // M3 AppBar defaults for light themes:
      // - elevation: 0
      // - shadow color: null
      // - surface tint color: ColorScheme.surfaceTint
      // - background color: ColorScheme.surface
      // - foreground color: ColorScheme.onSurface
      // - actions text: style bodyText2, foreground color
      // - status bar brightness: light (based on color scheme brightness)
      {
        await tester.pumpWidget(buildFrame(lightTheme));

        final Material widget = _getAppBarMaterial(tester);
        final IconTheme iconTheme = _getAppBarIconTheme(tester);
        final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
        final RichText actionIconText = _getAppBarIconRichText(tester);
        final DefaultTextStyle text = _getAppBarText(tester);

        expect(SystemChrome.latestStyle!.statusBarBrightness, Brightness.light);
        expect(widget.color, lightTheme.colorScheme.surface);
        expect(widget.elevation, 0);
        expect(widget.shadowColor, null);
        expect(widget.surfaceTintColor, lightTheme.colorScheme.surfaceTint);
        expect(iconTheme.data.color, lightTheme.colorScheme.onSurface);
        expect(actionsIconTheme.data.color, lightTheme.colorScheme.onSurface);
        expect(actionIconText.text.style!.color, lightTheme.colorScheme.onSurface);
        expect(text.style, Typography.material2021().englishLike.bodyText2!.merge(Typography.material2021().black.bodyText2).copyWith(color: lightTheme.colorScheme.onSurface));
      }

      // M3 AppBar defaults for dark themes:
      // - elevation: 0
      // - shadow color: null
      // - surface tint color: ColorScheme.surfaceTint
      // - background color: ColorScheme.surface
      // - foreground color: ColorScheme.onSurface
      // - actions text: style bodyText2, foreground color
      // - status bar brightness: dark (based on background color)
      {
        await tester.pumpWidget(buildFrame(ThemeData.from(colorScheme: const ColorScheme.dark())));
        await tester.pumpAndSettle(); // Theme change animation

        final Material widget = _getAppBarMaterial(tester);
        final IconTheme iconTheme = _getAppBarIconTheme(tester);
        final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
        final RichText actionIconText = _getAppBarIconRichText(tester);
        final DefaultTextStyle text = _getAppBarText(tester);

        expect(SystemChrome.latestStyle!.statusBarBrightness, Brightness.dark);
        expect(widget.color, darkTheme.colorScheme.surface);
        expect(widget.elevation, 0);
        expect(widget.shadowColor, null);
        expect(widget.surfaceTintColor, darkTheme.colorScheme.surfaceTint);
        expect(iconTheme.data.color, darkTheme.colorScheme.onSurface);
        expect(actionsIconTheme.data.color, darkTheme.colorScheme.onSurface);
        expect(actionIconText.text.style!.color, darkTheme.colorScheme.onSurface);
        expect(text.style, Typography.material2021().englishLike.bodyText2!.merge(Typography.material2021().black.bodyText2).copyWith(color: darkTheme.colorScheme.onSurface));
      }
    } else {
      // AppBar defaults for light themes:
      // - elevation: 4
      // - shadow color: black
      // - surface tint color: null
      // - background color: ColorScheme.primary
      // - foreground color: ColorScheme.onPrimary
      // - actions text: style bodyText2, foreground color
      // - status bar brightness: light (based on color scheme brightness)
      {
        await tester.pumpWidget(buildFrame(lightTheme));

        final Material widget = _getAppBarMaterial(tester);
        final IconTheme iconTheme = _getAppBarIconTheme(tester);
        final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
        final RichText actionIconText = _getAppBarIconRichText(tester);
        final DefaultTextStyle text = _getAppBarText(tester);

        expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.light.statusBarBrightness);
        expect(widget.color, lightTheme.colorScheme.primary);
        expect(widget.elevation, 4.0);
        expect(widget.shadowColor, Colors.black);
        expect(widget.surfaceTintColor, null);
        expect(iconTheme.data.color, lightTheme.colorScheme.onPrimary);
        expect(actionsIconTheme.data.color, lightTheme.colorScheme.onPrimary);
        expect(actionIconText.text.style!.color, lightTheme.colorScheme.onPrimary);
        expect(text.style, Typography.material2014().englishLike.bodyText2!.merge(Typography.material2014().black.bodyText2).copyWith(color: lightTheme.colorScheme.onPrimary));
      }

      // AppBar defaults for dark themes:
      // - elevation: 4
      // - shadow color: black
      // - surface tint color: null
      // - background color: ColorScheme.surface
      // - foreground color: ColorScheme.onSurface
      // - actions text: style bodyText2, foreground color
      // - status bar brightness: dark (based on background color)
      {
        await tester.pumpWidget(buildFrame(darkTheme));
        await tester.pumpAndSettle(); // Theme change animation

        final Material widget = _getAppBarMaterial(tester);
        final IconTheme iconTheme = _getAppBarIconTheme(tester);
        final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
        final RichText actionIconText = _getAppBarIconRichText(tester);
        final DefaultTextStyle text = _getAppBarText(tester);

        expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.light.statusBarBrightness);
        expect(widget.color, darkTheme.colorScheme.surface);
        expect(widget.elevation, 4.0);
        expect(widget.shadowColor, Colors.black);
        expect(widget.surfaceTintColor, null);
        expect(iconTheme.data.color, darkTheme.colorScheme.onSurface);
        expect(actionsIconTheme.data.color, darkTheme.colorScheme.onSurface);
        expect(actionIconText.text.style!.color, darkTheme.colorScheme.onSurface);
        expect(text.style, Typography.material2014().englishLike.bodyText2!.merge(Typography.material2014().black.bodyText2).copyWith(color: darkTheme.colorScheme.onSurface));
      }
394 395
    }
  });
396

397 398 399 400 401
  testWidgets('AppBar iconTheme with color=null defers to outer IconTheme', (WidgetTester tester) async {
    // Verify claim made in https://github.com/flutter/flutter/pull/71184#issuecomment-737419215

    Widget buildFrame({ Color? appIconColor, Color? appBarIconColor }) {
      return MaterialApp(
402
        theme: ThemeData.from(useMaterial3: false, colorScheme: const ColorScheme.light()),
403 404 405 406 407 408 409 410
        home: IconTheme(
          data: IconThemeData(color: appIconColor),
          child: Builder(
            builder: (BuildContext context) {
              return Scaffold(
                appBar: AppBar(
                  iconTheme: IconThemeData(color: appBarIconColor),
                  actions: <Widget>[
411
                    IconButton(icon: const Icon(Icons.share), onPressed: () { }),
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
                  ],
                ),
              );
            },
          ),
        ),
      );
    }

    RichText getIconText() {
      return tester.widget<RichText>(
        find.descendant(
          of: find.byType(Icon),
          matching: find.byType(RichText),
        ),
      );
    }
429

430
    await tester.pumpWidget(buildFrame(appIconColor: Colors.lime));
431
    expect(getIconText().text.style!.color, Colors.lime);
432

433 434
    await tester.pumpWidget(buildFrame(appIconColor: Colors.lime, appBarIconColor: Colors.purple));
    expect(getIconText().text.style!.color, Colors.purple);
435
  });
436 437 438 439

  testWidgets('AppBar uses AppBarTheme.centerTitle when centerTitle is null', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(centerTitle: true)),
440 441 442
      home: Scaffold(appBar: AppBar(
        title: const Text('Title'),
      )),
443 444 445 446 447 448 449 450 451 452
    ));

    final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
    expect(navToolBar.centerMiddle, true);
  });

  testWidgets('AppBar.centerTitle takes priority over AppBarTheme.centerTitle', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(centerTitle: true)),
      home: Scaffold(
453 454 455 456 457
        appBar: AppBar(
          title: const Text('Title'),
          centerTitle: false,
        ),
      ),
458 459 460 461 462 463 464 465 466 467
    ));

    final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
    // The AppBar.centerTitle should be used instead of AppBarTheme.centerTitle.
    expect(navToolBar.centerMiddle, false);
  });

  testWidgets('AppBar.centerTitle adapts to TargetPlatform when AppBarTheme.centerTitle is null', (WidgetTester tester) async{
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(platform: TargetPlatform.iOS),
468
      home: Scaffold(appBar: AppBar(
469
        title: const Text('Title'),
470
      )),
471 472 473 474 475 476 477
    ));

    final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
    // When ThemeData.platform is TargetPlatform.iOS, and AppBarTheme is null,
    // the value of NavigationToolBar.centerMiddle should be true.
    expect(navToolBar.centerMiddle, true);
  });
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

  testWidgets('AppBar.shadowColor takes priority over AppBarTheme.shadowColor', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(shadowColor: Colors.red)),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Title'),
          shadowColor: Colors.yellow,
        ),
      ),
    ));

    final AppBar appBar = tester.widget(find.byType(AppBar));
    // The AppBar.shadowColor should be used instead of AppBarTheme.shadowColor.
    expect(appBar.shadowColor, Colors.yellow);
  });
494

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
  testWidgets('AppBar.surfaceTintColor takes priority over AppBarTheme.surfaceTintColor', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(surfaceTintColor: Colors.red)),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Title'),
          surfaceTintColor: Colors.yellow,
        ),
      ),
    ));

    final AppBar appBar = tester.widget(find.byType(AppBar));
    // The AppBar.surfaceTintColor should be used instead of AppBarTheme.surfaceTintColor.
    expect(appBar.surfaceTintColor, Colors.yellow);
  });

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 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
  testWidgets('AppBar uses AppBarTheme.titleSpacing', (WidgetTester tester) async {
    const double kTitleSpacing = 10;
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(titleSpacing: kTitleSpacing)),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Title'),
        ),
      ),
    ));

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

  testWidgets('AppBar.titleSpacing takes priority over AppBarTheme.titleSpacing', (WidgetTester tester) async {
    const double kTitleSpacing = 10;
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(titleSpacing: kTitleSpacing)),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Title'),
          titleSpacing: 40,
        ),
      ),
    ));

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

  testWidgets('SliverAppBar uses AppBarTheme.titleSpacing', (WidgetTester tester) async {
    const double kTitleSpacing = 10;
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(titleSpacing: kTitleSpacing)),
      home: const CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            title: Text('Title'),
          ),
        ],
      ),
    ));

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

  testWidgets('SliverAppBar.titleSpacing takes priority over AppBarTheme.titleSpacing ', (WidgetTester tester) async {
    const double kTitleSpacing = 10;
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(appBarTheme: const AppBarTheme(titleSpacing: kTitleSpacing)),
      home: const CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            title: Text('Title'),
            titleSpacing: 40,
          ),
        ],
      ),
    ));

    final NavigationToolbar navToolbar = tester.widget(find.byType(NavigationToolbar));
    expect(navToolbar.middleSpacing, 40);
  });

  testWidgets('Default AppBarTheme debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const AppBarTheme().debugFillProperties(builder);

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString())
      .toList();

    expect(description, <String>[]);
  });

  testWidgets('AppBarTheme implements debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const AppBarTheme(
      brightness: Brightness.dark,
593
      backgroundColor: Color(0xff000001),
594 595
      elevation: 8.0,
      shadowColor: Color(0xff000002),
596
      surfaceTintColor: Color(0xff000003),
597 598 599 600 601 602 603 604 605 606 607
      centerTitle: true,
      titleSpacing: 40.0,
    ).debugFillProperties(builder);

    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString())
      .toList();

    expect(description, <String>[
      'brightness: Brightness.dark',
608
      'backgroundColor: Color(0xff000001)',
609 610
      'elevation: 8.0',
      'shadowColor: Color(0xff000002)',
611
      'surfaceTintColor: Color(0xff000003)',
612 613 614 615 616 617 618 619 620
      'centerTitle: true',
      'titleSpacing: 40.0',
    ]);

    // On the web, Dart doubles and ints are backed by the same kind of object because
    // JavaScript does not support integers. So, the Dart double "4.0" is identical
    // to "4", which results in the web evaluating to the value "4" regardless of which
    // one is used. This results in a difference for doubles in debugFillProperties between
    // the web and the rest of Flutter's target platforms.
621
  }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87364
622 623 624 625
}

AppBarTheme _appBarTheme() {
  const Brightness brightness = Brightness.light;
626
  const Color backgroundColor = Colors.lightBlue;
627
  const double elevation = 6.0;
628
  const Color shadowColor = Colors.red;
629
  const Color surfaceTintColor = Colors.green;
630
  const IconThemeData iconThemeData = IconThemeData(color: Colors.black);
631
  const IconThemeData actionsIconThemeData = IconThemeData(color: Colors.pink);
632
  return const AppBarTheme(
633
    actionsIconTheme: actionsIconThemeData,
634
    brightness: brightness,
635
    backgroundColor: backgroundColor,
636
    elevation: elevation,
637
    shadowColor: shadowColor,
638
    surfaceTintColor: surfaceTintColor,
639
    shape: StadiumBorder(),
640
    iconTheme: iconThemeData,
641
    toolbarHeight: 96,
642 643
    toolbarTextStyle: TextStyle(color: Colors.yellow),
    titleTextStyle: TextStyle(color: Colors.pink),
644 645 646
  );
}

647 648 649 650 651
Material _getAppBarMaterial(WidgetTester tester) {
  return tester.widget<Material>(
    find.descendant(
      of: find.byType(AppBar),
      matching: find.byType(Material),
652
    ),
653 654 655 656 657 658 659 660
  );
}

IconTheme _getAppBarIconTheme(WidgetTester tester) {
  return tester.widget<IconTheme>(
    find.descendant(
      of: find.byType(AppBar),
      matching: find.byType(IconTheme),
661 662 663 664 665 666 667 668 669 670
    ).first,
  );
}

IconTheme _getAppBarActionsIconTheme(WidgetTester tester) {
  return tester.widget<IconTheme>(
    find.descendant(
      of: find.byType(NavigationToolbar),
      matching: find.byType(IconTheme),
    ).first,
671 672 673
  );
}

674 675 676 677 678 679 680 681
RichText _getAppBarIconRichText(WidgetTester tester) {
  return tester.widget<RichText>(
    find.descendant(
      of: find.byType(Icon),
      matching: find.byType(RichText),
    ).first,
  );
}
682

683 684 685 686 687 688 689 690
DefaultTextStyle _getAppBarText(WidgetTester tester) {
  return tester.widget<DefaultTextStyle>(
    find.descendant(
      of: find.byType(CustomSingleChildLayout),
      matching: find.byType(DefaultTextStyle),
    ).first,
  );
}