Created
July 8, 2021 18:41
-
-
Save wilsonowilson/011e7b00d6f3eae3f5829d9e92d74e25 to your computer and use it in GitHub Desktop.
Swapping web renderers at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 --> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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