Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何包含用户字体_Flutter_Dart - Fatal编程技术网

Flutter 如何包含用户字体

Flutter 如何包含用户字体,flutter,dart,Flutter,Dart,所以我有一个应用程序在颤振,我用自定义字体从谷歌字体。这些字体包含在字体文件夹中,我在pubspec.yaml中导入了这些字体 fonts: - family: Source Sans Pro fonts: - asset: fonts/SourceSansPro-Regular.ttf weight: 400 - asset: fonts/SourceSansPro-SemiBold.ttf weight: 600 - asset: fonts/

所以我有一个应用程序在颤振,我用自定义字体从谷歌字体。这些字体包含在
字体
文件夹中,我在
pubspec.yaml
中导入了这些字体

fonts:
  - family: Source Sans Pro
  fonts:
    - asset: fonts/SourceSansPro-Regular.ttf
    weight: 400
    - asset: fonts/SourceSansPro-SemiBold.ttf
    weight: 600
    - asset: fonts/SourceSansPro-Bold.ttf
    weight: 700
    - asset: fonts/SourceSansPro-Black.ttf
    weight: 900 
但我希望用户能够添加自己的字体(TTf文件)。我可以让他们选择TTf文件并将其保存在
appsDocDirectory
,之后,我如何使用应用程序中的字体?

试试这个:从

Future loadFont(字符串路径)异步{
文件=文件(路径);
Uint8List bytes=wait file.readAsBytes();
返回ByteData.view(bytes.buffer);
}
var custom=FontLoader('Pacifico');
custom.addFont(loadFont(“/storage/emulated/0/Download/Pacifico Regular.ttf”);
等待custom.load();
setState((){});
编辑:

现在,您的字体将作为
'Pacifico'
等待custom.load()之后可用完成

Future<ByteData> loadFont(String path) async{
      File file = File(path);
      Uint8List bytes = await file.readAsBytes();
      return ByteData.view(bytes.buffer);
    }
    
    var custom = FontLoader('Pacifico');
    custom.addFont(loadFont("/storage/emulated/0/Download/Pacifico-Regular.ttf"));
    await custom.load();
    setState(() {});