|
| 1 | +package com.example.tform |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | + |
| 5 | +import io.flutter.embedding.engine.plugins.FlutterPlugin |
| 6 | +import io.flutter.plugin.common.MethodCall |
| 7 | +import io.flutter.plugin.common.MethodChannel |
| 8 | +import io.flutter.plugin.common.MethodChannel.MethodCallHandler |
| 9 | +import io.flutter.plugin.common.MethodChannel.Result |
| 10 | +import io.flutter.plugin.common.PluginRegistry.Registrar |
| 11 | + |
| 12 | +/** TformPlugin */ |
| 13 | +public class TformPlugin: FlutterPlugin, MethodCallHandler { |
| 14 | + /// The MethodChannel that will the communication between Flutter and native Android |
| 15 | + /// |
| 16 | + /// This local reference serves to register the plugin with the Flutter Engine and unregister it |
| 17 | + /// when the Flutter Engine is detached from the Activity |
| 18 | + private lateinit var channel : MethodChannel |
| 19 | + |
| 20 | + override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
| 21 | + channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "tform") |
| 22 | + channel.setMethodCallHandler(this); |
| 23 | + } |
| 24 | + |
| 25 | + // This static function is optional and equivalent to onAttachedToEngine. It supports the old |
| 26 | + // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting |
| 27 | + // plugin registration via this function while apps migrate to use the new Android APIs |
| 28 | + // post-flutter-1.12 via https://flutter.dev/go/android-project-migration. |
| 29 | + // |
| 30 | + // It is encouraged to share logic between onAttachedToEngine and registerWith to keep |
| 31 | + // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called |
| 32 | + // depending on the user's project. onAttachedToEngine or registerWith must both be defined |
| 33 | + // in the same class. |
| 34 | + companion object { |
| 35 | + @JvmStatic |
| 36 | + fun registerWith(registrar: Registrar) { |
| 37 | + val channel = MethodChannel(registrar.messenger(), "tform") |
| 38 | + channel.setMethodCallHandler(TformPlugin()) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { |
| 43 | + if (call.method == "getPlatformVersion") { |
| 44 | + result.success("Android ${android.os.Build.VERSION.RELEASE}") |
| 45 | + } else { |
| 46 | + result.notImplemented() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { |
| 51 | + channel.setMethodCallHandler(null) |
| 52 | + } |
| 53 | +} |
0 commit comments