Explain the method of Flutter calling Android Native in detail
- 2021-12-11 08:55:54
- OfStack
Flutter calls the method of Android Native, which is implemented by MethodChannel:
On the Android side:
Create an Class to implement FlutterPlugin and MethodCallHandler interfaces Override onAttachedToEngine (), onDetachedFromEngine (), onMethodCall () In onAttachedToEngine (), MethodChannel is created according to the custom CHANNEL_NAME, and in onDetachedFromEngine, MethodChannel is released In onMethodCall, the user-defined METHOD_NAME is used to respond to the communication between invokeMethod and Native in Flutter. The code is as follows
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
}
On the Flutter side:
Create MethodChannel from METHOD_NAME defined in Native Through MethodChannel.invokeMethod (METHOD_NAME, params), the parameter METHOD_NAME is METHOD_NAME defined in Native, and params is a passed parameter, you can communicate with Native. In onMethodCall method of Native, it is determined by call.method = = METHOD_NAME whether Flutter calls METHOD_NAME method defined in Native. The code is as follows
class NoteMainFulState extends State<NoteMainFul> {
//flutter And native Communication
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(
// Remove the underline
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 {
// Call native Method to return to the home page
var content = textEditingController.text;
Map<String, dynamic> map = {"content": content};
var result = await _methodMessageChannel.invokeMethod("saveNote", map);
print("result $result");
}
The above is the process of Flutter calling Android Native method. Have time to analyze their implementation principle again
To be continued....
Attached, my current version of Flutter is:
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