Flutter中PlatformView在鸿蒙中的使用
概述
集成平台视图(后称为平台视图)允许将原生视图嵌入到 Flutter 应用中,所以你可以通过 Dart 将变换、裁剪和不透明度等效果应用到原生视图。
例如,这使你可以通过使用平台视图直接在 Flutter 应用内部使用鸿蒙中的原生地图。
在Flutter中的处理
首先我们需要再Flutter中创建一个视图,用来加载Ohos平台的view。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
typedef OnViewCreated = Function(CustomViewController);
class CustomOhosView extends StatefulWidget {
final OnViewCreated onViewCreated;
const CustomOhosView(this.onViewCreated, {
super.key});
State<CustomOhosView> createState() => _CustomOhosViewState();
}
class _CustomOhosViewState extends State<CustomOhosView> {
late MethodChannel _channel;
Widget build(BuildContext context) {
return _getPlatformFaceView();
}
Widget _getPlatformFaceView() {
// 加载platformview
/**
* viewType:传递给Native侧,告知插件需要创建那个PlatformView,这个PlatformView需要在插件初始化时注册。
onPlatformViewCreated:PlatformView创建成功时的回调。
creationParams:传递给PlatformView的初始化参数。
*/
return OhosView(
viewType: 'com.rex.custom.ohos/customView',
onPlatformViewCreated: _onPlatformViewCreated,
creationParams: const <String, dynamic>{
'initParams': 'hello world'},
creationParamsCodec: const StandardMessageCodec(),
);
}
void _onPlatformViewCreated(int id) {
print("platformview==================创建成功");
_channel = MethodChannel('com.rex.custom.ohos/customView$id');
final controller = CustomViewController._(
_channel,
);
widget.onViewCreated(controller);
}
}
// 用于实现Dart侧与Native侧的交互
class CustomViewC

最低0.47元/天 解锁文章
892

被折叠的 条评论
为什么被折叠?



