import 'package:flutter/services.dart'; import 'font_choice.dart'; /// Registers [sources] under [family]. Production reads them from the bundle /// or the custom font store; tests substitute a recorder. typedef AppFontAssetLoader = Future Function(String family, List sources); /// Registers font files already in memory under [family]; used to check an /// added font before it is stored. typedef AppFontBytesLoader = Future Function(String family, List faces); /// Reads the bytes of an added face by its store key. typedef CustomFontReader = Future Function(String key); /// Loads font files on demand with a [FontLoader]: bundle assets (on the web /// each is fetched only now, when its family is first previewed or chosen) /// and [customFontSource] keys, read through [readCustom]. AppFontAssetLoader appFontSourceLoader(CustomFontReader readCustom) => (family, sources) async { final loader = FontLoader(family); for (final source in sources) { loader.addFont( source.startsWith(customFontSourcePrefix) ? readCustom( source.substring(customFontSourcePrefix.length), ).then(ByteData.sublistView) : rootBundle.load(source), ); } await loader.load(); }; /// Registers in-memory font files with a [FontLoader]. Future loadFontBytes(String family, List faces) async { final loader = FontLoader(family); for (final face in faces) { loader.addFont(Future.value(ByteData.sublistView(face))); } await loader.load(); }