1

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.