Add onPlatformViewCreated to HtmlElementView (#84095)

This commit is contained in:
Arthas
2021-07-21 05:29:04 +08:00
committed by GitHub
parent 89c8616f0e
commit 376584289c
2 changed files with 44 additions and 1 deletions

View File

@@ -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;
}
}

View File

@@ -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();