Giter VIP home page Giter VIP logo

trendingtechnology / dynamic_cached_fonts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sidrao2006/dynamic_cached_fonts

0.0 1.0 0.0 2.65 MB

A customizable dynamic font loader for flutter with caching enabled. Supports Firebase Cloud Storage too!

Home Page: https://pub.dev/packages/dynamic_cached_fonts

License: BSD 3-Clause "New" or "Revised" License

Java 0.53% Dart 54.57% Objective-C 0.73% CMake 14.54% C++ 23.78% C 1.07% Swift 1.16% HTML 2.21% JavaScript 0.70% Shell 0.72%

dynamic_cached_fonts's Introduction

Dynamic Cached Fonts

GitHub license Pub Version Supported Platforms

Code Analysis Documentation Analysis Unit Tests Integration Tests Release and publish package

A customizable dynamic font loader for flutter with caching enabled. Supports Firebase Cloud Storage too!

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 in your pubspec.yaml file.

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 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 the 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 loaders.

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: Every method, except load(), has a parameter named verboseLog which is used 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 app

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

GitHub forks

dynamic_cached_fonts's People

Contributors

sidrao2006 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.