Skip to content

Flutter - how add custom font for svg image? #680

Open

Description

@FetFrumos

I have svg image(export from Adobe Xd). For the correct display of this file, I need to install an additional font in the system(MAC or Windows) - then the picture is displayed correctly, without this font it is incorrect. but I need to display this drawing in application. I using flutter_svg plugin. I download ttf of font, add in assets and pubspec. But after that it doesn't work either. how add custom font for svg image(with flutter_svg)?

Activity

  1. dnfield commented on Mar 13, 2022

    @dnfield
    Owner

    I'd find it helpful if you shared a small but complete example of the issue.

    Some places where this could possibly go wrong:

    Obviously if it's that last point, there's a bug in this package. A reproduction case would be able to narrow this down.

  2. L4hibYoussef commented on Mar 15, 2023

    @L4hibYoussef

    Is there any updates on this. I am appearing to have the same issue. Font is loaded before image is rendered and font is available for use in the app. I am building the app for web using canvaskit.

  3. devalev commented on Jul 12, 2023

    @devalev

    Is there any updates on this. I am appearing to have the same issue. Font is loaded before image is rendered and font is available for use in the app. I am building the app for web using canvaskit.

    You need to load the font when executing the application.

    void main() async {
      runApp(const MyApp());
    }
    
    void loadFont() async {
      var fontLoader = FontLoader("FontName");
      fontLoader.addFont(rootBundle.load('fonts/FontName.ttf'));
      await fontLoader.load();
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        loadFont();
        return Container();
      }
    }
    

    Please note that sometimes the font-family is specified in extra quotes in the svg file

    font-family="FontName"
    

    and

    font-family="'FontName'"
    

    these are different things, and, accordingly, you need to import different fonts

    var fontLoader = FontLoader("FontName");
    
    var fontLoader = FontLoader("'FontName'");
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions