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.

example/lib/main.dart

/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:hadss_knock_share/hadss_knock_share.dart';

import 'constants/share_constants.dart';

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()});
  }
}
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