如何在Flutter中调用Android中的Native方法-创新互联
本篇文章给大家分享的是有关如何在Flutter中调用 Android中的Native方法,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联建站-专业网站定制、快速模板网站建设、高性价比徐闻网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式徐闻网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖徐闻地区。费用合理售后完善,10余年实体公司更值得信赖。Flutter 调用 Android Native 的方法,是通过MethodChannel的方式来实现的:
在Android端:
创建一个Class,实现FlutterPlugin和MethodCallHandler接口
重写onAttachedToEngine(),onDetachedFromEngine(),onMethodCall()
onAttachedToEngine()中,根据自定义的CHANNEL_NAME创建MethodChannel, onDetachedFromEngine中,释放MethodChannel
onMethodCall中,通过自定义的METHOD_NAME,来响应Flutter中,invokeMethod对Native的通信,代码如下
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("content") mNoteForFlutterListener?.sendData(content!!) result.success("success") } else { result.notImplemented() } } fun setListener(noteForFlutterListener: NoteForFlutterListener) { mNoteForFlutterListener = noteForFlutterListener }
在Flutter端:
根据Native中定义的METHOD_NAME来创建MethodChannel
通过MethodChannel.invokeMethod(METHOD_NAME,params),参数METHOD_NAME为Native中定义的METHOD_NAME,params为传递的参数,就可以和Native进行通信了,在Native的onMethodCall方法中,通过call.method == METHOD_NAME来确定,Flutter是否调用的是Native中定义的METHOD_NAME的方法,代码如下
class NoteMainFulState extends State{ //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 map = {"content": content}; var result = await _methodMessageChannel.invokeMethod("saveNote", map); print("result $result"); }
以上就是如何在Flutter中调用 Android中的Native方法,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
名称栏目:如何在Flutter中调用Android中的Native方法-创新互联
当前路径:http://hbruida.cn/article/psceo.html