From 376584289cc3b3b5a40687fc579cb2530945e3c0 Mon Sep 17 00:00:00 2001 From: Arthas <15215604969@163.com> Date: Wed, 21 Jul 2021 05:29:04 +0800 Subject: [PATCH] Add onPlatformViewCreated to HtmlElementView (#84095) --- .../lib/src/widgets/platform_view.dart | 11 +++++- .../test/widgets/html_element_view_test.dart | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/platform_view.dart b/packages/flutter/lib/src/widgets/platform_view.dart index d2184f3672..18d2988a67 100644 --- a/packages/flutter/lib/src/widgets/platform_view.dart +++ b/packages/flutter/lib/src/widgets/platform_view.dart @@ -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; } } diff --git a/packages/flutter/test/widgets/html_element_view_test.dart b/packages/flutter/test/widgets/html_element_view_test.dart index 198e775513..e07dc2785f 100644 --- a/packages/flutter/test/widgets/html_element_view_test.dart +++ b/packages/flutter/test/widgets/html_element_view_test.dart @@ -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(currentViewId + 1, 'webview'), + ]), + ); + }); + testWidgets('Resize HTML view', (WidgetTester tester) async { final int currentViewId = platformViewsRegistry.getNextPlatformViewId(); final FakeHtmlPlatformViewsController viewsController = FakeHtmlPlatformViewsController();