Flutter Pro Design

Small details that build taste in Flutter.

curated by Kamran BekirovKamran Bekirov

Fix the GoogleFonts glitch

The google_fonts package downloads fonts when first used. That means on a fresh install, users see your text in the default font for a split second before it swaps to your chosen one.

Here's how it looks like:

Not a good first impression.

The fix is built into the package: GoogleFonts.pendingFonts lets you preload fonts before you use them. Await the fonts during app's splash, when finished show with the right typography from frame one:

Future<void> preloadFonts() async {
  await GoogleFonts.pendingFonts([
    GoogleFonts.inter(fontWeight: FontWeight.w400),
    GoogleFonts.inter(fontWeight: FontWeight.w700),
  ]);
  
  // Preload other weights in the background
  _preloadOtherFonts();
}
  
Future<void> _preloadOtherFonts() async {
  await GoogleFonts.pendingFonts([
    GoogleFonts.inter(fontWeight: FontWeight.w600),
  ]);
}

One thing: GoogleFonts.inter() doesn't load the whole font. It only loads weight 400, normal style. So when preloading, specify the exact weights and styles you've used in your app. If you used w700 on a heading and w400 italic on a caption, preload both. The key here is: preload fonts you need immediately, then load the rest in the background.