Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Device's Default Font in Flutter?

I opened a Java native Android app and a Flutter Android app on the same device and took screenshots of both of the applications:

Screenshots of the Java (left) and Flutter (right) applications

As you can see, the fonts are they are using to display their text are different.
Both applications were run using the Samsung Galaxy S5 Choco Cooky default theme.

The Java native app adapts and displays the custom Samsung Galaxy font, whereas the Flutter app does not adjust fonts.

How can I configure my Flutter app to use the device's default font?

like image 424
Favor Avatar asked Aug 19 '18 00:08

Favor


People also ask

What font does Flutter use by default?

The default font of MaterialApp is roboto , a Google font.


2 Answers

Create a PlatformChannels for the TextView and access this Android TextView into your Flutter Application. And Use it instead of the Text widget of the Flutter.

Check out this article for more information.

like image 186
ibhavikmakwana Avatar answered Oct 02 '22 13:10

ibhavikmakwana


In Flutter, default font on Android is Roboto, and on iOS is .SF UI Pro and .SF UI Text.

check out this answer https://stackoverflow.com/a/56339147/1776125

on iOS this is OK, but on android there are lots of ROMs with different default font family.

In order to get the device default font family, I wrote a platform channel on Android, here is the code to get device default font family:

private String getDefaultFont() {
        File configFilename = new File("/system/etc/system_fonts.xml");
        // sans-serif is the default font family name in Android SDK, check out the code in Typeface.java
        String defaultFontName = "sans-serif";

        try {
            FileInputStream fontsIn = new FileInputStream(configFilename);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(fontsIn, null);
            boolean done = false;
            boolean getTheText = false;
            int eventType;
            while (!done) {
                eventType = parser.next();
                if (eventType == parser.START_TAG && parser.getName().equalsIgnoreCase("name")) {
                    getTheText = true;
                }
                if (eventType == parser.TEXT && getTheText) {
                    // first name
                    defaultFontName = parser.getText();
                    done = true;
                }
                if (eventType == parser.END_DOCUMENT) {
                    done = true;
                }
            }
        } catch (RuntimeException e) {
            System.err.println("Didn't create default family (most likely, non-Minikin build)");
        } catch (FileNotFoundException e) {
            System.err.println("GetDefaultFont: config file Not found");
        } catch (IOException e) {
            System.err.println("GetDefaultFont: IO exception: " + e.getMessage());
        } catch (XmlPullParserException e) {
            System.err.println("getDefaultFont: XML parse exception " + e.getMessage());
        }
        return defaultFontName;
    }
like image 38
DongXu Avatar answered Oct 02 '22 13:10

DongXu