Flutter插件(Plugin)开发 - Android视角,Android进阶之光
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
class FlutterPluginExample: FlutterPlugin, MethodChannel.MethodCallHandler {
private lateinit var channel : MethodChannel
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_plugin_example")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"getPlatformVersion" -> result.success("Android ${android.os.Build.VERSION.RELEASE}")
else -> result.notImplemented()
}
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
这段代码展示了如何创建一个简单的Flutter插件,它提供了一个方法getPlatformVersion
来获取Android设备的系统版本号。这是一个基本的插件开发示例,展示了如何处理方法调用并返回结果。在实际开发中,插件可以提供更多复杂的功能。
评论已关闭