Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Flutter 调用Android Native

详解Flutter 调用 Android Native 的方法

作者:zzzyplove

这篇文章主要介绍了详解Flutter 调用 Android Native 的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Flutter 调用 Android Native 的方法,是通过MethodChannel的方式来实现的:

在Android端:

class MethodChannelPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {

  private var methodChannel: MethodChannel? = null
  private var mNoteForFlutterListener: NoteForFlutterListener? = null

  companion object {
    private const val CHANNEL_NAME = "method_channel"
    private const val METHOD_NAME = "saveNote"
    val instance: MethodChannelPlugin by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
      MethodChannelPlugin()
    }
  }

  override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
    methodChannel = MethodChannel(binding.binaryMessenger, CHANNEL_NAME)
    methodChannel?.setMethodCallHandler(this)
  }

  override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
    methodChannel?.setMethodCallHandler(null)
    methodChannel = null
  }

  override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
    if (call.method == METHOD_NAME) {
      val content = call.argument<String>("content")
      mNoteForFlutterListener?.sendData(content!!)
      result.success("success")
    } else {
      result.notImplemented()
    }
  }

  fun setListener(noteForFlutterListener: NoteForFlutterListener) {
    mNoteForFlutterListener = noteForFlutterListener
  }

在Flutter端:

class NoteMainFulState extends State<NoteMainFul> {
 //flutter 和 native 通信
 static const _methodMessageChannel = MethodChannel("method_channel");
 TextField textField;
 TextEditingController textEditingController;

 @override
 Widget build(BuildContext context) {
  return Scaffold(
   body: Container(
    margin: EdgeInsets.fromLTRB(20.0, 60.0, 20.0, 20.0),
    child: Column(
     children: [
      Container(
       child: Column(
        children: [
         textField = TextField(
          //去掉下划线
          decoration: InputDecoration(border: InputBorder.none),
          enabled: true,
          controller: textEditingController = NoteTextEditingController(),
          keyboardType: TextInputType.multiline,
          textInputAction: TextInputAction.newline,
          maxLines: null,
         ),
         RichText(
          text: TextSpan(),
         ),
        ],
       ),
      ),
      Container(
       color: Colors.deepPurple,
       height: 50,
       child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
         GestureDetector(
          child: Icon(
           Icons.forward,
          ),
          onTap: clickOut,
         )
        ],
       ),
      )
     ],
    ),
   ),
  );
 }

 void clickOut() async {
  //调用native方法,返回首页
  var content = textEditingController.text;
  Map<String, dynamic> map = {"content": content};
  var result = await _methodMessageChannel.invokeMethod("saveNote", map);
  print("result $result");
 }

以上就是Flutter 调用 Android Native 的方法的过程,有时间再分析下他们的实现原理
待续....
附,我当前的Flutter版本是 :
Flutter 1.22.3 • channel stable • https://github.com/flutter/flutter
Framework • revision 8874f21e79 (3 months ago) • 2020-10-29 14:14:35 -0700
Engine • revision a1440ca392
Tools • Dart 2.10.3

 到此这篇关于详解Flutter 调用 Android Native 的方法的文章就介绍到这了,更多相关Flutter 调用Android Native内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文