hadss_knock_share 1.0.0-rc.0 copy "hadss_knock_share: ^1.0.0-rc.0" to clipboard
hadss_knock_share: ^1.0.0-rc.0 copied to clipboard

A plug-in library for share.

hadss_knock_share #

介绍 #

为 Flutter 提供“碰一碰”分享能力,当前支持手机与手机之间的碰一碰分享。该库封装了鸿蒙原生能力,开发者可通过简洁接口在Flutter中实现原生的碰一碰分享功能。

提供鸿蒙碰一碰分享能力flutter插件库,目前库提供的api能力如下:

  1. 碰一碰分享能力的检测
  2. 设置碰一碰分享数据
  3. 开启碰一碰能力
  4. 关闭碰一碰能力
[./screenshots/ImageShare.gif] [./screenshots/ImmersiveCards.gif] [./screenshots/OneHopEngineCheck.gif] [./screenshots/VerticalLayout.gif]

API #

KnockShare #

碰一碰分享

方法 类型 说明
canUseKnockShare canUseKnockShare(): bool 判断当前设备是否支持碰一碰。
onKnockShare onKnockShare(): void 注册华为分享事件监听。
offKnockShare offKnockShare(): void 取消华为分享事件监听。
setKnockShareData setKnockShareData(SharedData data, bool? isAutoClean): void 设置分享数据。isAutoClean: 设置分享完成后是否自动清空,默认是false。

SharedData #

表示分享数据对象,提供封装一组数据记录的方法。

方法 类型 说明
constructor constructor(record: SharedRecord) 表示分享数据对象,提供封装一组数据记录的方法。一个分享数据对象至少存在一条记录,开发者需要在SharedData实例化过程中,通过构造器第一个参数传入;当分享数据包含多条数据记录时,则需要使用addRecord(record: SharedRecord)方法追加记录。
addRecord addRecord(record: SharedRecord): void 在当前分享数据中添加一条分享数据记录。
getRecords getRecords(): List 获取当前分享数据中的所有数据记录。

SharedRecord属性 #

用于构造一条分享数据记录。包含数据类型、数据内容及描述等信息。

名称 类型 可选 说明
utd String 统一数据类型,参考@ohos.data.uniformTypeDescriptor (标准化数据定义与描述)建议开发者传入精准的数据类型,有助于匹配到精确的目标应用
title String 如果是文本、链接等内容,建议填入title标识其标题。
label String 标识当前数据记录类型的标签,在单选模式时生效。缺省为UniformDataType类型相应的标签。具体如下:HYPERLINK显示为链接;HTML显示为网页;TEXT显示为文本;VIDEO显示为视频;AUDIO显示为音乐;IMAGE显示为图片;FILE显示为文件;
description String 数据记录的描述。
thumbnail dynamic 数据记录缩略图。 建议开发者传入符合数据记录的缩略图,如无,可传入应用图标。 说明 限制图片大小:32KB以下。过大的图片可能导致want数据超限无法拉起分享。
thumbnailUri String 数据记录缩略图。仅支持沙箱uri。 说明 起始版本:5.0.0(12)。 与thumbnail字段同时存在时,优先使用thumbnail字段。
uri String 数据记录的uri。仅支持沙箱文件uri,参见:用户文件uri介绍 说明 沙箱路径可通过fileUri.getUriFromPath方法获取文件URI。 content和uri二者至少有一个不为空。
content String 数据记录内容。链接(包含App Linking)、文本类型的内容通过该字段传递。 说明 content和uri二者至少有一个不为空。
extraData Map<String, dynamic> 扩展数据,用于向目标应用/设备分享自定义的扩展内容。

工程目录 #

knock_share
├─lib/src                                       // flutter代码实现
└─ohos/src/main/ets/components/plugin           // harmony代码实现

安装和使用 #

在生成的项目中添加hadss_knock_share库的依赖,在pubspec.yaml文件中的依赖项中添加

dependencies:  
  hadss_knock_share:
    path: ../  

然后执行以下命令,用于更新package依赖

flutter pub get

在项目目录中通过以下命令编译hap包

flutter build hap --release

命令执行完成后会生成hap包,并显示hap包路径

在hap的文件夹中如下命令使用hdc来安装应用

hdc install .\entry-default-signed.hap

示例代码 #

void main() {
  runApp(const MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool canUseKnockShareState = false;
  bool isOpenShare = false;
  final knockSharePlugin = KnockShare();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SizedBox(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const Padding(padding: EdgeInsets.only(top: 20)),
              Text(canUseKnockShareState ? "该设备支持碰一碰" : "请检测设备是否支持碰一碰"),
              const Padding(padding: EdgeInsets.only(top: 20)),
              TextButton(
                onPressed: () async {
                  bool canUseKnockShare = false;
                  canUseKnockShare =
                      await knockSharePlugin.canUseKnockShare() ?? false;
                  setState(() {
                    canUseKnockShareState = canUseKnockShare;
                  });
                },
                style: const ButtonStyle(
                  backgroundColor: MaterialStatePropertyAll(Colors.blue),
                ),
                child: const Text(
                  "canUseKnockShare",
                  style: TextStyle(color: Colors.white),
                ),
              ),
              const Padding(padding: EdgeInsets.only(top: 20)),
              Text(
                isOpenShare ? "监听中" : "未监听",
                style: const TextStyle(fontSize: 20),
              ),
              const Padding(padding: EdgeInsets.only(top: 20)),
              const Text("纯图片"),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TextButton(
                    onPressed: () {
                      if (isOpenShare) {
                        showAlert(context);
                      } else {
                        var shareBean = SharedRecord(UniformDataType.jpeg);
                        shareBean.thumbnailUri =
                            ShareConstants.exampleKnockUrl3;
                        shareBean.uri = ShareConstants.exampleKnockUrl3;
                        addKnockShareListener(shareBean);
                      }
                    },
                    style: const ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(Colors.blue),
                    ),
                    child: const Text(
                      ShareConstants.addKnockShareListener,
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  const Padding(padding: EdgeInsets.all(5)),
                  TextButton(
                    onPressed: () {
                      removeKnockShareListener();
                    },
                    style: const ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(Colors.blue),
                    ),
                    child: const Text(
                      ShareConstants.removeKnockShareListener,
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ],
              ),
              const Padding(padding: EdgeInsetsDirectional.only(top: 20)),
              const Text("沉浸式卡片"),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TextButton(
                    onPressed: () {
                      if (isOpenShare) {
                        showAlert(context);
                      } else {
                        var shareBean = SharedRecord(UniformDataType.hyperlink);
                        shareBean.content = ShareConstants.hyperlinkUrl;
                        shareBean.thumbnailUri =
                            ShareConstants.exampleKnockUrl1;
                        shareBean.description = ShareConstants.description;
                        shareBean.title = "我看到了中国唯一没有的地貌,峡湾地貌";
                        addKnockShareListener(shareBean);
                      }
                    },
                    style: const ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(Colors.blue),
                    ),
                    child: const Text(
                      ShareConstants.addKnockShareListener,
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  const Padding(padding: EdgeInsets.all(5)),
                  TextButton(
                    onPressed: () {
                      removeKnockShareListener();
                    },
                    style: const ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(Colors.blue),
                    ),
                    child: const Text(
                      ShareConstants.removeKnockShareListener,
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ],
              ),
              const Padding(padding: EdgeInsetsDirectional.only(top: 20)),
              const Text("白卡上下布局"),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TextButton(
                    onPressed: () {
                      if (isOpenShare) {
                        showAlert(context);
                      } else {
                        var shareBean = SharedRecord(UniformDataType.hyperlink);
                        shareBean.content = ShareConstants.hyperlinkUrl;
                        shareBean.thumbnailUri =
                            ShareConstants.exampleKnockUrl2;
                        shareBean.description = ShareConstants.description;
                        shareBean.title = "海岸风光·天气晴朗";
                        addKnockShareListener(shareBean);
                      }
                    },
                    style: const ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(Colors.blue),
                    ),
                    child: const Text(
                      ShareConstants.addKnockShareListener,
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  const Padding(padding: EdgeInsets.all(5)),
                  TextButton(
                    onPressed: () {
                      removeKnockShareListener();
                    },
                    style: const ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(Colors.blue),
                    ),
                    child: const Text(
                      ShareConstants.removeKnockShareListener,
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  void addKnockShareListener(SharedRecord shareBean) {
    knockSharePlugin.setKnockShareData(SharedData(shareBean),
        isAutoClean: true);
    knockSharePlugin.onKnockShare();
    setState(() {
      isOpenShare = true;
    });
  }

  void removeKnockShareListener() {
    knockSharePlugin.offKnockShare();
    setState(() {
      isOpenShare = false;
    });
  }

  void showAlert(BuildContext context) {
    var alert = AlertDialog(
      title: const Text("提示"),
      content: const Text("请先停止当前监听再开启新监听"),
      actions: [
        TextButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: const Text("OK"))
      ],
    );

    showDialog(
        context: context,
        builder: (BuildContext context) {
          return alert;
        }).then((value) => {removeKnockShareListener()});
  }
}

约束与限制 #

本示例仅支持标准系统上运行,支持设备:Phone

地区限制:仅支持中国境内(不包含中国香港、中国澳门、中国台湾)提供服务。

DevEco Studio版本:DevEco Studio 5.0.0 Release及以上。

OpenHarmony SDK: >= API 12.

flutter sdk 版本要求:>= 3.7.12

使用过程中发现任何问题都可以提 Issue

同时,也非常欢迎您提交 PR

开源协议 #

本项目基于Apache License 2.0,请自由地享受和参与开源。

0
likes
110
points
21
downloads

Publisher

unverified uploader

Weekly Downloads

A plug-in library for share.

Homepage
Repository

Topics

#share

Documentation

Documentation
API reference

License

Apache-2.0 (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on hadss_knock_share

Packages that implement hadss_knock_share