Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[google_fonts] Enhance font fallback #505

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/google_fonts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.2.0 - 2023-11-17
### Added
- Add font fallback mechanism. This update allows for more robust handling of unavailable fonts, ensuring graceful degradation to alternative fonts.

## 6.1.0 - 2023-09-20
### Added
- Add an example where one can select any available font
Expand Down
8 changes: 7 additions & 1 deletion packages/google_fonts/lib/src/google_fonts_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,15 @@ TextStyle googleFontsTextStyle({
pendingFontFutures.add(loadingFuture);
loadingFuture.then((_) => pendingFontFutures.remove(loadingFuture));

final currentFontFamily = textStyle.fontFamily;

return textStyle.copyWith(
fontFamily: familyWithVariant.toString(),
fontFamilyFallback: [fontFamily],
fontFamilyFallback: [
fontFamily,
if (currentFontFamily != null) currentFontFamily,
...?textStyle.fontFamilyFallback,
Comment on lines +124 to +126
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the need for 124 and 125, can you please clarify?

],
);
}

Expand Down
16 changes: 16 additions & 0 deletions packages/google_fonts/test/generated_font_methods_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ void main() {
expect(outputTextStyle.fontFamily, equals('Lato_regular'));
});

testWidgets('Combining text styles maintains fontFamily and fallback order',
(tester) async {
const textStyle = TextStyle();
final outputTextStyle = GoogleFonts.lato(
textStyle: GoogleFonts.notoColorEmoji(
textStyle: textStyle,
),
);

expect(outputTextStyle.fontFamily, 'Lato_regular');
expect(
outputTextStyle.fontFamilyFallback,
['Lato', 'NotoColorEmoji_regular', 'NotoColorEmoji'],
);
});

///////////////////////////
// TextStyle param tests //
///////////////////////////
Expand Down