Skip to content

Commit a8549ae

Browse files
authored
[material_ui] Deprecate the black/white parameters in Typography.material2021 (#13443)
Adds a deprecation message to the black/white parameters in the `Typography.material2021` factory constructor. Fixes flutter/flutter#195200 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 587ee23 commit a8549ae

3 files changed

Lines changed: 80 additions & 5 deletions

File tree

‎packages/material_ui/lib/src/typography.dart‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,52 @@ class Typography with Diagnosticable {
164164

165165
/// Creates a typography instance using Material Design 3 2021 defaults.
166166
///
167-
/// If [platform] is [TargetPlatform.iOS] or [TargetPlatform.macOS], the
168-
/// default values for [black] and [white] are [blackCupertino] and
169-
/// [whiteCupertino] respectively. Otherwise they are [blackMountainView] and
170-
/// [whiteMountainView]. If [platform] is null then both [black] and [white]
171-
/// must be specified.
167+
/// Text colors are derived from [colorScheme]. For a light [colorScheme],
168+
/// every style in the resulting [Typography.black] uses
169+
/// [ColorScheme.onSurface] and every style in [Typography.white] uses
170+
/// [ColorScheme.surface]. For a dark [colorScheme], these are reversed. The
171+
/// same colors are used for [TextStyle.decorationColor].
172+
///
173+
/// The default values for [black] and [white] depend on [platform]:
174+
/// [blackCupertino] and [whiteCupertino] on iOS, [blackRedwoodCity] and
175+
/// [whiteRedwoodCity] on macOS, [blackRedmond] and [whiteRedmond] on
176+
/// Windows, [blackHelsinki] and [whiteHelsinki] on Linux, and
177+
/// [blackMountainView] and [whiteMountainView] otherwise. If [platform] is
178+
/// null then both [black] and [white] must be specified.
172179
///
173180
/// The default values for [englishLike], [dense], and [tall] are
174181
/// [englishLike2021], [dense2021], and [tall2021].
175182
///
183+
/// The [black] and [white] parameters are deprecated. Only their non-color
184+
/// properties (such as font family) are used. Any [TextStyle.color] or
185+
/// [TextStyle.decorationColor] they specify is replaced with a color from
186+
/// [colorScheme] as described above. To customize the text theme's colors,
187+
/// call [copyWith] on the result:
188+
///
189+
/// ```dart
190+
/// final Typography base = Typography.material2021(colorScheme: colorScheme);
191+
/// final Typography typography = base.copyWith(
192+
/// black: base.black.merge(myBlack),
193+
/// white: base.white.merge(myWhite),
194+
/// );
195+
/// ```
196+
///
176197
/// See also:
177198
/// * <https://m3.material.io/styles/typography>
178199
factory Typography.material2021({
179200
TargetPlatform? platform = TargetPlatform.android,
180201
ColorScheme colorScheme = const ColorScheme.light(),
202+
@Deprecated(
203+
'Text colors are derived from colorScheme, so colors in this text theme are ignored. '
204+
'Use copyWith on the result to customize the black text theme. '
205+
'This feature was deprecated after material_ui v1.8.0.',
206+
)
181207
TextTheme? black,
208+
@Deprecated(
209+
'Text colors are derived from colorScheme, so colors in this text theme are ignored. '
210+
'Use copyWith on the result to customize the white text theme. '
211+
'This feature was deprecated after material_ui v1.8.0.',
212+
)
182213
TextTheme? white,
183214
TextTheme? englishLike,
184215
TextTheme? dense,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Deprecates the `black` and `white` parameters of `Typography.material2021`, and documents that their colors are replaced with colors from the `colorScheme`.
3+
version: minor

‎packages/material_ui/test/typography_test.dart‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,45 @@ void main() {
444444
expect(textTheme.labelMedium!.color, light);
445445
expect(textTheme.labelSmall!.color, light);
446446
});
447+
448+
group('Typography.material2021 deprecated black and white', () {
449+
const lightScheme = ColorScheme.light(onSurface: Color(0xFF000001), surface: Color(0xFFFFFFF1));
450+
const custom = TextTheme(
451+
bodyMedium: TextStyle(
452+
fontFamily: 'Custom',
453+
color: Color(0xFFFF0000),
454+
decorationColor: Color(0xFF00FF00),
455+
),
456+
);
457+
458+
test('keep non-color properties but replace colors from the color scheme', () {
459+
final typography = Typography.material2021(
460+
colorScheme: lightScheme,
461+
// ignore: deprecated_member_use_from_same_package
462+
black: custom,
463+
// ignore: deprecated_member_use_from_same_package
464+
white: custom,
465+
);
466+
expect(typography.black.bodyMedium!.fontFamily, 'Custom');
467+
expect(typography.black.bodyMedium!.color, lightScheme.onSurface);
468+
expect(typography.black.bodyMedium!.decorationColor, lightScheme.onSurface);
469+
expect(typography.white.bodyMedium!.fontFamily, 'Custom');
470+
expect(typography.white.bodyMedium!.color, lightScheme.surface);
471+
expect(typography.white.bodyMedium!.decorationColor, lightScheme.surface);
472+
});
473+
474+
test('copyWith can be used to customize colors instead', () {
475+
final base = Typography.material2021(colorScheme: lightScheme);
476+
final Typography typography = base.copyWith(
477+
black: base.black.merge(custom),
478+
white: base.white.merge(custom),
479+
);
480+
expect(typography.black.bodyMedium!.fontFamily, 'Custom');
481+
expect(typography.black.bodyMedium!.color, const Color(0xFFFF0000));
482+
expect(typography.white.bodyMedium!.color, const Color(0xFFFF0000));
483+
// Styles that are not overridden keep the generated values.
484+
expect(typography.black.bodyLarge!.fontFamily, 'Roboto');
485+
expect(typography.black.bodyLarge!.color, lightScheme.onSurface);
486+
});
487+
});
447488
}

0 commit comments

Comments
 (0)