Flutter1.12与原生Android交互(kotlin)

开发原生部分,还是点击Open for Editing in Android Studio好用,提示、自动引用功能都能正常使用。

 

-----------android/app/src/main/kotlin/com/glodon/gzzjy_app/MainActivity.kt

 

import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger,"app.channel.shared.data").setMethodCallHandler(
            MethodChannel.MethodCallHandler { call, result ->
                run {
                    if (call.method.contentEquals("getParam")) {
                        result.success("message")
                        Log.i("test","ms")
                    }
                }
            }
        )
    }

}

 

注:这个flutterEngine.dartExecutor.binaryMessenger找了好久,各种文档上都是写的继承于io.flutter.app.FlutterActivity的MainActivity,直接getFlutterView()就行了,但是1.12这个版本的不行了。

------------------lib/main.dart



class _MyHomePageState extends State<MyHomePage> {
  static const platform = const MethodChannel('app.channel.shared.data');
  String dataShared = "No data";

  @override
  void initState() {
    super.initState();
    getSharedText();
  }

  getSharedText() async {
    var sharedData = await platform.invokeMethod("getParam");
    if (sharedData != null) {
      setState(() {
        dataShared = sharedData;
      });
      print(dataShared);
    }
  }

相关推荐
<p>本套课程适用于有一定的<span style="color: #e03e2d;">iOS、AndroidFlutter</span>开发基础。</p> <p>学完本次课程,能够让大家对Flutter如何调用移动端原生页面有一个清晰的认识;在纯Flutter开发过程中遇到需要调用原生功能的时候,能够快速定制属于自己或者公司的私有插件- Plugin。</p> <p>课程一共氛围两部分:</p> <p>1、Flutter插件跟iOS的交互部分:包括调用iOS原生页面、如何使用iOS的<span style="color: #e03e2d;">framework二进制</span>、<span style="color: #e03e2d;">bundle资源文件</span>、依赖的cocoapods资源;</p> <p>2、Flutter插件跟安卓的交互部分:包括调用Android原生页面、如何接收原生页面的回调、如何使用aar文件、依赖的其他资源。</p> <p>最终能够帮助大家定制私有插件;提升工作技能。</p> <p><span style="color: #e03e2d;">备注:课程中使用环境</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9);"><span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d;">[✓]</span><span style="font-variant-ligatures: no-common-ligatures;"> Flutter (Channel stable, 1.22.5, on macOS 11.0.1 20B29 darwin-arm, locale zh-Hans-CN)</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9); min-height: 14px;"><span style="font-variant-ligatures: no-common-ligatures;"> </span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9);"><span style="font-variant-ligatures: no-common-ligatures; color: #9fa01c;">[!]</span><span style="font-variant-ligatures: no-common-ligatures;"> Android toolchain - develop for Android devices (Android SDK version 30.0.3)</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #00ff00; background-color: rgba(0, 0, 0, 0.9);"><span style="font-variant-ligatures: no-common-ligatures;">    </span><span style="font-variant-ligatures: no-common-ligatures; color: #9fa01c;">!</span><span style="font-variant-ligatures: no-common-ligatures;"> Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9);"><span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d;">[✓]</span><span style="font-variant-ligatures: no-common-ligatures;"> Xcode - develop for iOS and macOS (Xcode 12.2)</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9);"><span style="font-variant-ligatures: no-common-ligatures; color: #9fa01c;">[!]</span><span style="font-variant-ligatures: no-common-ligatures;"> Android Studio (version 4.1)</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9);"><span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d;">[✓]</span><span style="font-variant-ligatures: no-common-ligatures;"> IntelliJ IDEA Community Edition (version 2020.3)</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9);"><span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d;">[✓]</span><span style="font-variant-ligatures: no-common-ligatures;"> Connected device (1 available)</span></p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9); min-height: 14px;"> </p> <p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: 'Andale Mono'; color: #2fff12; background-color: rgba(0, 0, 0, 0.9);"> </p>
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页