A DOM form inside a Flutter web app
Flutter draws every widget itself with CanvasKit, so a Flutter web app has no real inputs apart from one hidden text field.
This app shows a real <form> with an <input>, a <select> and a <textarea> as one of its widgets.
Flutter gets the form's pixels through createImageFromTextureSource() in dart:ui_web and rotates, tilts, fades and blurs them with its own widgets.
The browser still handles the form's input. Load the app, type in the form, then move the Flutter sliders.
Flutter 3.50.1 with CanvasKit, served from this site. It isn't loaded until you ask.
The text field and the four sliders at the top are Flutter widgets. The form below them is the DOM form: the caret, the selection, the keyboard on a phone, autofill and the native select picker are the browser's. The yellow badge is a Flutter widget drawn over the form. The second design restarts the page with a different engine setting.
Show the code a DOM form as a Flutter widget
- The form is a child of a 2D canvas that sits over the Flutter view.
- The canvas is invisible and still takes input.
- JavaScript: on each paint event, draw the form into the canvas and wake Flutter.
- JavaScript: what Flutter is given. Three ways of handing over the same canvas.
- Dart: the interface to the page.
- Dart: canvas to
ui.Image. OnecreateImageFromTextureSource()call per paint event. - Dart: the widget. Flutter sets the width, the browser answers with the height, and a
RawImageshows the pixels. - Dart: the widget in a scene.
Transform,OpacityandImageFilteredapply to it like any widget. - Dart: report where the widget was drawn.
- JavaScript: put the real form there. Flutter's matrix becomes a CSS
matrix3d()on the canvas. - JavaScript: layout.
- The second design: the form inside Flutter's canvas. Flutter is given the form itself, and one
texImage2D()call is replaced bytexElementSubImage2D(). - Starting Flutter in a host element.
- Loading it on request.
- All of main.dart.
- All of bridge.js.
How it works
createImageFromTextureSource() makes a ui.Image from an image, a video, a canvas, an ImageBitmap or a VideoFrame.
It doesn't take an element, so the page needs something that turns the form into one of those. Two designs do that here.
-
A second canvas. The form is a child of a 2D canvas with
content="drawable". Eachpaintevent draws the form withdrawElementImage(), and Flutter makes a newui.Imagefrom the canvas. The same canvas lies over the Flutter view withopacity: 0. After each frame Flutter reports the widget's 4×4 matrix, and the page sets it as a CSSmatrix3d()on the canvas, so the real fields are under the pointer where Flutter drew them. This works with an unchanged Flutter engine. -
The form inside Flutter's own canvas. With the engine option
canvasKitForceMultiSurfaceRasterizer, CanvasKit renders into a WebGL canvas on the page. The page setscontent="drawable"on that canvas and moves the form into it. Flutter is given the form itself as the texture source. CanvasKit uploads a source withgl.texImage2D(…, source); the page replaces that one call withtexElementSubImage2D(). The matrix goes toupdateElementGeometry(). There's no second canvas and no copy. The patch stands in for a change to the engine.
Live checks
The checks load two more copies of the Flutter app in a small frame, one for each design, so they don't touch the one above. They take about 20 seconds.
| Result | Experiment | Detail |
|---|
Findings
Measured in headless Chromium 157 on 2026-11-21 with software rendering, at 1× and at a 2.625 device scale with touch. Flutter 3.50.1, Dart 3.14.0.
createImageFromTextureSource()doesn't take an element or anElementImage. WithtransferOwnership: falsethe engine callscreateImageBitmap()on the source, which throws aTypeErrorfor both. WithtransferOwnership: truethe call returns aui.Imageof the size it was told, and the error comes when the image is used: CanvasKit and Skwasm both upload lazily.- It takes a 2D canvas that drew the element. A canvas with
content="drawable"draws the form withdrawElementImage()on eachpaintevent, and Flutter makes a newui.Imagefrom it. The pixels read back from the image withtoByteData()match the canvas. Flutter'sTransform(with perspective),OpacityandImageFilteredapply to the form, and a Flutter widget is drawn over it. This needs no change to Flutter. It worked the same with CanvasKit (dart2js) and with single-threaded Skwasm (dart2wasm). - The copy is cheap next to Flutter's own frame. For a 924×572 px form image:
drawElementImage()0.1 ms;createImageFromTextureSource()4 to 5 ms when the engine copies the canvas withcreateImageBitmap(), and 0.2 ms withtransferOwnership: true, which hands CanvasKit the canvas itself. Flutter's raster time for the frame was 37 to 41 ms in every case, on a software renderer. The texture upload is inside the raster time and couldn't be separated. These numbers need a phone to mean anything: the page shows them live under the demo. - Input works through the same canvas laid over the Flutter view. The canvas has
opacity: 0and Flutter's 4×4 widget matrix as a CSSmatrix3d(). Typing, the caret and the<select>by keyboard worked with the form rotated 38° and tilted 35° in perspective. Drag selection and Tab between the three fields worked at the starting angle. A click hands focus between Flutter'sTextFieldand the DOM fields in both directions. - Flutter's canvas is in a shadow root and, by default, isn't the canvas it renders into. The canvas inside
flt-glass-pane's shadow root has abitmaprenderercontext. CanvasKit renders into anOffscreenCanvasand transfers bitmaps.content="drawable"can be set on the visible canvas: a child of it getspaintevents andupdateElementGeometry()moves its hit box. Its pixels can't be used: abitmaprendererhas no draw call for elements, and anElementImagecaptured there is refused by every other canvas with "The source was captured from a different canvas". - With the multi-surface rasterizer the form can live in Flutter's own canvas. The engine option
canvasKitForceMultiSurfaceRasterizer(in the engine source, the mode chosen for Safari and Firefox) makes CanvasKit draw into a WebGL2 canvas on the page. The form is moved into it, Flutter is handed the form as the texture source, and CanvasKit'sgl.texImage2D(…, source)is replaced bytexElementSubImage2D()in a short patch. There's no second canvas and no copy:createImageFromTextureSource()took 0.2 ms and Flutter's raster 8 ms for the same form. The two raster times come from different rasterizers, so they don't measure the copy alone. The patch stands in for an engine change. Not tried with Skwasm. updateElementGeometry()accepts a perspective matrix. In the in-canvas design Flutter's 4×4 matrix is passed ascanvasTransform, and the three fields are hit tested correctly while rotated and tilted.- Flutter takes focus back when focus moves inside its view. When Flutter's hidden text input loses focus to another element in the same
flutter-view, the engine refocuses its input. With the form inside Flutter's canvas a click on a DOM field after using the FlutterTextFielddidn't move focus. The bridge blurs Flutter's input onpointerdownfirst, which closes Flutter's text connection. - Flutter listens for keys on
windowin the capture phase. It sees every key typed into the form. With the form inside its view, Tab in the email field moved focus to the FlutterTextFieldinstead of the next DOM field. The bridge adds its own capture listener before the engine starts and stops key events that come from the form. - Flutter sets
pointer-events: noneon its scene. A form moved into Flutter's canvas inherits it and needspointer-events: auto. Page stylesheets don't reach the shadow root, so the bridge adds the stylesheet there. - The browser does not know what Flutter draws on top. The yellow badge is a Flutter widget over a corner of the form, and a pointer on the badge still goes to the form. The same was found for egui on /native-ui/. Neither design here guards against it.
- Tab order is split. Tab walks the three DOM fields in DOM order and then leaves the form. Flutter's widgets aren't in that order.
- Not verified on a phone. Frame rates, the on-screen keyboard, IME composition, where the native
<select>picker opens for a rotated form, and screen readers weren't tested. Multi-threaded Skwasm, which renders in a worker, wasn't tested either: the site isn't cross-origin isolated.