英文:
java custom view in flutter
问题
我有一个自定义视图,用Java编写,并希望在我的Flutter项目中使用它。是否可以将它转换为Dart本地代码,或者在Flutter中将它用作Java类?
自定义视图非常复杂,而且我在Dart语言方面经验有限,所以将它转换为Dart或者直接使用它会非常方便。
英文:
I have a custom view, written in Java and want to use it in my flutter project. Is it possible to convert it to dart-native code or use it as a java class in flutter?
Custom view is quite complex and I am not very experienced in dart language, so would be great to have it either converted to dart or use it as it is
答案1
得分: 0
以下是翻译好的部分:
Step 1 : 您需要将方法通道写入Dart代码中:
static Future<void> initSupport({
String? url,
String? appId,
String? clientId,
String? id,
}) async {
await _channel.invokeMethod<void>('initSupport', {
'url': url,
'appId': appId,
'clientId': clientId,
'id': id,
});
}
您需要将此方法通道写入您想要打开Java视图的视图中,然后打开Android Studio项目。
Step 2: 检查以下代码以从Dart获取方法通道:
class MainActivity : FlutterFragmentActivity() {
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
}
private val CHANNEL = "channelname"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if (call.method == "initSupport") {
initSupport(call)
result.success(true)
}
}
}
}
Step 3: 初始化方法如下:
fun initSupport(call: MethodCall){
val url = call.argument<String>("url") ?: "" // 这里是您的Dart数据
val appId = call.argument<String>("appId") ?: ""
val clientId = call.argument<String>("clientId") ?: ""
val id = call.argument<String>("id") ?: "1"
// 您可以在此处初始化视图,如下所示
Toast.makeText(this, "hello from native", Toast.LENGTH_SHORT).show()
}
英文:
Step 1 : You have to write method channel into dart code:
static Future<void> initSupport({
String? url,
String? appId,
String? clientId,
String? id,
}) async {
await _channel.invokeMethod<void>('initSupport', {
'url': url,
'appId': appId,
'clientId': clientId,
'id': id,
});
}
You have to write into your view where you want to open java view to init this method channel
After this, you have to open your project in android studio
Step 2: Check the below code to get method channel from dart
class MainActivity: FlutterFragmentActivity() {
override fun onNewIntent(intent : Intent){
super.onNewIntent(intent)
setIntent(intent)
}
private val CHANNEL = "channelname"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result ->
if(call.method == "initSupport"){
initSupport(call)
result.success(true)
}
}
}
Step 3: method of init is
fun initSupport(call: MethodCall){
val url = call.argument<String>("url") ?: "" // here is your dart data
val appId = call.argument<String>("appId") ?: ""
val clientId = call.argument<String>("clientId") ?: ""
val id = call.argument<String>("id") ?: "1"
// You can init your view here like below
Toast.makeText(this,"hello from native", Toast.LENGTH_SHORT).show()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论