Skip to content

Commit d01e5bc

Browse files
authored
[material_ui] Add fontFeatures and fontVariations to TextTheme.apply() (flutter#12879)
## Description This PR ports flutter#192730 to flutter/packages. Adds `List<FontFeature>?` and `List<FontVariation>?` parameters to `TextTheme.apply()`, passing them through to each `TextStyle.apply()` call. This allows developers to apply font features and variations consistently across an entire text theme without modifying each `TextStyle` individually. ## Related Issue Fixes flutter#192692 Ports flutter#192730 ## Proposed Change - Added `List<FontFeature>?` and `List<FontVariation>?` parameters to `TextTheme.apply()` - Both default to `null`, preserving backward compatibility - Each of the 15 `TextStyle.apply()` calls within `TextTheme.apply()` now passes these parameters through ## Technical Details The `TextStyle` class already supports `fontFeatures` and `fontVariations` in its own `apply()` method. The `TextTheme.apply()` method was missing the ability to pass these through. This is a purely additive change - no existing behavior is modified. ### Use Case: Persian Digits ```dart final theme = Theme.of(context).textTheme.apply( fontFeatures: const [FontFeature("ss01")], ); ``` ### Use Case: Variable Font Grade ```dart final theme = Theme.of(context).textTheme.apply( fontVariations: const [FontVariation('GRAD', 40.0)], ); ``` ## Testing Added 2 new tests to text_theme_test.dart: 1. `TextTheme apply fontFeatures fontVariations` - verifies all 15 styles receive the parameters 2. `TextTheme apply null fontFeatures fontVariations preserves original` - verifies null defaults don't overwrite existing values (fixes incomplete test from original PR) All 18 tests pass (including existing tests). ## Pre-Review Checklist [^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. ## Note This PR ports the original flutter/flutter PR flutter#192730. The original PR should be closed with a reference to this new PR. The original PR had a test that claimed to verify both `fontFeatures` and `fontVariations` preservation, but only checked `fontFeatures`. This has been fixed in this ported version to properly verify both properties.
1 parent 04c946c commit d01e5bc

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ class TextTheme with Diagnosticable {
415415
TextDecoration? decoration,
416416
Color? decorationColor,
417417
TextDecorationStyle? decorationStyle,
418+
List<FontFeature>? fontFeatures,
419+
List<FontVariation>? fontVariations,
418420
}) {
419421
return TextTheme(
420422
displayLarge: displayLarge?.apply(
@@ -433,6 +435,8 @@ class TextTheme with Diagnosticable {
433435
heightFactor: heightFactor,
434436
heightDelta: heightDelta,
435437
package: package,
438+
fontFeatures: fontFeatures,
439+
fontVariations: fontVariations,
436440
),
437441
displayMedium: displayMedium?.apply(
438442
color: displayColor,
@@ -450,6 +454,8 @@ class TextTheme with Diagnosticable {
450454
heightFactor: heightFactor,
451455
heightDelta: heightDelta,
452456
package: package,
457+
fontFeatures: fontFeatures,
458+
fontVariations: fontVariations,
453459
),
454460
displaySmall: displaySmall?.apply(
455461
color: displayColor,
@@ -467,6 +473,8 @@ class TextTheme with Diagnosticable {
467473
heightFactor: heightFactor,
468474
heightDelta: heightDelta,
469475
package: package,
476+
fontFeatures: fontFeatures,
477+
fontVariations: fontVariations,
470478
),
471479
headlineLarge: headlineLarge?.apply(
472480
color: displayColor,
@@ -484,6 +492,8 @@ class TextTheme with Diagnosticable {
484492
heightFactor: heightFactor,
485493
heightDelta: heightDelta,
486494
package: package,
495+
fontFeatures: fontFeatures,
496+
fontVariations: fontVariations,
487497
),
488498
headlineMedium: headlineMedium?.apply(
489499
color: displayColor,
@@ -501,6 +511,8 @@ class TextTheme with Diagnosticable {
501511
heightFactor: heightFactor,
502512
heightDelta: heightDelta,
503513
package: package,
514+
fontFeatures: fontFeatures,
515+
fontVariations: fontVariations,
504516
),
505517
headlineSmall: headlineSmall?.apply(
506518
color: bodyColor,
@@ -518,6 +530,8 @@ class TextTheme with Diagnosticable {
518530
heightFactor: heightFactor,
519531
heightDelta: heightDelta,
520532
package: package,
533+
fontFeatures: fontFeatures,
534+
fontVariations: fontVariations,
521535
),
522536
titleLarge: titleLarge?.apply(
523537
color: bodyColor,
@@ -535,6 +549,8 @@ class TextTheme with Diagnosticable {
535549
heightFactor: heightFactor,
536550
heightDelta: heightDelta,
537551
package: package,
552+
fontFeatures: fontFeatures,
553+
fontVariations: fontVariations,
538554
),
539555
titleMedium: titleMedium?.apply(
540556
color: bodyColor,
@@ -552,6 +568,8 @@ class TextTheme with Diagnosticable {
552568
heightFactor: heightFactor,
553569
heightDelta: heightDelta,
554570
package: package,
571+
fontFeatures: fontFeatures,
572+
fontVariations: fontVariations,
555573
),
556574
titleSmall: titleSmall?.apply(
557575
color: bodyColor,
@@ -569,6 +587,8 @@ class TextTheme with Diagnosticable {
569587
heightFactor: heightFactor,
570588
heightDelta: heightDelta,
571589
package: package,
590+
fontFeatures: fontFeatures,
591+
fontVariations: fontVariations,
572592
),
573593
bodyLarge: bodyLarge?.apply(
574594
color: bodyColor,
@@ -586,6 +606,8 @@ class TextTheme with Diagnosticable {
586606
heightFactor: heightFactor,
587607
heightDelta: heightDelta,
588608
package: package,
609+
fontFeatures: fontFeatures,
610+
fontVariations: fontVariations,
589611
),
590612
bodyMedium: bodyMedium?.apply(
591613
color: bodyColor,
@@ -603,6 +625,8 @@ class TextTheme with Diagnosticable {
603625
heightFactor: heightFactor,
604626
heightDelta: heightDelta,
605627
package: package,
628+
fontFeatures: fontFeatures,
629+
fontVariations: fontVariations,
606630
),
607631
bodySmall: bodySmall?.apply(
608632
color: displayColor,
@@ -620,6 +644,8 @@ class TextTheme with Diagnosticable {
620644
heightFactor: heightFactor,
621645
heightDelta: heightDelta,
622646
package: package,
647+
fontFeatures: fontFeatures,
648+
fontVariations: fontVariations,
623649
),
624650
labelLarge: labelLarge?.apply(
625651
color: bodyColor,
@@ -637,6 +663,8 @@ class TextTheme with Diagnosticable {
637663
heightFactor: heightFactor,
638664
heightDelta: heightDelta,
639665
package: package,
666+
fontFeatures: fontFeatures,
667+
fontVariations: fontVariations,
640668
),
641669
labelMedium: labelMedium?.apply(
642670
color: bodyColor,
@@ -654,6 +682,8 @@ class TextTheme with Diagnosticable {
654682
heightFactor: heightFactor,
655683
heightDelta: heightDelta,
656684
package: package,
685+
fontFeatures: fontFeatures,
686+
fontVariations: fontVariations,
657687
),
658688
labelSmall: labelSmall?.apply(
659689
color: bodyColor,
@@ -671,6 +701,8 @@ class TextTheme with Diagnosticable {
671701
heightFactor: heightFactor,
672702
heightDelta: heightDelta,
673703
package: package,
704+
fontFeatures: fontFeatures,
705+
fontVariations: fontVariations,
674706
),
675707
);
676708
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Added `fontFeatures` and `fontVariations` parameters to `TextTheme.apply()`.
3+
version: minor

‎packages/material_ui/test/text_theme_test.dart‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,56 @@ void main() {
149149
expect(themeStyles.every((TextStyle style) => style.decoration == decoration), true);
150150
});
151151

152+
test('TextTheme apply fontFeatures fontVariations', () {
153+
const fontFeatures = <FontFeature>[FontFeature.enable('smcp')];
154+
const fontVariations = <FontVariation>[FontVariation('wght', 400.0)];
155+
final typography = Typography.material2018();
156+
final TextTheme theme = typography.black.apply(
157+
fontFeatures: fontFeatures,
158+
fontVariations: fontVariations,
159+
);
160+
161+
final themeStyles = <TextStyle>[
162+
theme.displayLarge!,
163+
theme.displayMedium!,
164+
theme.displaySmall!,
165+
theme.headlineLarge!,
166+
theme.headlineMedium!,
167+
theme.headlineSmall!,
168+
theme.titleLarge!,
169+
theme.titleMedium!,
170+
theme.titleSmall!,
171+
theme.bodyLarge!,
172+
theme.bodyMedium!,
173+
theme.bodySmall!,
174+
theme.labelLarge!,
175+
theme.labelMedium!,
176+
theme.labelSmall!,
177+
];
178+
for (final style in themeStyles) {
179+
expect(style.fontFeatures, fontFeatures);
180+
expect(style.fontVariations, fontVariations);
181+
}
182+
});
183+
184+
test('TextTheme apply null fontFeatures fontVariations preserves original', () {
185+
const fontFeatures = <FontFeature>[FontFeature.enable('smcp')];
186+
const fontVariations = <FontVariation>[FontVariation('wght', 400.0)];
187+
final typography = Typography.material2018();
188+
final TextTheme baseTheme = typography.black.copyWith(
189+
displayLarge: typography.black.displayLarge!.copyWith(
190+
fontFeatures: fontFeatures,
191+
fontVariations: fontVariations,
192+
),
193+
);
194+
final TextTheme theme = baseTheme.apply();
195+
196+
expect(theme.displayLarge!.fontFeatures, fontFeatures);
197+
expect(theme.displayLarge!.fontVariations, fontVariations);
198+
expect(theme.displayMedium!.fontFeatures, isNull);
199+
expect(theme.displayMedium!.fontVariations, isNull);
200+
});
201+
152202
test('TextTheme apply fontSizeFactor fontSizeDelta', () {
153203
final typography = Typography.material2018();
154204
final TextTheme baseTheme = Typography.englishLike2018.merge(typography.black);

0 commit comments

Comments
 (0)