DynamicCachedFonts class

Allows dynamically loading fonts from the given url.

Fetching fonts from Firebase Storage is also supported.

For ready to use, simple interface, initialize DynamicCachedFonts and call the load method either in you initState() or just before runApp() itself.

class _SomeStateFulWidgetState extends State<_SomeStateFulWidget> {
  @override
  void initState() {
    const DynamicCachedFonts dynamicCachedFont = DynamicCachedFonts(
      fontFamily: 'font',
      url: // Add url to a font
    );
    dynamicCachedFont.load();

    super.initState();
  }
void main() async {
  final DynamicCachedFont dynamicCachedFont = DynamicCachedFont(
    fontFamily: 'font',
    url: // Add url to a font
  );
  dynamicCachedFont.load();

  runApp(
    YourAppHere(
      ...
      TextStyle(
        fontFamily: 'font'
      ),
      ...
    ),
  );
}

For greater customization, use static methods.

Constructors

DynamicCachedFonts({required String url, required String fontFamily, int maxCacheObjects = kDefaultMaxCacheObjects, Duration cacheStalePeriod = kDefaultCacheStalePeriod})
Allows dynamically loading fonts from the given url and caching them.
DynamicCachedFonts.family({required List<String> urls, required String fontFamily, int maxCacheObjects = kDefaultMaxCacheObjects, Duration cacheStalePeriod = kDefaultCacheStalePeriod})
Allows dynamically loading fonts from the given list of url and caching them. The fontFamily groups a series of related font assets, each of which defines how to render a specific FontWeight and FontStyle within the family.
DynamicCachedFonts.fromFirebase({required String bucketUrl, required String fontFamily, int maxCacheObjects = kDefaultMaxCacheObjects, Duration cacheStalePeriod = kDefaultCacheStalePeriod})
Allows dynamically loading fonts from firebase storage with the given firebase storage url, and caching them.

Properties

cacheStalePeriod Duration
The time duration in which a cache object is considered 'stale'. When a file is cached but not being used for a certain time the file will be deleted
final
fontFamily String
Used to specify the name of the font family which is to be used as TextStyle.fontFamily.
final
hashCode int
The hash code for this object.
no setterinherited
maxCacheObjects int
Defines how large the cache is allowed to be.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
urls List<String>
Used to specify the download url(s) for the required font(s).
final

Methods

load() Future<Iterable<FileInfo>>
Used to download and load a font into the app with the given urls and cache configuration.
loadStream({ItemCountProgressListener? itemCountProgressListener, DownloadProgressListener? downloadProgressListener}) Stream<FileInfo>
Used to download and load a font into the app with the given urls and cache configuration. A stream of FileInfo object(s) is returned which emits the font files as they are loaded. The total number of files returned corresponds to the number of urls provided.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

cacheFont(String url, {Duration cacheStalePeriod = kDefaultCacheStalePeriod, int maxCacheObjects = kDefaultMaxCacheObjects}) Future<FileInfo>
Downloads and caches font from the url with the given configuration.
cacheFontStream(String url, {Duration cacheStalePeriod = kDefaultCacheStalePeriod, int maxCacheObjects = kDefaultMaxCacheObjects, DownloadProgressListener? progressListener}) Stream<FileInfo>
Downloads and caches font from the url with the given configuration. A single FileInfo object is returned as a stream and the download progress is can be listened to using the progressListener callback.
canLoadFont(String url) Future<bool>
Checks whether the given url can be loaded directly from cache.
custom({required CacheManager cacheManager, bool force = false}) → void
Accepts cacheManager and force to provide a custom CacheManager for testing.
loadCachedFamily(List<String> urls, {required String fontFamily, FontLoader? fontLoader}) Future<Iterable<FileInfo>>
Fetches the given urls from cache and loads them into the engine to be used.
loadCachedFamilyStream(List<String> urls, {required String fontFamily, ItemCountProgressListener? progressListener, FontLoader? fontLoader}) Stream<FileInfo>
Fetches the given urls from cache and loads them into the engine to be used. A stream of FileInfo objects is returned, which emits the font files as they are loaded. The total number of files returned corresponds to the number of urls provided. The download progress can be listened to using progressListener.
loadCachedFont(String url, {required String fontFamily, FontLoader? fontLoader}) Future<FileInfo>
Fetches the given url from cache and loads it as an asset.
removeCachedFont(String url) Future<void>
Removes the given url can be loaded directly from cache.
toggleVerboseLogging(bool shouldVerboseLog) → void
Used to specify whether detailed logs should be printed for debugging.