Add onPlatformViewCreated to HtmlElementView (#84095)
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user