📜  Flutter 默认设备字体 PlatformChannel - Dart 代码示例

📅  最后修改于: 2022-03-11 14:48:05.198000             🧑  作者: Mango

代码示例1
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;
    }