From b1177ad3ea4f67ff8bba2a6e59c779524a6cf994 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 14 May 2024 13:44:51 +0800 Subject: [PATCH] fix: font family warning (#5329) --- .../lib/shared/google_fonts_extension.dart | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/frontend/appflowy_flutter/lib/shared/google_fonts_extension.dart b/frontend/appflowy_flutter/lib/shared/google_fonts_extension.dart index bfd67afc8c..287bfcbcd9 100644 --- a/frontend/appflowy_flutter/lib/shared/google_fonts_extension.dart +++ b/frontend/appflowy_flutter/lib/shared/google_fonts_extension.dart @@ -1,7 +1,14 @@ +import 'package:appflowy/workspace/application/settings/appearance/base_appearance.dart'; import 'package:appflowy_backend/log.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; +const _defaultFontFamilies = [ + defaultFontFamily, + builtInCodeFontFamily, + fallbackFontFamily, +]; + // if the font family is not available, google fonts packages will throw an exception // this method will return the system font family if the font family is not available TextStyle getGoogleFontSafely( @@ -12,19 +19,31 @@ TextStyle getGoogleFontSafely( double? letterSpacing, double? lineHeight, }) { - try { - return GoogleFonts.getFont( - fontFamily, + // if the font family is the built-in font family, we can use it directly + if (_defaultFontFamilies.contains(fontFamily)) { + return TextStyle( + fontFamily: fontFamily.isEmpty ? null : fontFamily, fontWeight: fontWeight, fontSize: fontSize, color: fontColor, letterSpacing: letterSpacing, height: lineHeight, ); - } catch (e) { - Log.error( - 'Font family $fontFamily is not available, using default font family instead', - ); + } else { + try { + return GoogleFonts.getFont( + fontFamily, + fontWeight: fontWeight, + fontSize: fontSize, + color: fontColor, + letterSpacing: letterSpacing, + height: lineHeight, + ); + } catch (e) { + Log.error( + 'Font family $fontFamily is not available, using default font family instead', + ); + } } return TextStyle(