Java custom view in flutter.

huangapple go评论68阅读模式
英文:

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&lt;void&gt; initSupport({
    String? url,
    String? appId,
    String? clientId,
    String? id,
  }) async {
    await _channel.invokeMethod&lt;void&gt;(&#39;initSupport&#39;, {
      &#39;url&#39;: url,
      &#39;appId&#39;: appId,
      &#39;clientId&#39;: clientId,
      &#39;id&#39;: 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 = &quot;channelname&quot;

  override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {

    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
        call, result -&gt;
      if(call.method == &quot;initSupport&quot;){
        initSupport(call)
        result.success(true)
      } 
    }
  }

Step 3: method of init is

   fun initSupport(call: MethodCall){
       val url = call.argument&lt;String&gt;(&quot;url&quot;) ?: &quot;&quot;  // here is your dart data
      val appId = call.argument&lt;String&gt;(&quot;appId&quot;) ?: &quot;&quot; 
      val clientId = call.argument&lt;String&gt;(&quot;clientId&quot;) ?: &quot;&quot;
      val id = call.argument&lt;String&gt;(&quot;id&quot;) ?: &quot;1&quot;

     // You can init your view here like below 
   Toast.makeText(this,&quot;hello from native&quot;, Toast.LENGTH_SHORT).show() 

     }

huangapple
  • 本文由 发表于 2023年2月16日 12:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定