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

import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';

import 'typography.dart';

/// Material design text theme.
///
12
/// Definitions for the various typographical styles found in Material Design
13
/// (e.g., labelLarge, bodySmall). Rather than creating a [TextTheme] directly,
14 15 16 17 18
/// you can obtain an instance as [Typography.black] or [Typography.white].
///
/// To obtain the current text theme, call [Theme.of] with the current
/// [BuildContext] and read the [ThemeData.textTheme] property.
///
19
/// The names of the TextTheme properties match this table from the
20 21 22 23 24 25 26 27 28
/// [Material Design spec](https://m3.material.io/styles/typography/tokens).
///
/// ![](https://lh3.googleusercontent.com/Yvngs5mQSjXa_9T4X3JDucO62c5hdZHPDa7qeRH6DsJQvGr_q7EBrTkhkPiQd9OeR1v_Uk38Cjd9nUpP3nevDyHpKWuXSfQ1Gq78bOnBN7sr=s0)
///
/// The Material Design typography scheme was significantly changed in the
/// current (2021) version of the specification
/// ([https://m3.material.io/styles/typography/tokens](https://m3.material.io/styles/typography/tokens)).
///
/// The names of the 2018 TextTheme properties match this table from the
29 30 31 32 33
/// [Material Design spec](https://material.io/design/typography/the-type-system.html#type-scale)
/// with two exceptions: the styles called H1-H6 in the spec are
/// headline1-headline6 in the API, and body1,body2 are called
/// bodyText1 and bodyText2.
///
34
/// The 2018 spec has thirteen text styles:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
///
/// | NAME       | SIZE |  WEIGHT |  SPACING |             |
/// |------------|------|---------|----------|-------------|
/// | headline1  | 96.0 | light   | -1.5     |             |
/// | headline2  | 60.0 | light   | -0.5     |             |
/// | headline3  | 48.0 | regular |  0.0     |             |
/// | headline4  | 34.0 | regular |  0.25    |             |
/// | headline5  | 24.0 | regular |  0.0     |             |
/// | headline6  | 20.0 | medium  |  0.15    |             |
/// | subtitle1  | 16.0 | regular |  0.15    |             |
/// | subtitle2  | 14.0 | medium  |  0.1     |             |
/// | body1      | 16.0 | regular |  0.5     | (bodyText1) |
/// | body2      | 14.0 | regular |  0.25    | (bodyText2) |
/// | button     | 14.0 | medium  |  1.25    |             |
/// | caption    | 12.0 | regular |  0.4     |             |
/// | overline   | 10.0 | regular |  1.5     |             |
51 52
///
/// ...where "light" is `FontWeight.w300`, "regular" is `FontWeight.w400` and
53 54
/// "medium" is `FontWeight.w500`.
///
55
/// By default, text styles are initialized to match the 2018 Material Design
56
/// specification as listed above. To provide backwards compatibility, the 2014
57
/// specification is also available.
58
///
59 60 61 62 63 64
/// To explicitly configure a [Theme] for the 2018 sizes, weights, and letter
/// spacings, you can initialize its [ThemeData.typography] value using
/// [Typography.material2018]. The [Typography] constructor defaults to this
/// configuration. To configure a [Theme] for the 2014 sizes, weights, and letter
/// spacings, initialize its [ThemeData.typography] value using
/// [Typography.material2014].
65 66 67 68
///
/// See also:
///
///  * [Typography], the class that generates [TextTheme]s appropriate for a platform.
69
///  * [Theme], for other aspects of a Material Design application that can be
70
///    globally adjusted, such as the color scheme.
71
///  * <https://material.io/design/typography/>
72
@immutable
73
class TextTheme with Diagnosticable {
74 75 76 77
  /// Creates a text theme that uses the given values.
  ///
  /// Rather than creating a new text theme, consider using [Typography.black]
  /// or [Typography.white], which implement the typography styles in the
78
  /// Material Design specification:
79
  ///
80
  /// <https://material.io/design/typography/#type-scale>
81 82 83
  ///
  /// If you do decide to create your own text theme, consider using one of
  /// those predefined themes as a starting point for [copyWith] or [apply].
84 85 86 87
  ///
  /// Please note that you can not mix and match the 2018 styles with the 2021
  /// styles. Only one or the other is allowed in this constructor. The 2018
  /// styles will be deprecated and removed eventually.
88
  const TextTheme({
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    TextStyle? displayLarge,
    TextStyle? displayMedium,
    TextStyle? displaySmall,
    this.headlineLarge,
    TextStyle? headlineMedium,
    TextStyle? headlineSmall,
    TextStyle? titleLarge,
    TextStyle? titleMedium,
    TextStyle? titleSmall,
    TextStyle? bodyLarge,
    TextStyle? bodyMedium,
    TextStyle? bodySmall,
    TextStyle? labelLarge,
    this.labelMedium,
    TextStyle? labelSmall,
104 105 106 107
    @Deprecated(
      'Use displayLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
108
    TextStyle? headline1,
109 110 111 112
    @Deprecated(
      'Use displayMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
113
    TextStyle? headline2,
114 115 116 117
    @Deprecated(
      'Use displaySmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
118
    TextStyle? headline3,
119 120 121 122
    @Deprecated(
      'Use headlineMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
123
    TextStyle? headline4,
124 125 126 127
    @Deprecated(
      'Use headlineSmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
128
    TextStyle? headline5,
129 130 131 132
    @Deprecated(
      'Use titleLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
133
    TextStyle? headline6,
134 135 136 137
    @Deprecated(
      'Use titleMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
138
    TextStyle? subtitle1,
139 140 141 142
    @Deprecated(
      'Use titleSmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
143
    TextStyle? subtitle2,
144 145 146 147
    @Deprecated(
      'Use bodyLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
148
    TextStyle? bodyText1,
149 150 151 152
    @Deprecated(
      'Use bodyMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
153
    TextStyle? bodyText2,
154 155 156 157
    @Deprecated(
      'Use bodySmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
158
    TextStyle? caption,
159 160 161 162
    @Deprecated(
      'Use labelLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
163
    TextStyle? button,
164 165 166 167
    @Deprecated(
      'Use labelSmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    TextStyle? overline,
  }) : assert(
         (displayLarge == null && displayMedium == null && displaySmall == null && headlineMedium == null &&
             headlineSmall == null && titleLarge == null && titleMedium == null && titleSmall == null &&
             bodyLarge == null && bodyMedium == null && bodySmall == null && labelLarge == null && labelSmall == null) ||
         (headline1 == null && headline2 == null && headline3 == null && headline4 == null &&
             headline5 == null && headline6 == null && subtitle1 == null && subtitle2 == null &&
             bodyText1 == null && bodyText2 == null && caption == null && button == null && overline == null),
         'Cannot mix 2018 and 2021 terms in call to TextTheme() constructor.'
       ),
       displayLarge = displayLarge ?? headline1,
       displayMedium = displayMedium ?? headline2,
       displaySmall = displaySmall ?? headline3,
       headlineMedium = headlineMedium ?? headline4,
       headlineSmall = headlineSmall ?? headline5,
       titleLarge = titleLarge ?? headline6,
       titleMedium = titleMedium ?? subtitle1,
       titleSmall = titleSmall ?? subtitle2,
       bodyLarge = bodyLarge ?? bodyText1,
       bodyMedium = bodyMedium ?? bodyText2,
       bodySmall = bodySmall ?? caption,
       labelLarge = labelLarge ?? button,
       labelSmall = labelSmall ?? overline;

  /// Largest of the display styles.
  ///
  /// As the largest text on the screen, display styles are reserved for short,
  /// important text or numerals. They work best on large screens.
  final TextStyle? displayLarge;

  /// Middle size of the display styles.
  ///
  /// As the largest text on the screen, display styles are reserved for short,
  /// important text or numerals. They work best on large screens.
  final TextStyle? displayMedium;

  /// Smallest of the display styles.
  ///
  /// As the largest text on the screen, display styles are reserved for short,
  /// important text or numerals. They work best on large screens.
  final TextStyle? displaySmall;

  /// Largest of the headline styles.
  ///
  /// Headline styles are smaller than display styles. They're best-suited for
  /// short, high-emphasis text on smaller screens.
  final TextStyle? headlineLarge;

  /// Middle size of the headline styles.
  ///
  /// Headline styles are smaller than display styles. They're best-suited for
  /// short, high-emphasis text on smaller screens.
  final TextStyle? headlineMedium;

  /// Smallest of the headline styles.
  ///
  /// Headline styles are smaller than display styles. They're best-suited for
  /// short, high-emphasis text on smaller screens.
  final TextStyle? headlineSmall;

  /// Largest of the title styles.
  ///
  /// Titles are smaller than headline styles and should be used for shorter,
  /// medium-emphasis text.
  final TextStyle? titleLarge;

  /// Middle size of the title styles.
  ///
  /// Titles are smaller than headline styles and should be used for shorter,
  /// medium-emphasis text.
  final TextStyle? titleMedium;

  /// Smallest of the title styles.
  ///
  /// Titles are smaller than headline styles and should be used for shorter,
  /// medium-emphasis text.
  final TextStyle? titleSmall;

  /// Largest of the body styles.
  ///
  /// Body styles are used for longer passages of text.
  final TextStyle? bodyLarge;

  /// Middle size of the body styles.
  ///
  /// Body styles are used for longer passages of text.
  ///
  /// The default text style for [Material].
  final TextStyle? bodyMedium;

  /// Smallest of the body styles.
  ///
  /// Body styles are used for longer passages of text.
  final TextStyle? bodySmall;

  /// Largest of the label styles.
  ///
  /// Label styles are smaller, utilitarian styles, used for areas of the UI
  /// such as text inside of components or very small supporting text in the
  /// content body, like captions.
  ///
  /// Used for text on [ElevatedButton], [TextButton] and [OutlinedButton].
  final TextStyle? labelLarge;

  /// Middle size of the label styles.
  ///
  /// Label styles are smaller, utilitarian styles, used for areas of the UI
  /// such as text inside of components or very small supporting text in the
  /// content body, like captions.
  final TextStyle? labelMedium;

  /// Smallest of the label styles.
  ///
  /// Label styles are smaller, utilitarian styles, used for areas of the UI
  /// such as text inside of components or very small supporting text in the
  /// content body, like captions.
  final TextStyle? labelSmall;
285 286

  /// Extremely large text.
287 288 289 290
  @Deprecated(
    'Use displayLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
291
  TextStyle? get headline1 => displayLarge;
292 293 294 295

  /// Very, very large text.
  ///
  /// Used for the date in the dialog shown by [showDatePicker].
296 297 298 299
  @Deprecated(
    'Use displayMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
300
  TextStyle? get headline2 => displayMedium;
301 302

  /// Very large text.
303 304 305 306
  @Deprecated(
    'Use displaySmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
307
  TextStyle? get headline3 => displaySmall;
308 309

  /// Large text.
310 311 312 313
  @Deprecated(
    'Use headlineMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
314
  TextStyle? get headline4 => headlineMedium;
315 316 317

  /// Used for large text in dialogs (e.g., the month and year in the dialog
  /// shown by [showDatePicker]).
318 319 320 321
  @Deprecated(
    'Use headlineSmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
322
  TextStyle? get headline5 => headlineSmall;
323 324 325

  /// Used for the primary text in app bars and dialogs (e.g., [AppBar.title]
  /// and [AlertDialog.title]).
326 327 328 329
  @Deprecated(
    'Use titleLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
330
  TextStyle? get headline6 => titleLarge;
331 332

  /// Used for the primary text in lists (e.g., [ListTile.title]).
333 334 335 336
  @Deprecated(
    'Use titleMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
337
  TextStyle? get subtitle1 => titleMedium;
338

339 340 341 342 343
  /// For medium emphasis text that's a little smaller than [titleMedium].
  @Deprecated(
    'Use titleSmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
344
  TextStyle? get subtitle2 => titleSmall;
345

346 347 348 349 350
  /// Used for emphasizing text that would otherwise be [bodyMedium].
  @Deprecated(
    'Use bodyLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
351
  TextStyle? get bodyText1 => bodyLarge;
352

353
  /// The default text style for [Material].
354 355 356 357
  @Deprecated(
    'Use bodyMedium instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
358
  TextStyle? get bodyText2 => bodyMedium;
359 360

  /// Used for auxiliary text associated with images.
361 362 363 364
  @Deprecated(
    'Use bodySmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
365
  TextStyle? get caption => bodySmall;
366

367
  /// Used for text on [ElevatedButton], [TextButton] and [OutlinedButton].
368 369 370 371
  @Deprecated(
    'Use labelLarge instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
372
  TextStyle? get button => labelLarge;
373

374
  /// The smallest style.
375 376
  ///
  /// Typically used for captions or to introduce a (larger) headline.
377 378 379 380
  @Deprecated(
    'Use labelSmall instead. '
    'This feature was deprecated after v3.1.0-0.0.pre.',
  )
381
  TextStyle? get overline => labelSmall;
382 383 384 385 386

  /// Creates a copy of this text theme but with the given fields replaced with
  /// the new values.
  ///
  /// Consider using [Typography.black] or [Typography.white], which implement
387
  /// the typography styles in the Material Design specification, as a starting
388 389
  /// point.
  ///
390
  /// {@tool snippet}
391 392 393 394 395
  ///
  /// ```dart
  /// /// A Widget that sets the ambient theme's title text color for its
  /// /// descendants, while leaving other ambient theme attributes alone.
  /// class TitleColorThemeCopy extends StatelessWidget {
396
  ///   const TitleColorThemeCopy({super.key, required this.titleColor, required this.child});
397 398 399 400 401 402 403 404 405 406
  ///
  ///   final Color titleColor;
  ///   final Widget child;
  ///
  ///   @override
  ///   Widget build(BuildContext context) {
  ///     final ThemeData theme = Theme.of(context);
  ///     return Theme(
  ///       data: theme.copyWith(
  ///         textTheme: theme.textTheme.copyWith(
407
  ///           titleLarge: theme.textTheme.titleLarge!.copyWith(
408 409 410 411 412 413 414 415 416
  ///             color: titleColor,
  ///           ),
  ///         ),
  ///       ),
  ///       child: child,
  ///     );
  ///   }
  /// }
  /// ```
417
  /// {@end-tool}
418 419 420
  ///
  /// See also:
  ///
421 422
  ///  * [merge] is used instead of [copyWith] when you want to merge all
  ///    of the fields of a TextTheme instead of individual fields.
423
  TextTheme copyWith({
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    TextStyle? displayLarge,
    TextStyle? displayMedium,
    TextStyle? displaySmall,
    TextStyle? headlineLarge,
    TextStyle? headlineMedium,
    TextStyle? headlineSmall,
    TextStyle? titleLarge,
    TextStyle? titleMedium,
    TextStyle? titleSmall,
    TextStyle? bodyLarge,
    TextStyle? bodyMedium,
    TextStyle? bodySmall,
    TextStyle? labelLarge,
    TextStyle? labelMedium,
    TextStyle? labelSmall,
439 440 441 442
    @Deprecated(
      'Use displayLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
443
    TextStyle? headline1,
444 445 446 447
    @Deprecated(
      'Use displayMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
448
    TextStyle? headline2,
449 450 451 452
    @Deprecated(
      'Use displaySmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
453
    TextStyle? headline3,
454 455 456 457
    @Deprecated(
      'Use headlineMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
458
    TextStyle? headline4,
459 460 461 462
    @Deprecated(
      'Use headlineSmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
463
    TextStyle? headline5,
464 465 466 467
    @Deprecated(
      'Use titleLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
468
    TextStyle? headline6,
469 470 471 472
    @Deprecated(
      'Use titleMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
473
    TextStyle? subtitle1,
474 475 476 477
    @Deprecated(
      'Use titleSmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
478
    TextStyle? subtitle2,
479 480 481 482
    @Deprecated(
      'Use bodyLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
483
    TextStyle? bodyText1,
484 485 486 487
    @Deprecated(
      'Use bodyMedium instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
488
    TextStyle? bodyText2,
489 490 491 492
    @Deprecated(
      'Use bodySmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
493
    TextStyle? caption,
494 495 496 497
    @Deprecated(
      'Use labelLarge instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
498
    TextStyle? button,
499 500 501 502
    @Deprecated(
      'Use labelSmall instead. '
      'This feature was deprecated after v3.1.0-0.0.pre.',
    )
503
    TextStyle? overline,
504
  }) {
505 506 507 508 509 510 511 512 513
    assert(
      (displayLarge == null && displayMedium == null && displaySmall == null && headlineMedium == null &&
          headlineSmall == null && titleLarge == null && titleMedium == null && titleSmall == null &&
          bodyLarge == null && bodyMedium == null && bodySmall == null && labelLarge == null && labelSmall == null) ||
      (headline1 == null && headline2 == null && headline3 == null && headline4 == null &&
          headline5 == null && headline6 == null && subtitle1 == null && subtitle2 == null &&
          bodyText1 == null && bodyText2 == null && caption == null && button == null && overline == null),
      'Cannot mix 2018 and 2021 terms in call to TextTheme() constructor.'
    );
514
    return TextTheme(
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
      displayLarge: displayLarge ?? headline1 ?? this.displayLarge,
      displayMedium: displayMedium ?? headline2 ?? this.displayMedium,
      displaySmall: displaySmall ?? headline3 ?? this.displaySmall,
      headlineLarge: headlineLarge ?? this.headlineLarge,
      headlineMedium: headlineMedium ?? headline4 ?? this.headlineMedium,
      headlineSmall: headlineSmall ?? headline5 ?? this.headlineSmall,
      titleLarge: titleLarge ?? headline6 ?? this.titleLarge,
      titleMedium: titleMedium ?? subtitle1 ?? this.titleMedium,
      titleSmall: titleSmall ?? subtitle2 ?? this.titleSmall,
      bodyLarge: bodyLarge ?? bodyText1 ?? this.bodyLarge,
      bodyMedium: bodyMedium ?? bodyText2 ?? this.bodyMedium,
      bodySmall: bodySmall ?? caption ?? this.bodySmall,
      labelLarge: labelLarge ?? button ?? this.labelLarge,
      labelMedium: labelMedium ?? this.labelMedium,
      labelSmall: labelSmall ?? overline ?? this.labelSmall,
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    );
  }

  /// Creates a new [TextTheme] where each text style from this object has been
  /// merged with the matching text style from the `other` object.
  ///
  /// The merging is done by calling [TextStyle.merge] on each respective pair
  /// of text styles from this and the [other] text themes and is subject to
  /// the value of [TextStyle.inherit] flag. For more details, see the
  /// documentation on [TextStyle.merge] and [TextStyle.inherit].
  ///
  /// If this theme, or the `other` theme has members that are null, then the
  /// non-null one (if any) is used. If the `other` theme is itself null, then
  /// this [TextTheme] is returned unchanged. If values in both are set, then
  /// the values are merged using [TextStyle.merge].
  ///
  /// This is particularly useful if one [TextTheme] defines one set of
  /// properties and another defines a different set, e.g. having colors
  /// defined in one text theme and font sizes in another, or when one
  /// [TextTheme] has only some fields defined, and you want to define the rest
  /// by merging it with a default theme.
  ///
552
  /// {@tool snippet}
553 554 555 556 557
  ///
  /// ```dart
  /// /// A Widget that sets the ambient theme's title text color for its
  /// /// descendants, while leaving other ambient theme attributes alone.
  /// class TitleColorTheme extends StatelessWidget {
558
  ///   const TitleColorTheme({super.key, required this.child, required this.titleColor});
559 560 561 562 563 564 565 566 567 568 569 570
  ///
  ///   final Color titleColor;
  ///   final Widget child;
  ///
  ///   @override
  ///   Widget build(BuildContext context) {
  ///     ThemeData theme = Theme.of(context);
  ///     // This partialTheme is incomplete: it only has the title style
  ///     // defined. Just replacing theme.textTheme with partialTheme would
  ///     // set the title, but everything else would be null. This isn't very
  ///     // useful, so merge it with the existing theme to keep all of the
  ///     // preexisting definitions for the other styles.
571
  ///     final TextTheme partialTheme = TextTheme(titleLarge: TextStyle(color: titleColor));
572 573 574 575 576
  ///     theme = theme.copyWith(textTheme: theme.textTheme.merge(partialTheme));
  ///     return Theme(data: theme, child: child);
  ///   }
  /// }
  /// ```
577
  /// {@end-tool}
578 579 580
  ///
  /// See also:
  ///
581 582 583
  ///  * [copyWith] is used instead of [merge] when you wish to override
  ///    individual fields in the [TextTheme] instead of merging all of the
  ///    fields of two [TextTheme]s.
584
  TextTheme merge(TextTheme? other) {
585
    if (other == null) {
586
      return this;
587
    }
588
    return copyWith(
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
      displayLarge: displayLarge?.merge(other.displayLarge) ?? other.displayLarge,
      displayMedium: displayMedium?.merge(other.displayMedium) ?? other.displayMedium,
      displaySmall: displaySmall?.merge(other.displaySmall) ?? other.displaySmall,
      headlineLarge: headlineLarge?.merge(other.headlineLarge) ?? other.headlineLarge,
      headlineMedium: headlineMedium?.merge(other.headlineMedium) ?? other.headlineMedium,
      headlineSmall: headlineSmall?.merge(other.headlineSmall) ?? other.headlineSmall,
      titleLarge: titleLarge?.merge(other.titleLarge) ?? other.titleLarge,
      titleMedium: titleMedium?.merge(other.titleMedium) ?? other.titleMedium,
      titleSmall: titleSmall?.merge(other.titleSmall) ?? other.titleSmall,
      bodyLarge: bodyLarge?.merge(other.bodyLarge) ?? other.bodyLarge,
      bodyMedium: bodyMedium?.merge(other.bodyMedium) ?? other.bodyMedium,
      bodySmall: bodySmall?.merge(other.bodySmall) ?? other.bodySmall,
      labelLarge: labelLarge?.merge(other.labelLarge) ?? other.labelLarge,
      labelMedium: labelMedium?.merge(other.labelMedium) ?? other.labelMedium,
      labelSmall: labelSmall?.merge(other.labelSmall) ?? other.labelSmall,
604 605 606 607 608 609
    );
  }

  /// Creates a copy of this text theme but with the given field replaced in
  /// each of the individual text styles.
  ///
610 611 612
  /// The `displayColor` is applied to [displayLarge], [displayMedium],
  /// [displaySmall], [headlineLarge], [headlineMedium], and [bodySmall]. The
  /// `bodyColor` is applied to the remaining text styles.
613 614
  ///
  /// Consider using [Typography.black] or [Typography.white], which implement
615
  /// the typography styles in the Material Design specification, as a starting
616 617
  /// point.
  TextTheme apply({
618
    String? fontFamily,
619 620
    double fontSizeFactor = 1.0,
    double fontSizeDelta = 0.0,
621 622 623 624 625
    Color? displayColor,
    Color? bodyColor,
    TextDecoration? decoration,
    Color? decorationColor,
    TextDecorationStyle? decorationStyle,
626 627
  }) {
    return TextTheme(
628 629 630 631 632 633 634 635 636 637
      displayLarge: displayLarge?.apply(
        color: displayColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
      displayMedium: displayMedium?.apply(
638 639 640 641 642 643 644 645
        color: displayColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
646
      displaySmall: displaySmall?.apply(
647 648 649 650 651 652 653 654
        color: displayColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
655
      headlineLarge: headlineLarge?.apply(
656 657 658 659 660 661 662 663
        color: displayColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
664
      headlineMedium: headlineMedium?.apply(
665 666 667 668 669 670 671 672
        color: displayColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
673
      headlineSmall: headlineSmall?.apply(
674
        color: bodyColor,
675 676 677 678 679 680 681
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
682
      titleLarge: titleLarge?.apply(
683
        color: bodyColor,
684 685 686 687 688 689 690
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
691
      titleMedium: titleMedium?.apply(
692
        color: bodyColor,
693 694 695 696 697 698 699
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
700
      titleSmall: titleSmall?.apply(
701
        color: bodyColor,
702 703 704 705 706 707 708
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
709
      bodyLarge: bodyLarge?.apply(
710
        color: bodyColor,
711 712 713 714 715 716 717
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
718
      bodyMedium: bodyMedium?.apply(
719
        color: bodyColor,
720 721 722 723 724 725 726
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
727
      bodySmall: bodySmall?.apply(
728
        color: displayColor,
729 730 731 732 733 734 735
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
736 737 738 739 740 741 742 743 744 745
      labelLarge: labelLarge?.apply(
        color: bodyColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
      labelMedium: labelMedium?.apply(
746 747 748 749 750 751 752 753
        color: bodyColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
754
      labelSmall: labelSmall?.apply(
755 756 757 758 759 760 761 762 763 764 765 766 767
        color: bodyColor,
        decoration: decoration,
        decorationColor: decorationColor,
        decorationStyle: decorationStyle,
        fontFamily: fontFamily,
        fontSizeFactor: fontSizeFactor,
        fontSizeDelta: fontSizeDelta,
      ),
    );
  }

  /// Linearly interpolate between two text themes.
  ///
768
  /// {@macro dart.ui.shadow.lerp}
769
  static TextTheme lerp(TextTheme? a, TextTheme? b, double t) {
770 771
    assert(t != null);
    return TextTheme(
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
      displayLarge: TextStyle.lerp(a?.displayLarge, b?.displayLarge, t),
      displayMedium: TextStyle.lerp(a?.displayMedium, b?.displayMedium, t),
      displaySmall: TextStyle.lerp(a?.displaySmall, b?.displaySmall, t),
      headlineLarge: TextStyle.lerp(a?.headlineLarge, b?.headlineLarge, t),
      headlineMedium: TextStyle.lerp(a?.headlineMedium, b?.headlineMedium, t),
      headlineSmall: TextStyle.lerp(a?.headlineSmall, b?.headlineSmall, t),
      titleLarge: TextStyle.lerp(a?.titleLarge, b?.titleLarge, t),
      titleMedium: TextStyle.lerp(a?.titleMedium, b?.titleMedium, t),
      titleSmall: TextStyle.lerp(a?.titleSmall, b?.titleSmall, t),
      bodyLarge: TextStyle.lerp(a?.bodyLarge, b?.bodyLarge, t),
      bodyMedium: TextStyle.lerp(a?.bodyMedium, b?.bodyMedium, t),
      bodySmall: TextStyle.lerp(a?.bodySmall, b?.bodySmall, t),
      labelLarge: TextStyle.lerp(a?.labelLarge, b?.labelLarge, t),
      labelMedium: TextStyle.lerp(a?.labelMedium, b?.labelMedium, t),
      labelSmall: TextStyle.lerp(a?.labelSmall, b?.labelSmall, t),
787 788 789 790
    );
  }

  @override
791
  bool operator ==(Object other) {
792
    if (identical(this, other)) {
793
      return true;
794 795
    }
    if (other.runtimeType != runtimeType) {
796
      return false;
797
    }
798
    return other is TextTheme
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
      && displayLarge == other.displayLarge
      && displayMedium == other.displayMedium
      && displaySmall == other.displaySmall
      && headlineLarge == other.headlineLarge
      && headlineMedium == other.headlineMedium
      && headlineSmall == other.headlineSmall
      && titleLarge == other.titleLarge
      && titleMedium == other.titleMedium
      && titleSmall == other.titleSmall
      && bodyLarge == other.bodyLarge
      && bodyMedium == other.bodyMedium
      && bodySmall == other.bodySmall
      && labelLarge == other.labelLarge
      && labelMedium == other.labelMedium
      && labelSmall == other.labelSmall;
814 815 816
  }

  @override
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
  int get hashCode => Object.hash(
    displayLarge,
    displayMedium,
    displaySmall,
    headlineLarge,
    headlineMedium,
    headlineSmall,
    titleLarge,
    titleMedium,
    titleSmall,
    bodyLarge,
    bodyMedium,
    bodySmall,
    labelLarge,
    labelMedium,
    labelSmall,
  );
834 835 836 837

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
838
    final TextTheme defaultTheme = Typography.material2018(platform: defaultTargetPlatform).black;
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    properties.add(DiagnosticsProperty<TextStyle>('displayLarge', displayLarge, defaultValue: defaultTheme.displayLarge));
    properties.add(DiagnosticsProperty<TextStyle>('displayMedium', displayMedium, defaultValue: defaultTheme.displayMedium));
    properties.add(DiagnosticsProperty<TextStyle>('displaySmall', displaySmall, defaultValue: defaultTheme.displaySmall));
    properties.add(DiagnosticsProperty<TextStyle>('headlineLarge', headlineLarge, defaultValue: defaultTheme.headlineLarge));
    properties.add(DiagnosticsProperty<TextStyle>('headlineMedium', headlineMedium, defaultValue: defaultTheme.headlineMedium));
    properties.add(DiagnosticsProperty<TextStyle>('headlineSmall', headlineSmall, defaultValue: defaultTheme.headlineSmall));
    properties.add(DiagnosticsProperty<TextStyle>('titleLarge', titleLarge, defaultValue: defaultTheme.titleLarge));
    properties.add(DiagnosticsProperty<TextStyle>('titleMedium', titleMedium, defaultValue: defaultTheme.titleMedium));
    properties.add(DiagnosticsProperty<TextStyle>('titleSmall', titleSmall, defaultValue: defaultTheme.titleSmall));
    properties.add(DiagnosticsProperty<TextStyle>('bodyLarge', bodyLarge, defaultValue: defaultTheme.bodyLarge));
    properties.add(DiagnosticsProperty<TextStyle>('bodyMedium', bodyMedium, defaultValue: defaultTheme.bodyMedium));
    properties.add(DiagnosticsProperty<TextStyle>('bodySmall', bodySmall, defaultValue: defaultTheme.bodySmall));
    properties.add(DiagnosticsProperty<TextStyle>('labelLarge', labelLarge, defaultValue: defaultTheme.labelLarge));
    properties.add(DiagnosticsProperty<TextStyle>('labelMedium', labelMedium, defaultValue: defaultTheme.labelMedium));
    properties.add(DiagnosticsProperty<TextStyle>('labelSmall', labelSmall, defaultValue: defaultTheme.labelSmall));
854 855
  }
}