Flutter MethodChannel 学习
好久没写东西,正好最近有时间又开始研究起来Flutter,不了解的童鞋可以去查查资料了解一下, 在我看来是目前我了解盗的最理想的跨平台解决方案了。而跨平台中一个比较重要的点就是与底层进行交互,本文就主要说一下在Flutter中是如何与Android进行通讯的(iOS应该同理)。不涉及到源码,只是简单的使用和在使用过程中的一些想法。
创建初始的Demo
使用 flutter create methodchannellearn 创建一个 flutter 项目。
导入 services,async 包
import 'dart:async'; import 'package:flutter/services.dart';
首先先在main.dart的 _MyHomePageState 中创建一个静态成员变量methodChannel
static const MethodChannel methodChannel=const MethodChannel("flutter_method_channel");
然后修改原有的_incrementCounter()方法,内部调用methodChannel.invokeMethod()方法,调用Native端的方法。
void _incrementCounter() {
// setState(() {
// // This call to setState tells the Flutter framework that something has
// // changed in this State, which causes it to rerun the build method below
// // so that the display can reflect the updated values. If we changed
// // _counter without calling setState(), then the build method would not be
// // called again, and so nothing would appear to happen.
// _counter++;
// });
methodChannel.invokeMethod("_incrementCounter", _counter++);
}
接下来就是在Android端添加响应的代码
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), "flutter_method_channel").setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("_incrementCounter")) {
toast(methodCall.arguments);
}
}
});
}
跑一下之后可以看到正常的toast弹出。
添加回调
在onMethodCall中我们可以看到还有另外的一个参数result,可以使用它将数据传递回Flutter中。
修改Android端代码
if (methodCall.method.equals("_incrementCounter")) {
if(methodCall.arguments instanceof Integer){
int count= (int) methodCall.arguments;
count++;
result.success(count);
}
}
同时修改Flutter中的代码
Future<Null> _incrementCounter() async {
// setState(() {
// // This call to setState tells the Flutter framework that something has
// // changed in this State, which causes it to rerun the build method below
// // so that the display can reflect the updated values. If we changed
// // _counter without calling setState(), then the build method would not be
// // called again, and so nothing would appear to happen.
// _counter++;
// });
var result =
await methodChannel.invokeMethod("_incrementCounter", _counter);
setState(() {
_counter = result;
});
}
运行一下,点击次数正常增加。
主动通知Flutter
有时候我们可能会需要主动的通知flutter ,而不是等待flutter调用,利用回调传递数据,这是就需要一个新的Channel, EventChannel
同样还是刚才的例子,这会先修改flutter代码,使用EventChannel
static const EventChannel eventChannel =
const EventChannel("flutter_event_channel");
void _onData(event) {
setState(() {
_counter = event;
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
eventChannel.receiveBroadcastStream().listen(_onData);
}
然后修改Android的代码
new MethodChannel(getFlutterView(), "flutter_method_channel").setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("_incrementCounter")) {
if (methodCall.arguments instanceof Integer) {
int count = (int) methodCall.arguments;
count++;
if (eventSink != null) {
eventSink.success(count);
}
}
}
}
});
new EventChannel(getFlutterView(), "flutter_event_channel").setStreamHandler(
new EventChannel.StreamHandler() {
@Override public void onListen(Object o, EventChannel.EventSink eventSink) {
MainActivity.this.eventSink = eventSink;
}
@Override public void onCancel(Object o) {
MainActivity.this.eventSink = null;
}
});
同样 运行,数据也能够正常显示
这里EventChannel的使用场景应该会有好多,毕竟一个持久长时间的消息通道在实际场景中会有很多应用。
项目的代码地址在这里