简体   繁体   English

如何检测 Flutter web CanvasKit 或 HTML 渲染器?

[英]How to detect Flutter web CanvasKit or HTML renderer?

There are two backends for Flutter Web currently, namely HTML and CanvasKit. Flutter Web目前有两个后端,分别是HTML和CanvasKit。 As a library author, for performance reasons, I would like to identify which backend the app is currently running on.作为库作者,出于性能原因,我想确定应用程序当前正在哪个后端运行。 Is there anyway to detect that in code?反正有没有在代码中检测到这一点?

Flutter 2.0 Update (Mar 2021) Flutter 2.0 更新(2021 年 3 月)

Now that there is a new "auto" mode to choose web renderers.现在有一个新的“自动”模式可以选择 web 渲染器。 The correct way to check is described in https://github.com/flutter/flutter/issues/73369#issuecomment-760543461 https://github.com/flutter/flutter/issues/73369#issuecomment-760543461中描述了正确的检查方法

import 'dart:js' as js;
final isCanvasKit = js.context['flutterCanvasKit'] != null;

(Credit: github.com/slavap) (来源:github.com/slavap)


After digging into issues in the Flutter repository, I found the answer myself在深入研究 Flutter 存储库中的问题后,我自己找到了答案

As of Aug 2020, you can detect whether the backend is CanvasKit by截至2020年8月,您可以通过以下方式检测后端是否为CanvasKit

const bool.fromEnvironment('FLUTTER_WEB_USE_SKIA', defaultValue: false)

On web, a flutterCanvasKit property will be added to the window when running with CanvasKit.在 web 上,使用 CanvasKit 运行时,将向 window 添加一个flutterCanvasKit属性。 This means that we can usedart:js to access the window via context and retrieve the property from there.这意味着我们可以使用dart:js通过context访问 window 并从那里检索属性。

Boolean getter Boolean 吸气剂

To have a full example here, I want to expand on the GitHub comments and add a complete function with conditional support.在这里有一个完整的例子,我想扩展GitHub 评论并添加一个完整的 function 有条件支持。 The easiest way to set up the function is as a boolean getter:设置 function 的最简单方法是使用 boolean 吸气剂:

import 'dart:js';

/// Whether the CanvasKit renderer is being used on web.
///
/// Always returns `false` on non-web.
bool get isCanvasKit => context['flutterCanvasKit'] != null;

This getter works only on web and will return whether you are running on CavasKit, according to @yjbanov . 根据@yjbanov 的说法,这个 getter 仅适用于 web 并且将返回您是否在 CavasKit 上运行。

Conditional logic条件逻辑

In order to make your app compile on non-web (mobile and desktop), you need to make sure to not import this file when not running on web.为了使您的应用程序在非 Web(移动和桌面)上编译,您需要确保在不在 web 上运行时不导入此文件。
The easiest way to do this is using conditional exports :最简单的方法是使用条件导出

export 'canvas_kit_stub.dart' if (dart.library.html) 'canvas_kit_web.dart';

You store the line above in a file called canvas_kit.dart .您将上面的行存储在一个名为canvas_kit.dart的文件中。 Then, you store the getter from above in a file called canvas_kit_web.dart in the same directory.然后,将上面的 getter 存储在同一目录中名为canvas_kit_web.dart的文件中。 The last step is creating the stub file ( canvas_kit_stub.dart ) with the following contents:最后一步是创建存根文件 ( canvas_kit_stub.dart ),其内容如下:

/// Whether the CanvasKit renderer is being used on web.
///
/// Always returns `false` on non-web.
bool get isCanvasKit => false;

Importing输入

Now, you can simply import 'canvas_kit.dart';现在,您可以简单地import 'canvas_kit.dart'; (with the path pointing to the file you created) and this will work on both mobile, desktop, and web. (路径指向您创建的文件),这将适用于移动设备、桌面设备和 web。 You need to make sure to not import either the _stub or _web version.您需要确保不导入_stub_web版本。

So I finally found it.所以我终于找到了。 I noticed that the HTML body contains a property called "fit-renderer" that specifies the renderer used.我注意到 HTML 主体包含一个名为“fit-renderer”的属性,它指定使用的渲染器。 So you can use the following code所以你可以使用下面的代码

import 'dart:html' as HTML;

bool get usingHtmlRenderer =>
    html.document.body.getAttribute("flt-renderer").contains("html");

which returns "true" if you are using HTML renderer and "false" if you are using canvaskit.如果您使用的是 HTML 渲染器,则返回“true”;如果您使用的是 canvaskit,则返回“false”。

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

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