Flutter 我想在插件中执行这个方法,我应该怎么做
我正在做一个外部物理键盘输入功能,扫描代码枪以获取数据,执行此方法,然后从插件返回到颤振应用程序 现在我有一个问题,如何在插件中重写这个方法,它目前是无效的Flutter 我想在插件中执行这个方法,我应该怎么做,flutter,Flutter,我正在做一个外部物理键盘输入功能,扫描代码枪以获取数据,执行此方法,然后从插件返回到颤振应用程序 现在我有一个问题,如何在插件中重写这个方法,它目前是无效的 颤振提供了一种使用MethodChannel与本机平台代码通信的方法 从颤振MethodChannel文档(): Flatter使用灵活的系统,允许您调用特定于平台的API,无论是Android上的Kotlin或Java代码,还是iOS上的Swift或Objective-C代码 注意:如果需要,方法调用也可以反向发送,平台充当Dart中实现
颤振提供了一种使用
MethodChannel
与本机平台代码通信的方法
从颤振MethodChannel
文档():
Flatter使用灵活的系统,允许您调用特定于平台的API,无论是Android上的Kotlin或Java代码,还是iOS上的Swift或Objective-C代码
注意:如果需要,方法调用也可以反向发送,平台充当Dart中实现的方法的客户端。一个具体的例子是[quick_actions][2]插件
MethodChannel
从flatter调用本机平台代码上的函数的用法:
static final channelName = 'com.example.widget/keyEvent';
final methodChannel = MethodChannel(channelName);
await this.methodChannel.invokeMethod("dispatchKeyEvent");
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.widget/keyEvent";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("dispatchKeyEvent")) {
//TODO: Call the dispatch key event function here
bool resultValue;
if (data != null) {
result.success(data);
} else {
result.error("UNAVAILABLE", "Result not available", null);
}
} else {
result.notImplemented();
}
}
);
}
}
我们在flatter、iOS和Android中创建一个同名的方法通道对象。Android和iOS对象将设置一个方法调用处理程序来接收来自Flatter的调用。然后,flatter代码可以调用invokeMethod来调用本机对象上的处理程序
颤振:
static final channelName = 'com.example.widget/keyEvent';
final methodChannel = MethodChannel(channelName);
await this.methodChannel.invokeMethod("dispatchKeyEvent");
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.widget/keyEvent";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("dispatchKeyEvent")) {
//TODO: Call the dispatch key event function here
bool resultValue;
if (data != null) {
result.success(data);
} else {
result.error("UNAVAILABLE", "Result not available", null);
}
} else {
result.notImplemented();
}
}
);
}
}
Android:
static final channelName = 'com.example.widget/keyEvent';
final methodChannel = MethodChannel(channelName);
await this.methodChannel.invokeMethod("dispatchKeyEvent");
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.widget/keyEvent";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("dispatchKeyEvent")) {
//TODO: Call the dispatch key event function here
bool resultValue;
if (data != null) {
result.success(data);
} else {
result.error("UNAVAILABLE", "Result not available", null);
}
} else {
result.notImplemented();
}
}
);
}
}
感谢您的评论,我想实现的功能是覆盖插件中的dispatchKeyEvent方法。请添加一个复制您的问题的示例!