Skip to content

Instantly share code, notes, and snippets.

@smolck
Created November 27, 2020 19:53
Show Gist options
  • Save smolck/eb1473c4b11a6ab4d761a0531efe375a to your computer and use it in GitHub Desktop.
Save smolck/eb1473c4b11a6ab4d761a0531efe375a to your computer and use it in GitHub Desktop.
Loading fonts at runtime in Dart/Flutter
import 'dart:io';
import 'package:flutter/services.dart';
Future<ByteData> getFont() async {
var bytes = await File('${Platform.environment['HOME']}/.local/share/fonts/path/to/font}').readAsBytes();
return ByteData.sublistView(bytes);
}
void main() async {
var fontLoader = FontLoader('SomeFontFamily')..addFont(getFont());
await fontLoader.load();
// Call the usual Flutter `runApp(...)` here.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment