I'm doing the good old native view hosting (codes at the end, which is basically copied from Flutter website) for a video chat app. I want to show video feed of each user in one of these.
For that I tried to pass different creationParams
, but it seems only one View
can exist at a time; no new "view creation" occur after the first one.
Do I have to register
each view in advance, passing different viewType
for each? Or is there a better way for having unknown number of NativeView
s?
Flutter
const String viewType = '<platform-view-type>';
final Map<String, dynamic> creationParams = <String, dynamic>{};
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return AndroidView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
case TargetPlatform.iOS:
return UiKitView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
default:
throw UnsupportedError('Unsupported platform view');
iOS
registrar.register(nativeViewFactory, withId: "<platform-view-type>")
Android
flutterPluginBinding.platformViewRegistry.registerViewFactory("<platform-view-type>", nativeViewFactory)
Edit: This is how I tried to create different views by passing different params:
ViewFactory (android)
class NativeViewFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
private lateinit var selfVideoView: NativeView
private val remoteVideoViews: MutableMap<String, NativeView> = HashMap()
fun getSelfVideo(): NativeView {
return selfVideoView
}
override fun create(context: Context?, viewId: Int, args: Any?): PlatformView {
val creationParams = args as Map<String?, Any?>?
return if (context != null) {
val videoView = NativeView(context, viewId, creationParams)
if (creationParams == null || creationParams.isEmpty()) {
selfVideoView = videoView
} else {
val username = creationParams["username"] as String
remoteVideoViews[username] = videoView
/// ...
/// Connect video feed of 'username' to videoView
/// ...
}
videoView
} else {
throw IllegalStateException("Context is null")
}
}
}