解决Flutter3.10+版本web端输入emoji表情显示为黑白的问题

解决Flutter3.10+版本web端输入emoji表情显示为黑白的问题

问题可能在项目flutter项目升级flutter到3.1版本之后出现
原因在github中有提到
Some apps don’t want to use color emojis due to its size (~24MB). This PR makes color emojis opt-in.
就是说为了减少体积,就把彩色emoji弄成黑白了。

<project-dir>/web/index.html
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <!-- This script installs service_worker.js to provide PWA functionality to
       application. For more information, see:
       https://developers.google.com/web/fundamentals/primers/service-workers -->
    <div id="loader">
      <h1>
        loading
      </h1>
    </div>

    <script>
      var serviceWorkerVersion = null;
      var scriptLoaded = false;
      function loadMainDartJs() {
        if (scriptLoaded) {
          return;
        }
        scriptLoaded = true;
        var scriptTag = document.createElement("script");
        scriptTag.src = "main.dart.js";
        scriptTag.type = "application/javascript";
        document.body.append(scriptTag);
      }

      if ("serviceWorker" in navigator) {
        // Service workers are supported. Use them.
        window.addEventListener("load", function () {
          // Wait for registration to finish before dropping the <script> tag.
          // Otherwise, the browser will load the script multiple times,
          // potentially different versions.
          var serviceWorkerUrl =
            "flutter_service_worker.js?v=" + serviceWorkerVersion;
          navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => {
            function waitForActivation(serviceWorker) {
              serviceWorker.addEventListener("statechange", () => {
                if (serviceWorker.state == "activated") {
                  console.log("Installed new service worker.");
                  loadMainDartJs();
                }
              });
            }
            if (!reg.active && (reg.installing || reg.waiting)) {
              // No active web worker and we have installed or are installing
              // one for the first time. Simply wait for it to activate.
              waitForActivation(reg.installing || reg.waiting);
            } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
              // When the app updates the serviceWorkerVersion changes, so we
              // need to ask the service worker to update.
              console.log("New service worker available.");
              reg.update();
              waitForActivation(reg.installing);
            } else {
              // Existing service worker is still good.
              console.log("Loading app from service worker.");
              loadMainDartJs();
            }
          });

          // If service worker doesn't succeed in a reasonable amount of time,
          // fallback to plaint <script> tag.
          setTimeout(() => {
            if (!scriptLoaded) {
              console.warn(
                "Failed to load app from service worker. Falling back to plain <script> tag."
              );
              loadMainDartJs();
            }
          }, 4000);
        });
      } else {
        // Service workers not supported. Just drop the <script> tag.
        loadMainDartJs();
      }
    </script>
  </body>
</html>
效果

黑白色emoji

2.解决方法

google了一下,关键在于
useColorEmoji: true 不知道怎么写
参考

onEntrypointLoaded: function(engineInitializer) {
  engineInitializer.initializeEngine().then(function(appRunner) {
    useColorEmoji: true,
    appRunner.runApp();
  });
}

以及

官方文档

Example: Display a progress indicator
To give the user of your application feedback during the initialization process, use the hooks provided for each stage to update the DOM:

{{flutter_js}}
{{flutter_build_config}}

const loading = document.createElement('div');
document.body.appendChild(loading);
loading.textContent = "Loading Entrypoint...";
_flutter.loader.load({
  onEntrypointLoaded: async function(engineInitializer) {
    loading.textContent = "Initializing engine...";
    const appRunner = await engineInitializer.initializeEngine();

    loading.textContent = "Running app...";
    await appRunner.runApp();
  }
});

两者结合之后

<project-dir>/web/index.html

把原先index.html中的替换,替换如下

<script>
  {{flutter_js}}
  {{flutter_build_config}}
  console.log("begin flutter loader.load");
  _flutter.loader.load({
    onEntrypointLoaded: async function (engineInitializer) {
      console.log("begin enable emoji color");
      const appRunner = await engineInitializer.initializeEngine({
        useColorEmoji: true,
      });
      console.log("Running app...");
      await appRunner.runApp();
    },
  });
</script>
效果

彩色emoji
转载自本人github文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值