flutter / website

Flutter documentation web site
https://docs.flutter.dev
Other
2.79k stars 3.2k forks source link

Update `NativeFactoryView` and `NativeView` examples on 'Hosting native Android views in your Flutter app with Platform Views' page #7454

Open DavidPerezP124 opened 2 years ago

DavidPerezP124 commented 2 years ago

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

danagbemava-nc commented 2 years ago

/cc @blasten for more context on this