美文网首页Flutter开发圈
flutter 使用从网络下载的font

flutter 使用从网络下载的font

作者: 云上听风 | 来源:发表于2019-06-24 23:51 被阅读53次

flutter中文网的文档只有从资源载入自定义字体的办法,要想程序运行时载入自定义路径的字体可以使用FontLoader。
参考:
flutter 使用自定义字体
另一个例子

代码:

Future<void> readFont(String path) async {
    var fontLoader = FontLoader("Montserrat-Light");
    fontLoader.addFont(getCustomFont(path));
    await fontLoader.load();
  }

  Future<ByteData> getCustomFont(String path) async {
    ByteData byteData = await rootBundle.load(path);
    return byteData;
  }

readFont成功后就可以使用FontLoader初始化时传入的字体名来显示:

final textStyle = complete
        ? TextStyle(
            fontSize: 20,
            fontFamily: 'Montserrat-Light',
            fontWeight: FontWeight.bold,
            color: Colors.red,
          )
        : TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.normal,
            color: Colors.black,
          );

相关文章

网友评论

    本文标题:flutter 使用从网络下载的font

    本文链接:https://www.haomeiwen.com/subject/wqskqctx.html