Flutter:更改 textField 文本 - 由 PlatformVIew 在 Android 和 IOS 中创建

问题描述 投票:0回答:1

如何使用 FLutter PlatformView(在本机中)在 Android 和 IOS 中创建 TextView 并通过单击提升按钮更改本机中 TextView 的文本?

我已经为 Android 创建了 textview 示例,但我不知道如何更改 TextField 的值(文本) ps:它已经可以使用初始值,但我不知道如何更新文本 我的代码

class PlatformWidget extends StatelessWidget {
 final String text;
 const PlatformWidget({super.key, required this.text});

 @override
 Widget build(BuildContext context) {
   return PlatformViewLink(
     viewType: 'INTEGRATION_ANDROID',
     surfaceFactory: (
       BuildContext context,
       PlatformViewController controller,
     ) {
       return AndroidViewSurface(
         controller: controller as AndroidViewController,
         gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
         hitTestBehavior: PlatformViewHitTestBehavior.opaque,
       );
     },
     onCreatePlatformView: (PlatformViewCreationParams params) {
       final ExpensiveAndroidViewController controller =
           PlatformViewsService.initExpensiveAndroidView(
         id: params.id,
         viewType: params.viewType,
         layoutDirection: TextDirection.ltr,
         creationParams: {'initialText': text},
         creationParamsCodec: const StandardMessageCodec(),
       );
       controller
         ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
         ..create();

       return controller;
     },
   );
 }
}
swift flutter kotlin native platformview
1个回答
0
投票

Flutter 和原生(Android/iOS)可以通过平台通道相互通信 - 据我记得(6 个月前),它称为

MethodChannel

  1. Flutter 调用原生:

_methodChannel.invokeMethod('key_1', some_data_here);

  1. Android原生
    NativeView.kt
    监听方法通道(iOS同上):
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
    when (call.method) {
        "key_1" -> {
            doSomething1()
        }
        "key_2" -> {
            doSomething2()
        }
        else -> {}
    }
}
  1. Android原生
    NativeView.kt
    回调Flutter:

methodChannel.invokeMethod("another_key_native", data_call_back)

  1. Flutter监听方法通道:
initPlatformChannel(int id) {
    _methodChannel = MethodChannel('${_kViewType}_$id');
    _methodChannel!.setMethodCallHandler((call) {
      switch (call.method) {
        case 'another_key_native_1':
        case 'another_key_native_2':
        ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.