Flutter Web 在网页上显示表情符号为黑白色。

huangapple go评论144阅读模式
英文:

Flutter Web shows emojis in black&white

问题

在iOS/Android应用中,表情符号显示正常。但在任何网络浏览器(例如Chrome)中,表情符号会显示为黑白。我也尝试了不同的字体族,但结果都一样。

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Text('享受! &#129395; 如果有任何问题')
      ),
    );
  }
}
英文:

In ios/android apps emojis are shown correctly. But using any web-browser (e.g. Chrome) the emoji appears in black and white. I also tried different Font-Families but with the same result.

class _MyHomePageState extends State&lt;MyHomePage&gt; {
      @override
      Widget build(BuildContext context) {
        return const Scaffold(
          body: Center(
            child: Text(&#39;Enjoy! &#129395; If there\&#39;s any question&#39;)
          ),
        );
      }
    }

Flutter Web 在网页上显示表情符号为黑白色。
Flutter Web 在网页上显示表情符号为黑白色。

答案1

得分: 11

由于彩色表情字体的体积较大(约24MB),Flutter团队在Flutter 3.10.0中将彩色表情设为可选功能(参考链接)。

要启用它,请在您的index.html文件中初始化引擎时将useColorEmoji标志设置为true:

engineInitializer.initializeEngine({
  // 其他配置...
  useColorEmoji: true,
});
英文:

Due to the large size of the color emoji font (~24MB), the Flutter team made color emojis an opt-in feature in Flutter 3.10.0 (ref).

To enable it, set the useColorEmoji flag to true when initializing the engine in your index.html file:

engineInitializer.initializeEngine({
  // other config...
  useColorEmoji: true,
});

答案2

得分: 5

现在,存在与flutter-engine相关的问题。这可能是在此处引入的:链接

最初的问题似乎是flutter提供的字体不支持表情符号字符(.AppleSystemUIFont),因此会使用第一个回退字体NatoEmoji。

临时解决方案是将Apple Color Emoji字体文件提供给Web应用程序作为资源。你可以在这里找到该文件
"/System/Library/Fonts/Apple Color Emoji.ttc"
你可以将其复制到你的应用程序资产中。完成后,

...
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  universal_io: ^2.2.0
...

flutter:
  ...

  fonts:
    - family: "Apple Color Emoji"
      fonts:
        - asset: assets/fonts/apple_color_emoji.ttc

然后在main.dart中

import 'package:flutter/material.dart';
import 'package:universal_io/io.dart';

void main() async {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    final fontFamilyFallback = <String>[];
    if (Platform.isIOS || Platform.isMacOS) {
      fontFamilyFallback.add("Apple Color Emoji");
    }
    return MaterialApp(
      theme: ThemeData(fontFamilyFallback: fontFamilyFallback),
      home: Scaffold(
        appBar: AppBar(),
        body: Center(
          child: Builder(builder: (context) {
            return const SelectableText(
              '🙃',
              style: TextStyle(
                fontSize: 120,
              ),
            );
          }),
        ),
      ),
    );
  }
}

输出:
Flutter Web 在网页上显示表情符号为黑白色。

英文:

Right now, it is issue with the flutter-engine. It was probably introduced in here https://github.com/flutter/engine/commit/7406fd81865292772689a1bbbc2239c665ba07d8

The initial issue looks like the fonts provided in flutter does not have support for emoji characters (.AppleSystemUIFont) hence first fallback font is used that is NatoEmoji.

The temporary solution is to provide Apple Color Emoji font file to the web app as asset. You can find the file here
"/System/Library/Fonts/Apple Color Emoji.ttc"
You can copy this to your app's asset. Once that is done,

...
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  universal_io: ^2.2.0
...

flutter:
  ...

  fonts:
    - family: &quot;Apple Color Emoji&quot;
      fonts:
        - asset: assets/fonts/apple_color_emoji.ttc

Then inside main.dart

import &#39;package:flutter/material.dart&#39;;
import &#39;package:universal_io/io.dart&#39;;

void main() async {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    final fontFamilyFallback = &lt;String&gt;[];
    if (Platform.isIOS || Platform.isMacOS) {
      fontFamilyFallback.add(&quot;Apple Color Emoji&quot;);
    }
    return MaterialApp(
      theme: ThemeData(fontFamilyFallback: fontFamilyFallback),
      home: Scaffold(
        appBar: AppBar(),
        body: Center(
          child: Builder(builder: (context) {
            return const SelectableText(
              &#39;&#128518;&#39;,
              style: TextStyle(
                fontSize: 120,
              ),
            );
          }),
        ),
      ),
    );
  }
}

Output:
Flutter Web 在网页上显示表情符号为黑白色。

答案3

得分: 4

使用谷歌的Noto Color Emoji(按照以下步骤):

 1. 下载字体 [下载 Noto Color Emoji][1]

  [1]: https://fonts.google.com/noto/specimen/Noto+Color+Emoji

 2. 将其添加到 pubspec.yaml 中 `flutter:
  fonts:
    - family: Noto Color Emoji
      fonts:
        - asset: assets/fonts/NotoColorEmoji-Regular.ttf`

 3. 在 TextStyle 类中像这样使用它 `TextStyle(
  fontFamilyFallbackm: [
    'Apple Color Emoji',
    'Noto Color Emoji',
  ],
)`

与此不同的是尝试:

 . 删除 web 文件夹

 . 使用命令 `flutter upgrade` 更新 flutter

 . 运行命令 `flutter create .`
英文:

Use Noto Color Emoji from google (follow this steps):

  1. Download the font Download Noto Color Emoji
  1. Added it to pubspec.yaml `flutter:
    fonts:

    • family: Noto Color Emoji
      fonts:

      • asset: assets/fonts/NotoColorEmoji-Regular.ttf`
  2. And in TextStyle class use it like this TextStyle(
    fontFamilyFallbackm: [
    &#39;Apple Color Emoji&#39;,
    &#39;Noto Color Emoji&#39;,
    ],
    )

Unlike that try to:

. Delete web folder

. Update flutter by using command flutter upgrade

. Run command flutter create .

huangapple
  • 本文由 发表于 2023年2月14日 02:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定