Flutter 分享功能:facebook,whatsapp,twitter和系统分享

3,364 阅读2分钟

这是我参与8月更文挑战的第13天,活动详情查看:8月更文挑战

之前有有一些需求,需要单独分享到某些App,于是写了这个插件,开始的时候只支持安卓版本,最近添加了ios支持,测试后发布了最新版本,希望能帮到有需求的人。

flutter_share_me

pub package

Flutter 插件,用于将内容分享到社交媒体。支持Android & iOS

您可以使用它分享到 Facebook、WhatsApp(WhatsAppBusiness)、Twitter 和系统分享。 支持网址和文字,whatsapp支持图片

Note: 此插件仍在开发中,某些 API 可能尚不可用。非常欢迎反馈和拉取请求!

开始

add flutter_share_me as a dependency in your pubspec.yaml file.

请检查最新版本

dependencies:
  flutter:
    sdk: flutter
  # add flutter_share_me
  flutter_share_me: ^1.0.0

设置

Android

添加 "facebook app id" to the application tag 到 AndroidManifest.xml

    <application>
       ...
       //add this 
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
            
        <provider
            android:name="com.facebook.FacebookContentProvider"
            android:authorities="com.facebook.app.FacebookContentProvider[facebook_app_id]"
            android:exported="false" />
    </application>

string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Replace "343254889799245" with your Facebook App ID here. -->
    <string name="facebook_app_id">343254889799245</string>
</resources>

IOS

setup facebook

确保在 plist 文件中添加以下细节。

<key>FacebookAppID</key>
<string>fbid</string>
<key>CFBundleURLTypes</key>
	<array>
		<dict>
			<key>CFBundleURLSchemes</key>
			<array>
				<string>fb-your-fb-id</string>
			</array>
		</dict>
	</array>

注意:确保在 CFBundleURLSchemes 中的 fb Id 开头添加 fb。 在 url 方案中添加以下值。

	<array>
		<string>fbauth2</string>
		<string>fbapi</string>
		<string>fbapi20130214</string>
		<string>fbapi20130410</string>
		<string>fbapi20130702</string>
		<string>fbapi20131010</string>
		<string>fbapi20131219</string>
		<string>fbapi20140410</string>
		<string>fbapi20140116</string>
		<string>fbapi20150313</string>
		<string>fbapi20150629</string>
		<string>fbapi20160328</string>
		<string>fbauth</string>
		<string>fb-messenger-share-api</string>
		<string>fbauth2</string>
		<string>fbshareextension</string>
	</array>

设置 Whatsapp

Make sure you add whatsapp in plist.

        <array>
            <string>whatsapp</string>
        </array>

设置 Twiter

        <array>
            <string>twitter</string>
        </array>

使用

将以下导入添加到您的 Dart 代码中:

import 'package:flutter_share_me/flutter_share_me.dart';

方法

shareToFacebook({String msg, String url})

shareToTwitter({String msg, String url})

shareToWhatsApp({String msg,String imagePath})

shareToWhatsApp4Biz({String msg,String imagePath})

shareToSystem({String msg}) use system share ui

如果这些方法成功跳转到相应的应用程序,它们将返回“success”。

ParameterDescription
String msg分享的文本
String url分享的链接
String imagePath图片的路径

Example

 Container(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              SizedBox(height: 30),
              ElevatedButton(
                  onPressed: () => onButtonTap(Share.twitter),
                  child: Text('share to twitter')),
              ElevatedButton(
                onPressed: () => onButtonTap(Share.whatsapp),
                child: Text('share to WhatsApp'),
              ),
              ElevatedButton(
                onPressed: () => onButtonTap(Share.whatsapp_business),
                child: Text('share to WhatsApp  Business'),
              ),
              ElevatedButton(
                onPressed: () => onButtonTap(Share.facebook),
                child: Text('share to  FaceBook'),
              ),
              ElevatedButton(
                onPressed: () => onButtonTap(Share.share_system),
                child: Text('share to System'),
              ),
            ],
          ),
        )

具体实现请参考