website icon indicating copy to clipboard operation
website copied to clipboard

[PAGE ISSUE]: 'Hosting native Android views in your Flutter app with Platform Views'

Open DavidPerezP124 opened this issue 3 years ago • 1 comments

Page URL

https://docs.flutter.dev/development/platform-integration/android/platform-views/

Page source

https://github.com/flutter/website/tree/main/src/development/platform-integration/android/platform-views.md

Describe the problem

Overriden method on NativeFactoryView should have the method change from 3.* . Possibly related to this https://github.com/flutter/engine/pull/31530.

package dev.flutter.example

import android.content.Context
import android.view.View
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class NativeViewFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
    override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
        val creationParams = args as Map<String?, Any?>?
        return NativeView(context, viewId, creationParams)
    }
}

It should be something similar to this:

package dev.flutter.example

import android.content.Context
import android.view.View
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class NativeViewFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
    override fun create(p0: Context?, p1: Int, p2: Any?): PlatformView {
        val creationParams = p2 as Map<String?, Any?>?
        return NativeView(context, p1, creationParams)
    }
}

And same for the NativeView snippet, it should have an updated initializer for a nullable Context

package dev.flutter.example

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import io.flutter.plugin.platform.PlatformView

internal class NativeView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView {
    private val textView: TextView

    override fun getView(): View {
        return textView
    }

    override fun dispose() {}

    init {
        textView = TextView(context)
        textView.textSize = 72f
        textView.setBackgroundColor(Color.rgb(255, 255, 255))
        textView.text = "Rendered on a native Android view (id: $id)"
    }
}

Should be:

package dev.flutter.example

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import io.flutter.plugin.platform.PlatformView

internal class NativeView(context: Context?, id: Int, creationParams: Map<String?, Any?>?) : PlatformView {
    private val textView: TextView

    override fun getView(): View {
        return textView
    }

    override fun dispose() {}

    init {
        textView = TextView(context)
        textView.textSize = 72f
        textView.setBackgroundColor(Color.rgb(255, 255, 255))
        textView.text = "Rendered on a native Android view (id: $id)"
    }
}

Expected fix

No response

Additional context

No response

DavidPerezP124 avatar Aug 15 '22 21:08 DavidPerezP124

/cc @blasten for more context on this

danagbemava-nc avatar Aug 16 '22 12:08 danagbemava-nc