您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何在Flutter中調用 Android中的Native方法,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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方法,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。