Skip to content

Commit

Permalink
fix: preserve font fallbacks if they exist
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Aug 23, 2024
1 parent d679691 commit 76f97f4
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions lib/components/theming/font_fallbacks.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import 'package:flutter/material.dart';

extension FallbackTextTheme on TextTheme {
TextTheme withFallbacks() => copyWith(
displayLarge: displayLarge?.withFallbacks(),
displayMedium: displayMedium?.withFallbacks(),
displaySmall: displaySmall?.withFallbacks(),
headlineLarge: headlineLarge?.withFallbacks(),
headlineMedium: headlineMedium?.withFallbacks(),
headlineSmall: headlineSmall?.withFallbacks(),
titleLarge: titleLarge?.withFallbacks(),
titleMedium: titleMedium?.withFallbacks(),
titleSmall: titleSmall?.withFallbacks(),
bodyLarge: bodyLarge?.withFallbacks(),
bodyMedium: bodyMedium?.withFallbacks(),
bodySmall: bodySmall?.withFallbacks(),
labelLarge: labelLarge?.withFallbacks(),
labelMedium: labelMedium?.withFallbacks(),
labelSmall: labelSmall?.withFallbacks(),
);
}

extension FallbackTextStyle on TextStyle {
static const sansSerifFallbacks = [
'Inter',
'Noto Sans',
'Roboto',
'Droid Sans',
Expand All @@ -15,31 +36,9 @@ extension FallbackTextTheme on TextTheme {
'Dekko',
];

TextTheme withFallbacks() {
return copyWith(
displayLarge:
displayLarge?.copyWith(fontFamilyFallback: sansSerifFallbacks),
displayMedium:
displayMedium?.copyWith(fontFamilyFallback: sansSerifFallbacks),
displaySmall:
displaySmall?.copyWith(fontFamilyFallback: sansSerifFallbacks),
headlineLarge:
headlineLarge?.copyWith(fontFamilyFallback: sansSerifFallbacks),
headlineMedium:
headlineMedium?.copyWith(fontFamilyFallback: sansSerifFallbacks),
headlineSmall:
headlineSmall?.copyWith(fontFamilyFallback: sansSerifFallbacks),
titleLarge: titleLarge?.copyWith(fontFamilyFallback: sansSerifFallbacks),
titleMedium:
titleMedium?.copyWith(fontFamilyFallback: sansSerifFallbacks),
titleSmall: titleSmall?.copyWith(fontFamilyFallback: sansSerifFallbacks),
bodyLarge: bodyLarge?.copyWith(fontFamilyFallback: sansSerifFallbacks),
bodyMedium: bodyMedium?.copyWith(fontFamilyFallback: sansSerifFallbacks),
bodySmall: bodySmall?.copyWith(fontFamilyFallback: sansSerifFallbacks),
labelLarge: labelLarge?.copyWith(fontFamilyFallback: sansSerifFallbacks),
labelMedium:
labelMedium?.copyWith(fontFamilyFallback: sansSerifFallbacks),
labelSmall: labelSmall?.copyWith(fontFamilyFallback: sansSerifFallbacks),
);
}
TextStyle withFallbacks() => copyWith(
fontFamilyFallback: fontFamilyFallback == null
? sansSerifFallbacks
: [...fontFamilyFallback!, ...sansSerifFallbacks],
);
}

0 comments on commit 76f97f4

Please sign in to comment.