From df75d249b2ba94b8b098cfa634d7074034df6134 Mon Sep 17 00:00:00 2001 From: David Iglesias Date: Fri, 22 Mar 2024 17:53:26 -0700 Subject: [PATCH] [web][docs] Improve HtmlElementView widget docs. (#145192) This PR expands the `HtmlElementView` widget DartDocs, with the following sections: * Usage: How to use the widget, two ways: * The `HtmlElementView.fromTagName` constructor * The `PlatformViewRegistry` way * Lifecycle: There's an `onCreated` callback on the widget. When does it get called? * HTML Lifecycle: How to listen to events coming from the DOM. * Visibility: what is the `isVisible` property for? Small additional tweaks here and there to mention common pitfalls of using HtmlElementView on the web, and mentions to workarounds, like `package:pointer_interceptor`. ## Issues * Fixes: https://github.com/flutter/flutter/issues/143922 * Fixes: https://github.com/flutter/flutter/issues/49634 * Related: https://github.com/flutter/website/issues/5063 --- .../lib/src/widgets/platform_view.dart | 309 ++++++++++++++++-- 1 file changed, 278 insertions(+), 31 deletions(-) diff --git a/packages/flutter/lib/src/widgets/platform_view.dart b/packages/flutter/lib/src/widgets/platform_view.dart index 2ae217ebc9d..ae24e58757f 100644 --- a/packages/flutter/lib/src/widgets/platform_view.dart +++ b/packages/flutter/lib/src/widgets/platform_view.dart @@ -19,6 +19,8 @@ import 'framework.dart'; // PlatformViewController createFooWebView(PlatformViewCreationParams params) { return (null as dynamic) as PlatformViewController; } // Set> gestureRecognizers = >{}; // late PlatformViewController _controller; +// void myOnElementCreated(Object element) {} +// void myOnPlatformViewCreated(int viewId) {} /// Embeds an Android view in the Widget hierarchy. /// @@ -358,49 +360,291 @@ class AppKitView extends _DarwinView { State createState() => _AppKitViewState(); } -/// Callback signature for when the platform view's DOM element was created. +/// The signature of the function that gets called when the [HtmlElementView] +/// DOM element is created. /// /// [element] is the DOM element that was created. /// -/// Also see [HtmlElementView.fromTagName] that uses this callback -/// signature. +/// This callback is called before [element] is attached to the DOM, so it can +/// be modified as needed by the Flutter web application. +/// +/// See [HtmlElementView.fromTagName] that receives a callback of this type. +/// +/// {@template flutter.widgets.web.JSInterop.object} +/// Flutter uses type `Object` so this API doesn't force any JS interop API +/// implementation to Flutter users. This `element` can be cast to any compatible +/// JS interop type as needed. For example: `JSAny` (from `dart:js_interop`), +/// `HTMLElement` (from `package:web`) or any custom JS interop definition. +/// See "Next-generation JS interop": https://dart.dev/interop/js-interop +/// {@endtemplate} typedef ElementCreatedCallback = void Function(Object element); -/// Embeds an HTML element in the Widget hierarchy in Flutter Web. +/// Embeds an HTML element in the Widget hierarchy in Flutter web. /// -/// *NOTE*: This only works in Flutter Web. To embed web content on other -/// platforms, consider using the `flutter_webview` plugin. -/// -/// Embedding HTML is an expensive operation and should be avoided when a -/// Flutter equivalent is possible. -/// -/// The embedded HTML is painted just like any other Flutter widget and -/// transformations apply to it as well. This widget should only be used in -/// Flutter Web. +/// The embedded HTML is laid out like any other Flutter widget and +/// transformations (like opacity, and clipping) apply to it as well. /// /// {@macro flutter.widgets.AndroidView.layout} /// -/// Due to security restrictions with cross-origin `