简体   繁体   English

如何在 Flutter Web CanvasKit 上启用自定义图标?

[英]How to enable Custom Icons on Flutter Web CanvasKit?

I have custom icons that work fine on IOS and Android.我有在 IOS 和 Android 上运行良好的自定义图标。 When I deploy on the Web, a crossed out box appears in each place in the App where my icon should appear.当我在 Web 上部署时,应用程序中应该出现我的图标的每个位置都会出现一个划掉的框。

The error that appears in the console is the following:控制台中出现的错误如下:

Could not find a set of Noto fonts to display all missing characters.找不到一组 Noto 字体来显示所有缺失的字符。 Please add a font asset for the missing characters.请为缺少的字符添加字体资源。

When I check the documentation on Flutter Design Fonts , I see that I have followed the steps.当我查看有关Flutter Design Fonts的文档时,我发现我已按照这些步骤进行操作。

My custom icons ( fishfarm.ttf ) are located at assets folder.我的自定义图标 ( fishfarm.ttf ) 位于 assets 文件夹中。 In pubspec.yaml I have the following:pubspec.yaml我有以下内容:

  fonts:
   - family: FishFarm
     fonts:
      - asset: assets/fishfarm.ttf

I can use my Icons in IOS and Android using Icon(FishFarm.nombreicono)我可以使用Icon(FishFarm.nombreicono)在 IOS 和 Android 中使用我的图标

I tried to use flutter run -d chrome --web-renderer html to display custom icons and it works fine.我尝试使用flutter run -d chrome --web-renderer html来显示自定义图标,它工作正常。 However, the web interface looks poor and performance is low, so I'd like to keep the default build that uses flutter, which is canvaskit for PC browsers.但是,Web 界面看起来很差,性能也很低,所以我想保留使用 Flutter 的默认构建,即 PC 浏览器的 canvaskit。

How can I use custom icons in Flutter Web CanvasKit?如何在 Flutter Web CanvasKit 中使用自定义图标?

After reviewing the documentation and looking at different alternatives, I think the best way to go today is to not use custom fonts in Flutter .在查看文档并查看不同的替代方案后,我认为今天最好的方法是不在 Flutter 中使用自定义字体 In addition to FlutterAwesomeIcons, other icons are available from Material ( https://fonts.google.com/icons ).除了 FlutterAwesomeIcons,其他图标也可以从 Material ( https://fonts.google.com/icons ) 获得。

If still, it requires using custom fonts, as in my case.如果仍然如此,它需要使用自定义字体,就像我的情况一样。 The best thing is to load the PNG files inside the assets, include the folder in pubspec.yaml and make a function that shows it and on which you can change the color, size, among others.最好的办法是在资产中加载 PNG 文件,将文件夹包含在 pubspec.yaml 中,并创建一个显示它的函数,您可以在该函数上更改颜色、大小等。

What I made was a class called FarmIcons like so...我所做的是一个名为 FarmIcons 的类,就像这样......

  class FarmIcons extends StatelessWidget {
    const FarmIcons({ Key? key, required this.icono, this.height = 30.0,  this.padding = 12.0 }) : super(key: key);
  
    final String icono;
    final double height;
    final double padding;

    @override
    Widget build(BuildContext context) {

      String pathImage =
        (icono == 'bulto')      ? 'assets/icon/bulto.png' :
        (icono == 'mortalidad') ? 'assets/icon/mortalidad.png' :
        (icono == 'lago')      ? 'assets/icon/lago.png' :
        (icono == 'alevinos')    ? 'assets/icon/alevinos.png' :
        'assets/icon/general.png';

      return Padding(
        padding: EdgeInsets.all(padding),
        child: Image.asset(
                  pathImage,
                  width: 20,
                  color: Colors.black,
                  height: height,
                  fit: BoxFit.scaleDown,
                ),
      );
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM