Unverified Commit 37658428 authored by Arthas's avatar Arthas Committed by GitHub

Add onPlatformViewCreated to HtmlElementView (#84095)

parent 89c8616f
......@@ -345,6 +345,7 @@ class HtmlElementView extends StatelessWidget {
const HtmlElementView({
Key? key,
required this.viewType,
this.onPlatformViewCreated,
}) : assert(viewType != null),
assert(kIsWeb, 'HtmlElementView is only available on Flutter Web.'),
super(key: key);
......@@ -354,6 +355,11 @@ class HtmlElementView extends StatelessWidget {
/// A PlatformViewFactory for this type must have been registered.
final String viewType;
/// Callback to invoke after the platform view has been created.
///
/// May be null.
final PlatformViewCreatedCallback? onPlatformViewCreated;
@override
Widget build(BuildContext context) {
return PlatformViewLink(
......@@ -372,7 +378,10 @@ class HtmlElementView extends StatelessWidget {
/// Creates the controller and kicks off its initialization.
_HtmlElementViewController _createHtmlElementView(PlatformViewCreationParams params) {
final _HtmlElementViewController controller = _HtmlElementViewController(params.id, viewType);
controller._initialize().then((_) { params.onPlatformViewCreated(params.id); });
controller._initialize().then((_) {
params.onPlatformViewCreated(params.id);
onPlatformViewCreated?.call(params.id);
});
return controller;
}
}
......
......@@ -37,6 +37,40 @@ void main() {
);
});
testWidgets('Create HTML view with PlatformViewCreatedCallback', (WidgetTester tester) async {
final int currentViewId = platformViewsRegistry.getNextPlatformViewId();
final FakeHtmlPlatformViewsController viewsController = FakeHtmlPlatformViewsController();
viewsController.registerViewType('webview');
bool hasPlatformViewCreated = false;
void onPlatformViewCreatedCallBack(int id) {
hasPlatformViewCreated = true;
}
await tester.pumpWidget(
Center(
child: SizedBox(
width: 200.0,
height: 100.0,
child: HtmlElementView(
viewType: 'webview',
onPlatformViewCreated: onPlatformViewCreatedCallBack,
),
),
),
);
// Check the onPlatformViewCreatedCallBack has been called.
expect(hasPlatformViewCreated, true);
expect(
viewsController.views,
unorderedEquals(<FakeHtmlPlatformView>[
FakeHtmlPlatformView(currentViewId + 1, 'webview'),
]),
);
});
testWidgets('Resize HTML view', (WidgetTester tester) async {
final int currentViewId = platformViewsRegistry.getNextPlatformViewId();
final FakeHtmlPlatformViewsController viewsController = FakeHtmlPlatformViewsController();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment