How to use the Device's Default Font in Flutter?
Solution 1
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.
Solution 2
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;
}
Favor
Updated on July 26, 2022Comments
-
Favor almost 2 years
I opened a Java native Android app and a Flutter Android app on the same device and took screenshots of both of the 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?
-
wonpyohong about 3 yearsFileNotFoundException on Galaxy S10