如何使用 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;
},
);
}
}
Flutter 和原生(Android/iOS)可以通过平台通道相互通信 - 据我记得(6 个月前),它称为
MethodChannel
。
_methodChannel.invokeMethod('key_1', some_data_here);
NativeView.kt
监听方法通道(iOS同上):override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { when (call.method) { "key_1" -> { doSomething1() } "key_2" -> { doSomething2() } else -> {} } }
NativeView.kt
回调Flutter:methodChannel.invokeMethod("another_key_native", data_call_back)
initPlatformChannel(int id) { _methodChannel = MethodChannel('${_kViewType}_$id'); _methodChannel!.setMethodCallHandler((call) { switch (call.method) { case 'another_key_native_1': case 'another_key_native_2': ... } }