Reputation: 1013
So I have an app in flutter where I have used custom font from Google fonts. These fonts are included in the fonts
folder and I have imported in pubspec.yaml
like so,
fonts:
- family: Source Sans Pro
fonts:
- asset: fonts/SourceSansPro-Regular.ttf
weight: 400
- asset: fonts/SourceSansPro-SemiBold.ttf
weight: 600
- asset: fonts/SourceSansPro-Bold.ttf
weight: 700
- asset: fonts/SourceSansPro-Black.ttf
weight: 900
But I want the users to be able to add their own fonts (TTf files). I can ask them to pick the TTf file and save it in appsDocDirectory
, after this, how can I use the font in the app?
Upvotes: 1
Views: 676
Reputation: 2393
Try this: got from https://github.com/flutter/flutter/issues/55458
Future<ByteData> loadFont(String path) async{
File file = File(path);
Uint8List bytes = await file.readAsBytes();
return ByteData.view(bytes.buffer);
}
var custom = FontLoader('Pacifico');
custom.addFont(loadFont("/storage/emulated/0/Download/Pacifico-Regular.ttf"));
await custom.load();
setState(() {});
Edit:
now your font will be available as 'Pacifico'
after await custom.load();
completes
Upvotes: 2