initAppKitView static method

Future<AppKitViewController> initAppKitView({
  1. required int id,
  2. required String viewType,
  3. required TextDirection layoutDirection,
  4. dynamic creationParams,
  5. MessageCodec? creationParamsCodec,
  6. VoidCallback? onFocus,
})

Factory method to create an AppKitView.

The id parameter is an unused unique identifier generated with platformViewsRegistry.

The viewType parameter is the identifier of the iOS view type to be created, a factory for this view type must have been registered on the platform side. Platform view factories are typically registered by plugin code.

The onFocus parameter is a callback that will be invoked when the UIKit view asks to get the input focus. If creationParams is non null then creationParamsCodec must not be null.

Implementation

static Future<AppKitViewController> initAppKitView({
  required int id,
  required String viewType,
  required TextDirection layoutDirection,
  dynamic creationParams,
  MessageCodec<dynamic>? creationParamsCodec,
  VoidCallback? onFocus,
}) async {
  assert(creationParams == null || creationParamsCodec != null);

  // TODO(amirh): pass layoutDirection once the system channel supports it.
  // https://github.com/flutter/flutter/issues/133682
  final Map<String, dynamic> args = <String, dynamic>{
    'id': id,
    'viewType': viewType,
  };
  if (creationParams != null) {
    final ByteData paramsByteData = creationParamsCodec!.encodeMessage(creationParams)!;
    args['params'] = Uint8List.view(
      paramsByteData.buffer,
      0,
      paramsByteData.lengthInBytes,
    );
  }
  await SystemChannels.platform_views.invokeMethod<void>('create', args);
  if (onFocus != null) {
    _instance._focusCallbacks[id] = onFocus;
  }
  return AppKitViewController._(id, layoutDirection);
}