Skip to content

Commit 3ada763

Browse files
authored
[material_ui] Fix TextTheme and Typography debugFillProperties defaults (flutter#13374)
`TextTheme.debugFillProperties` and `Typography.debugFillProperties` decide which values to report by comparing them against a default. Both defaults were out of date: - `TextTheme` compared against `Typography.material2018(...).black` - `Typography` compared against `Typography.material2014()` This PR: - compares `TextTheme` styles against `ThemeData.fallback().textTheme`. - compares `Typography` text themes against `ThemeData.fallback().typography`. This matches how `ThemeData.debugFillProperties` already reports typography. - Treats null `TextTheme` styles as default. When a `TextTheme` is merged into a `ThemeData`, a null style falls back to the default style, so it shouldn't be reported as a difference. Fixes flutter/flutter#194981 ## 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 d96c511 commit 3ada763

5 files changed

Lines changed: 90 additions & 101 deletions

File tree

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

Lines changed: 29 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
/// @docImport 'text_button.dart';
99
/// @docImport 'theme.dart';
1010
/// @docImport 'theme_data.dart';
11+
/// @docImport 'typography.dart';
1112
library;
1213

1314
import 'package:flutter/foundation.dart';
1415
import 'package:flutter/widgets.dart';
1516

1617
import 'theme.dart';
17-
import 'typography.dart';
1818

1919
/// Material design text theme.
2020
///
@@ -800,103 +800,33 @@ class TextTheme with Diagnosticable {
800800
@override
801801
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
802802
super.debugFillProperties(properties);
803-
final TextTheme defaultTheme = Typography.material2018(platform: defaultTargetPlatform).black;
804-
properties.add(
805-
DiagnosticsProperty<TextStyle>(
806-
'displayLarge',
807-
displayLarge,
808-
defaultValue: defaultTheme.displayLarge,
809-
),
810-
);
811-
properties.add(
812-
DiagnosticsProperty<TextStyle>(
813-
'displayMedium',
814-
displayMedium,
815-
defaultValue: defaultTheme.displayMedium,
816-
),
817-
);
818-
properties.add(
819-
DiagnosticsProperty<TextStyle>(
820-
'displaySmall',
821-
displaySmall,
822-
defaultValue: defaultTheme.displaySmall,
823-
),
824-
);
825-
properties.add(
826-
DiagnosticsProperty<TextStyle>(
827-
'headlineLarge',
828-
headlineLarge,
829-
defaultValue: defaultTheme.headlineLarge,
830-
),
831-
);
832-
properties.add(
833-
DiagnosticsProperty<TextStyle>(
834-
'headlineMedium',
835-
headlineMedium,
836-
defaultValue: defaultTheme.headlineMedium,
837-
),
838-
);
839-
properties.add(
840-
DiagnosticsProperty<TextStyle>(
841-
'headlineSmall',
842-
headlineSmall,
843-
defaultValue: defaultTheme.headlineSmall,
844-
),
845-
);
846-
properties.add(
847-
DiagnosticsProperty<TextStyle>(
848-
'titleLarge',
849-
titleLarge,
850-
defaultValue: defaultTheme.titleLarge,
851-
),
852-
);
853-
properties.add(
854-
DiagnosticsProperty<TextStyle>(
855-
'titleMedium',
856-
titleMedium,
857-
defaultValue: defaultTheme.titleMedium,
858-
),
859-
);
860-
properties.add(
861-
DiagnosticsProperty<TextStyle>(
862-
'titleSmall',
863-
titleSmall,
864-
defaultValue: defaultTheme.titleSmall,
865-
),
866-
);
867-
properties.add(
868-
DiagnosticsProperty<TextStyle>('bodyLarge', bodyLarge, defaultValue: defaultTheme.bodyLarge),
869-
);
870-
properties.add(
871-
DiagnosticsProperty<TextStyle>(
872-
'bodyMedium',
873-
bodyMedium,
874-
defaultValue: defaultTheme.bodyMedium,
875-
),
876-
);
877-
properties.add(
878-
DiagnosticsProperty<TextStyle>('bodySmall', bodySmall, defaultValue: defaultTheme.bodySmall),
879-
);
880-
properties.add(
881-
DiagnosticsProperty<TextStyle>(
882-
'labelLarge',
883-
labelLarge,
884-
defaultValue: defaultTheme.labelLarge,
885-
),
886-
);
887-
properties.add(
888-
DiagnosticsProperty<TextStyle>(
889-
'labelMedium',
890-
labelMedium,
891-
defaultValue: defaultTheme.labelMedium,
892-
),
893-
);
894-
properties.add(
895-
DiagnosticsProperty<TextStyle>(
896-
'labelSmall',
897-
labelSmall,
898-
defaultValue: defaultTheme.labelSmall,
899-
),
900-
);
803+
final TextTheme defaultTheme = ThemeData.fallback().textTheme;
804+
// A null style falls back to the default style when merged into a
805+
// ThemeData, so it is not reported as differing from the default.
806+
void add(String name, TextStyle? style, TextStyle? defaultStyle) {
807+
properties.add(
808+
DiagnosticsProperty<TextStyle>(
809+
name,
810+
style,
811+
defaultValue: style == null ? null : defaultStyle,
812+
),
813+
);
814+
}
815+
816+
add('displayLarge', displayLarge, defaultTheme.displayLarge);
817+
add('displayMedium', displayMedium, defaultTheme.displayMedium);
818+
add('displaySmall', displaySmall, defaultTheme.displaySmall);
819+
add('headlineLarge', headlineLarge, defaultTheme.headlineLarge);
820+
add('headlineMedium', headlineMedium, defaultTheme.headlineMedium);
821+
add('headlineSmall', headlineSmall, defaultTheme.headlineSmall);
822+
add('titleLarge', titleLarge, defaultTheme.titleLarge);
823+
add('titleMedium', titleMedium, defaultTheme.titleMedium);
824+
add('titleSmall', titleSmall, defaultTheme.titleSmall);
825+
add('bodyLarge', bodyLarge, defaultTheme.bodyLarge);
826+
add('bodyMedium', bodyMedium, defaultTheme.bodyMedium);
827+
add('bodySmall', bodySmall, defaultTheme.bodySmall);
828+
add('labelLarge', labelLarge, defaultTheme.labelLarge);
829+
add('labelMedium', labelMedium, defaultTheme.labelMedium);
830+
add('labelSmall', labelSmall, defaultTheme.labelSmall);
901831
}
902832
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
/// @docImport 'material_localizations.dart';
66
/// @docImport 'theme.dart';
7-
/// @docImport 'theme_data.dart';
87
library;
98

109
import 'package:flutter/foundation.dart';
@@ -13,6 +12,7 @@ import 'package:flutter/painting.dart';
1312
import 'color_scheme.dart';
1413
import 'colors.dart';
1514
import 'text_theme.dart';
15+
import 'theme_data.dart';
1616

1717
part 'generated/typography_defaults_m3.g.dart';
1818

@@ -351,7 +351,7 @@ class Typography with Diagnosticable {
351351
@override
352352
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
353353
super.debugFillProperties(properties);
354-
final defaultTypography = Typography.material2014();
354+
final Typography defaultTypography = ThemeData.fallback().typography;
355355
properties.add(
356356
DiagnosticsProperty<TextTheme>('black', black, defaultValue: defaultTypography.black),
357357
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changelog: |
2+
- Fixes `TextTheme.debugFillProperties` to use the default `ThemeData` text theme, instead of `Typography.material2018(...).black`.
3+
- Fixes `Typography.debugFillProperties` to use the default `ThemeData` typography, instead of `Typography.material2014`.
4+
version: patch

‎packages/material_ui/test/text_theme_test.dart‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter_test/flutter_test.dart';
67
import 'package:material_ui/material_ui.dart';
78

@@ -493,4 +494,46 @@ void main() {
493494

494495
expect(actualTextTheme, equals(expectedTextTheme));
495496
});
497+
498+
test('Default TextTheme debugFillProperties', () {
499+
final builder = DiagnosticPropertiesBuilder();
500+
ThemeData.fallback().textTheme.debugFillProperties(builder);
501+
502+
final List<String> nonDefaultPropertyNames = builder.properties
503+
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
504+
.map((DiagnosticsNode node) => node.name!)
505+
.toList();
506+
507+
expect(nonDefaultPropertyNames, isEmpty);
508+
});
509+
510+
test('Custom TextTheme debugFillProperties', () {
511+
final builder = DiagnosticPropertiesBuilder();
512+
const TextTheme(
513+
displayLarge: TextStyle(fontSize: 40.0),
514+
bodyMedium: TextStyle(color: Color(0xFF123456)),
515+
).debugFillProperties(builder);
516+
517+
final List<String> nonDefaultPropertyNames = builder.properties
518+
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
519+
.map((DiagnosticsNode node) => node.name!)
520+
.toList();
521+
522+
expect(nonDefaultPropertyNames, <String>['displayLarge', 'bodyMedium']);
523+
});
524+
525+
test('TextTheme debugFillProperties does not report styles that match the default', () {
526+
final builder = DiagnosticPropertiesBuilder();
527+
TextTheme(
528+
displayLarge: ThemeData.fallback().textTheme.displayLarge,
529+
bodyMedium: const TextStyle(color: Color(0xFF123456)),
530+
).debugFillProperties(builder);
531+
532+
final List<String> nonDefaultPropertyNames = builder.properties
533+
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
534+
.map((DiagnosticsNode node) => node.name!)
535+
.toList();
536+
537+
expect(nonDefaultPropertyNames, <String>['bodyMedium']);
538+
});
496539
}

‎packages/material_ui/test/typography_test.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ void main() {
131131
expect(nonDefaultPropertyNames, <String>['black', 'white', 'englishLike', 'dense', 'tall']);
132132
});
133133

134+
test('Default Typography debugFillProperties', () {
135+
final builder = DiagnosticPropertiesBuilder();
136+
ThemeData.fallback().typography.debugFillProperties(builder);
137+
138+
final List<String> nonDefaultPropertyNames = builder.properties
139+
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
140+
.map((DiagnosticsNode node) => node.name!)
141+
.toList();
142+
143+
expect(nonDefaultPropertyNames, isEmpty);
144+
});
145+
134146
test('Can lerp between different typographies', () {
135147
final all = <Typography>[
136148
for (final TargetPlatform platform in TargetPlatform.values)

0 commit comments

Comments
 (0)