0
点赞
收藏
分享

微信扫一扫

Flutter 动态加载字体

Brose 2021-10-04 阅读 95

如果需要从网络上下载字体并且应用

    static Future<bool> downloadFontAndLoad(String fontFamily, String fontFamilyUrl) async {
    if (TextUtil.isEmpty(fontFamilyUrl) || TextUtil.isEmpty(fontFamilyUrl)) {
      return false;
    }
    //加载本地字体到app中
    Future<void> loadFontToApp(String path) async {
      try {
        var fontLoader = FontLoader(fontFamily);
        Future<ByteData> _byteData =  File(path).readAsBytes().then((value){
          return value.buffer.asByteData();
        });
        fontLoader.addFont(_byteData);
        await fontLoader.load();
      } catch (e) {
        print(e);
      }
    }

    File ttfFile = await FileUtil.getFilePath("ttf/", fontFamily);
    //如果本地已经存在这个字体,则加载本地
    if (ttfFile.existsSync()) {
      await loadFontToApp(ttfFile.path);
      return true;
    }

    String newTTFFile = await FileUtil.createFile2(ttfFile);
    String localFontFile = await DownFileUtil.down(fontFamilyUrl, newTTFFile); //检查是否需要本地下载字体
    await loadFontToApp(localFontFile); 
    return true;
  }
举报

相关推荐

0 条评论