A font loader to download, cache and load web fonts in flutter with support for Firebase Cloud Storage.

Overview

Dynamic Cached Fonts

Pub Version Supported Platforms

Unit Tests Integration Tests

A simple, easy to use yet customizable font loader to use web fonts.

Demo: https://sidrao2006.github.io/dynamic_cached_fonts

Demo 3

👋 Introduction

Dynamic Cached Fonts allows you to dynamically load a font from any url and cache it. This way, you can reduce your bundle size and load the font if and when it's required.

Another advantage of dynamically loading fonts is that you can now easily provide an option to your users to pick an app font. This allows for a greater level of customization.

Caching is an added performance upgrade as the font will be downloaded only once and used multiple times, reducing network and battery usage.

🏃 Get Started

To use the package, add dynamic_cached_fonts as a dependency.

⚒️ Usage

Loading fonts on demand

You can load font on demand, for example - when a page loads

@override
void initState() {
  final DynamicCachedFonts dynamicCachedFont = DynamicCachedFonts(
    fontFamily: fontFamilyName, // The font family name to be passed to TextStyle.fontFamily
    url: fontUrl, // A valid url pointing to a font file (.ttf or .otf files only) 
  );
  dynamicCachedFont.load(); // Downloads the font, caches and loads it.

  super.initState();
}
...
Text(
  'Some Text',
  style: TextStyle(fontFamily: fontFamilyName),
)

Demo 1

Or when a button is clicked

ElevatedButton(
  onPressed: () {
    final DynamicCachedFonts dynamicCachedFont = DynamicCachedFonts(
      fontFamily: fontFamilyName,
      url: fontUrl,
    );

    dynamicCachedFont.load();
  },
  child: const Text('Load Font'),
),

Demo 2

If you want to change how large the cache can be or maybe how long the font stays in cache, pass in maxCacheObjects and cacheStalePeriod.

DynamicCachedFonts(
  fontFamily: fontFamilyName,
  url: fontUrl,
  maxCacheObjects: 150,
  cacheStalePeriod: const Duration(days: 100),
);

TextStyle.fontFamilys are applied only after load() is called.

Calling load() more than once throws a StateError

What if you need to load multiple fonts, of varying weights and styles, as a single family...For that, you can use the DynamicCachedFonts.family constructor.

It accepts a list of urls, pointing to different fonts in the same family, as urls.

DynamicCachedFonts.family(
  urls: <String>[
    fontFamilyNameBoldUrl,
    fontFamilyNameItalicUrl,
    fontFamilyNameRegularUrl,
    fontFamilyNameThinUrl,
  ],
  fontFamily: fontFamilyName,
);

Demo 5

If you need more control, use the static methods!

cacheFont

onPressed: () {
  DynamicCachedFonts.cacheFont(fontUrl);
},
child: const Text('Download font'),

You can pass in maxCacheObjects and cacheStalePeriod here as well.

canLoadFont, loadCachedFont, loadCachedFamily

canLoadFont is used to check whether the font is available in cache. It is usually used in combination with the loadCached* methods.

First, Check whether the font is already in cache. If it is, then load the font.

if(DynamicCachedFonts.canLoadFont(fontUrl)) {
  // To load a single font...
  DynamicCachedFonts.loadCachedFont(
    fontUrl,
    fontFamily: fontFamilyName,
  );

  // Or if you want to load multiple fonts as a family...
  DynamicCachedFonts.loadCachedFamily(
    <String>[
      fontFamilyNameBoldUrl,
      fontFamilyNameItalicUrl,
      fontFamilyNameRegularUrl,
      fontFamilyNameThinUrl,
    ],
    fontFamily: fontFamilyName,
  );
}

Now, if the font isn't available in cache, download it!

if(DynamicCachedFonts.canLoadFont(fontUrl)) {
  ...
} else {
  DynamicCachedFonts.cacheFont(fontUrl);
}

removeCachedFont

To remove a font from cache permanently, use removeCachedFont.

Note - This does not change the font immediately until a complete app restart.

Demo 3

Finally, if you want to customize their implementation, extend RawDynamicCachedFonts and override the static methods.

Have a custom font to load from Firebase Cloud Storage? Go for the DynamicCachedFonts.fromFirebase constructor! It accepts a Google Cloud Storage location which is a url starting with gs://. Other than that, it is similar to the default constructor.

Tip: Use DynamicCachedFonts.toggleVerboseLogging to log detailed statuses and configurations for debugging.

Demo 4

🐛 Bug Reports and Help

If you find a bug, please open an issue on Github or if you need any help, let's discuss on Github Discussions!

💁 Contributing

To make things easier, you can use docker compose to set up a dev environment. Just run docker compose run linux to set up a Linux dev environment or run docker compose run windows to set up a Linux dev environment.

You need to be on a Windows machine to be able to set up a docker Windows environment.

To contribute to the package, fork the repository and open a pull request!

GitHub Forks Github Stars

Comments
Releases(v1.0.0)
  • v1.0.0(Apr 10, 2022)

    Features/Updates

    • Add DynamicCachedFonts.cacheFontStream and DynamicCachedFonts.loadCachedFamilyStream static methods to cache and load font and return the font files as Streams
    • Add loadStream instance method to DynamicCachedFonts to load font files as Streams

    Internal Updates

    • Update loadCachedFamily's implementation
    • Remove all reserved characters from the url to generate safer cache keys
    Source code(tar.gz)
    Source code(zip)
  • v0.4.0(Oct 6, 2021)

    BREAKING CHANGE: verboseLog, which was deprecated in v0.2.0, has been removed. DynamicCachedFonts.toggleVerboseLogging should be used instead

    The online demo (i.e, the hosted example app) is now available. Check it out here!!

    List of changes

    What's Changed

    • Fix function parameter default values by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/131
    • Fix js script import path by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/133
    • Don't checkout the repo unnecessarily by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/134
    • Revert "Don't checkout the repo unnecessarily" by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/136
    • Add workflow to auto build and deploy example Github Pages by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/135
    • Update icons and html template for example web app by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/132
    • Fix example web deploy path by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/137
    • Enable gh pages manual deployment by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/138
    • Add necessary permissions to deploy example by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/139
    • Add firebase bucket URL required to build the example by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/140
    • Disallow jobs from running concurrently by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/141
    • Fix release pre-check syntax error by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/143
    • Update gitignore (pubspec.lock) by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/145
    • Add pubignore file by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/144
    • Update release labeler strategy and refactor/format js scripts by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/142
    • Wait on tests before deploying the example by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/146
    • Fix revert labeler behaviour (async-await) by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/148
    • Enable manual triggers for all workflows using workflow_dispatch by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/147
    • Check out scripts to run release pre-check by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/149
    • Fix check run permission error in release pre check by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/150
    • Add link to online demo to README by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/151
    • Add emojis to README by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/152
    • Update package description by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/154
    • Update package description in README by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/155
    • Drop support for deprecated API - verboseLog by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/156
    • De-clutter README by removing unnecessary badges by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/157
    • Update labeler permissions for release labeler by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/161
    • Update example app's dependencies by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/153
    • Fix invalid object error in release labeler by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/162
    • Release v0.4.0 - Drop support for verboseLog by @sidrao2006 in https://github.com/sidrao2006/dynamic_cached_fonts/pull/160

    Full Changelog: https://github.com/sidrao2006/dynamic_cached_fonts/compare/v0.3.1...v0.4.0

    Source code(tar.gz)
    Source code(zip)
  • v0.3.1(Sep 5, 2021)

  • v0.3.0(Jul 6, 2021)

  • v0.2.0(Jun 17, 2021)

    Dependency Updates

    • Minimum version constraint for flutter_cache_manager is now v3.1.2

    Features/Updates

    • verboseLog is not deprecated in all APIs. DynamicCachedFonts.toggleVerboseLogging should be used instead to toggle verbose logging

    It's likely that support for verboseLog will end in v1.0.0.

    • loadCachedFont and loadCachedFamily now throws a StateError if the font has not been cached
    • UnsupportedError is thrown if the downloaded file is not a .ttf or .otf font file
    • DynamicCachedFonts.load and loadCachedFamily now return Future<Iterable<FileInfo>> instead of void
    • cacheFont and loadCachedFont now return Future<FileInfo> instead of void

    No migration is required for the above 2 changes since a method/variable that expects void allows any other type as well.

    • DynamicCachedFonts.load now exits and throws immediately if font has already been loaded
    • Add DynamicCachedFonts.custom and RawDynamicCachedFonts.custom methods to make the API testable
    • cacheKeyFromUrl is now exported for testing. It generates the cache key, used by the cache manager, from a given url

    Internal Updates

    • Improve file format verification logic
    • Update DynamicCachedFonts.load logic
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Mar 31, 2021)

    • Add complete web cache support
    • Update documentation for some public and private APIs
    • Disable RawDynamicCachedFonts' default constructor
    • Improve logging in RawDynamicCachedFonts.loadCachedFont and font extension verification
    Source code(tar.gz)
    Source code(zip)
  • v0.0.1(Mar 28, 2021)

Owner
Aneesh Rao
A hobbyist, student and open source contributor who loves learning new things
Aneesh Rao
Download files from Firebase Storage with Flutter. List all images, videos, or other files from Firebase and download them.

Flutter Tutorial - Download Files From Firebase Storage Download files from Firebase Storage with Flutter. List all images, videos, or other files fro

Johannes Milke 28 Dec 4, 2022
Flutter Download Manager is a Cross-Platform file downloader with Parallel and Batch Download support

Flutter Download Manager is a Cross-Platform file downloader with Parallel and Batch Download support. Manage download tasks by url and be notified of status and their progress. Pause, Cancel, Queue and Resume Downloads.

Nabil Mosharraf 11 Dec 17, 2022
Memory Cache is simple, fast and global in-memory cache with CRUD features.

Memory Cache Memory Cache is simple, fast and global in-memory cache. Features Create, read, update, delete and invalidate cache. Expirable Cache Gett

Gökberk Bardakçı 6 Dec 25, 2022
⚡ Cache Manager A tidy utility to handle cache of your flutter app like a Boss.

⚡ Cache Manager A tidy utility to handle cache of your flutter app like a Boss. It provides support for both iOS and Android platforms (offcourse). ??

Abhishek Chavhan 10 Oct 25, 2022
A most easily usable cache management library in Dart. With CacheStorage, you can easily manage cache on your application.

A most easily usable cache management library in Dart! 1. About 1.1. Introduction 1.1.1. Install Library 1.1.2. Import It 1.1.3. Use CacheStorage 1.2.

Kato Shinya 1 Dec 13, 2021
Nimbostratus is a reactive data-fetching and client-side cache management library built on top of Cloud Firestore.

Nimbostratus ?? Nimbostratus is a reactive data-fetching and client-side cache management library built on top of Cloud Firestore. The Cloud Firestore

Dan Reynolds 13 Dec 15, 2022
A cached Flutter ImageProvider for Firebase Cloud Storage image objects

?? Firebase Image Provider A cached Flutter ImageProvider for Firebase Cloud Sto

KaHero 0 Nov 2, 2022
A simple flutter app that downloads a file from the internet, shows a custom-made download progress dialog and saves the file to device's internal storage

http_downloader A simple flutter app that downloads a file from the internet using the http plugin. It has a custom-designed progress dialog which dis

Akora Ing. Debrah Kwesi Buabeng 4 Apr 6, 2021
Cloud storage status

cloud_storage_status Get me a coffee: Bitcoin Address: 1DiFn7B9APaQJKfYAKqesnGM2eVM1MW6U Ethereum Address: 0xAdc43dadbE2b64DC9ba1c8766764F7cD4a2Fa915

Cybdom 38 Jul 23, 2021
A small library support load infinite for ListView - GridView on Flutter.

Paging A Flutter package that supports pagination(load infinite) for ListView, GridView Demo DataSource PageKeyedDataSource To create a PagingListView

Đặng Ngọc Đức 32 Dec 4, 2022
A powerful official extension library of Tab/TabBar/TabView, which support to scroll ancestor or child Tabs when current is overscroll, and set scroll direction and cache extent.

extended_tabs Language: English | 中文简体 A powerful official extension library of Tab/TabBar/TabView, which support to scroll ancestor or child Tabs whe

FlutterCandies 185 Dec 13, 2022
A Flutter package for accessing the Google Fonts API

google_fonts The google_fonts package for Flutter allows you to easily use any of the thousands of fonts available from fonts.google.com in your Flutt

null 670 Dec 29, 2022
Add Fontsource fonts to your flutter app. Direct access to Fontsource API.

Fontsource for Flutter Add Fontsource fonts to your flutter app. Direct access to Fontsource API. Getting started To start, create a config in either

fontsource 5 Nov 2, 2022
A command-line application provide an load optimization solution for flutter web

一个命令行工具,针对flutter web加载慢和缓存问题提供了一套解决方案。 功能 通过大文件分片和资源文件cdn化方式,优化flutter web页面加载慢问题。 通过资源文件hash化,解决浏览器强缓存导致功能无法更新问题。 开始 局部安装 dev_dependencies: flutte

Barry 10 Dec 29, 2022
Upload Files To Firebase Storage with Flutter. Pick images, videos, or other files from your device and upload them to Firebase.

Flutter Tutorial - Upload Files To Firebase Storage Upload Files To Firebase Storage with Flutter. Pick images, videos, or other files from your devic

Johannes Milke 30 Dec 28, 2022
Google Cloud Platform support package (gcloud)

Google Cloud Platform support package (gcloud) The gcloud package provides a hig

Behruz Hurramov 0 Dec 28, 2021
A font catalogue app made with flutter

Fontina An app which showcases a personally curated collection of fonts. Written in flutter, cross-platform and responsive. Uses the Fontgen API to di

Shreeyans Bahadkar 54 Nov 22, 2022
Flutter screen adaptation, font adaptation, get screen information

flutter_screenutil A flutter plugin for adapting screen and font size.Let your UI display a reasonable layout on different screen sizes! Note: This pl

OpenFlutter 3.4k Jan 6, 2023