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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值