详解Flutter 调用 Android Native 的方法
程序员文章站
2022-03-26 18:41:18
flutter 调用 android native 的方法,是通过methodchannel的方式来实现的:在android端: 创建一个class,实现flutterplugin和methodca...
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<string>("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<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 •
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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!