loadAppFonts function

Future<void> loadAppFonts()

By default, flutter test only uses a single "test" font called Ahem.

This font is designed to show black spaces for every character and icon. This obviously makes goldens much less valuable.

To make the goldens more useful, we will automatically load any fonts included in your pubspec.yaml as well as from packages you depend on.

Implementation

Future<void> loadAppFonts() async {
  TestWidgetsFlutterBinding.ensureInitialized();
  final fontManifest = await rootBundle.loadStructuredData<Iterable<dynamic>>(
    'FontManifest.json',
    (string) async => json.decode(string),
  );

  final loaders = <FontLoader>[];
  for (final Map<String, dynamic> font in fontManifest) {
    final fontLoader = FontLoader(derivedFontFamily(font));
    for (final Map<String, dynamic> fontType in font['fonts']) {
      fontLoader.addFont(rootBundle.load(fontType['asset']));
    }
    loaders.add(fontLoader);
  }

  await Future.wait(loaders.map((loader) => loader.load()));
}