diff --git a/packages/google_fonts/CHANGELOG.md b/packages/google_fonts/CHANGELOG.md index cfa79220..2cb48839 100644 --- a/packages/google_fonts/CHANGELOG.md +++ b/packages/google_fonts/CHANGELOG.md @@ -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 diff --git a/packages/google_fonts/lib/src/google_fonts_base.dart b/packages/google_fonts/lib/src/google_fonts_base.dart index 00d352e5..015d3399 100755 --- a/packages/google_fonts/lib/src/google_fonts_base.dart +++ b/packages/google_fonts/lib/src/google_fonts_base.dart @@ -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, + ], ); } diff --git a/packages/google_fonts/test/generated_font_methods_test.dart b/packages/google_fonts/test/generated_font_methods_test.dart index db70cb1e..9a571264 100644 --- a/packages/google_fonts/test/generated_font_methods_test.dart +++ b/packages/google_fonts/test/generated_font_methods_test.dart @@ -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 // ///////////////////////////