Skip to content

Instantly share code, notes, and snippets.

@wilsonowilson
Created July 8, 2021 18:41
Show Gist options
  • Save wilsonowilson/011e7b00d6f3eae3f5829d9e92d74e25 to your computer and use it in GitHub Desktop.
Save wilsonowilson/011e7b00d6f3eae3f5829d9e92d74e25 to your computer and use it in GitHub Desktop.
Swapping web renderers at runtime
<body>
<script src="js/renderer_switcher.js"></script>
<script>
<!-- Service worker code -->
<!-- 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 -->
...
abstract class RendererSwitcher {
static void setHtml() {
js.context.callMethod('setHtml');
}
static void setCanvaskit() {
js.context.callMethod('setCanvaskit');
}
static WebRenderer get renderer {
return js.context.callMethod('getRenderer') == 'html'
? WebRenderer.html
: WebRenderer.canvaskit;
}
static bool get usingHtml {
return renderer == WebRenderer.html;
}
static bool get usingCanvaskit {
return !usingHtml;
}
}
enum WebRenderer { html, canvaskit }
// Add the file to the root of your web folder.
let localStorage = window.localStorage;
let renderer = getRenderer();
let useHtml = renderer == 'html';
if (useHtml) {
window.flutterWebRenderer = "html";
} else {
window.flutterWebRenderer = "canvaskit";
}
console.log(`Running on ${window.flutterWebRenderer}`);
function setHtml() {
localStorage.setItem('renderer', 'html');
window.location.reload();
}
function setCanvaskit() {
localStorage.setItem('renderer', 'canvaskit');
window.location.reload();
}
function getRenderer() {
return localStorage.getItem('renderer');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment