Flutter WhatsApp分享插件share_whatsapp_plus的使用
Flutter WhatsApp分享插件share_whatsapp_plus的使用
Share Whatsapp 插件
一个用于将内容从您的Flutter应用分享到WhatsApp分享对话框的插件。
安装
在您的Flutter项目中添加插件:
flutter pub add share_whatsapp_plus
iOS特定安装
在Runner
文件夹内的Info.plist
文件中添加以下内容:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
这将确保iOS应用可以使用URL方案whatsapp://
。
兼容性
功能 | Web | Android | iOS | MacOS | Windows | Linux |
---|---|---|---|---|---|---|
分享文本 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
分享图片 | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
文本+图片 | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
向特定电话号码分享(⚠️) | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
原生功能 | ✅ | ✅ | ||||
使用wa.me 通过url_launcher |
✅ | ✅ | ✅ | ✅ |
⚠️ 注意:
- 对于Web、MacOS、Windows和Linux是必需的。
- 对于Android是可选的。
示例代码
example/lib/main.dart
import 'dart:async';
import 'package:flimer/flimer.dart'; // 引入选择图片的库
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:share_whatsapp_plus/share_whatsapp_plus.dart';
const _kTextMessage = '分享来自share_whatsapp_plus的文本内容 '
'https://pub.dev/packages/share_whatsapp_plus';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _mapInstalled = WhatsApp.values.asMap().map<WhatsApp, String?>(
(key, value) {
return MapEntry(value, null);
},
);
[@override](/user/override)
void initState() {
super.initState();
_checkInstalledWhatsApp();
}
Future<void> _checkInstalledWhatsApp() async {
String whatsAppInstalled = await _check(WhatsApp.standard),
whatsAppBusinessInstalled = await _check(WhatsApp.business);
if (!mounted) return;
setState(() {
_mapInstalled[WhatsApp.standard] = whatsAppInstalled;
_mapInstalled[WhatsApp.business] = whatsAppBusinessInstalled;
});
}
Future<String> _check(WhatsApp type) async {
try {
return await shareWhatsappPlus.installed(type: type)
? 'INSTALLED'
: 'NOT INSTALLED';
} on PlatformException catch (e) {
return e.message ?? '错误';
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('分享WhatsApp示例'),
),
body: ListView(
children: ListTile.divideTiles(
context: context,
tiles: [
const ListTile(title: Text('安装状态')),
...WhatsApp.values.map((type) {
final status = _mapInstalled[type];
return ListTile(
title: Text(type.toString()),
trailing: status != null
? Text(status)
: const CircularProgressIndicator.adaptive(),
);
}),
const ListTile(title: Text('分享内容')),
ListTile(
title: const Text('分享文本'),
trailing: const Icon(Icons.share),
onTap: () => shareWhatsappPlus.shareText(_kTextMessage),
),
ListTile(
title: const Text('分享图片'),
trailing: const Icon(Icons.share),
onTap: () async {
final file = await flimer.pickImage();
if (file != null) {
shareWhatsappPlus.shareFile(file);
}
},
),
ListTile(
title: const Text('分享文本和图片'),
trailing: const Icon(Icons.share),
onTap: () async {
final file = await flimer.pickImage();
if (file != null) {
shareWhatsappPlus.share(text: _kTextMessage, file: file);
}
},
),
ListTile(
title: const Text('向特定电话号码分享文本'),
trailing: const Icon(Icons.share),
onTap: () => shareWhatsappPlus.share(
text: _kTextMessage,
// 更改为您实际的WhatsApp号码
phone: '+0 000-0000-00000',
),
),
],
).toList(),
),
),
);
}
}
更多关于Flutter WhatsApp分享插件share_whatsapp_plus的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter WhatsApp分享插件share_whatsapp_plus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用share_whatsapp_plus
插件的一个代码示例。这个插件允许你直接从Flutter应用分享内容到WhatsApp。
首先,确保你的Flutter项目已经设置完毕,然后在pubspec.yaml
文件中添加share_whatsapp_plus
依赖:
dependencies:
flutter:
sdk: flutter
share_whatsapp_plus: ^最新版本号 # 请替换为实际的最新版本号
之后,运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中导入share_whatsapp_plus
包,并编写分享功能。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:share_whatsapp_plus/share_whatsapp_plus.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('WhatsApp Share Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _shareToWhatsApp,
child: Text('Share to WhatsApp'),
),
),
),
);
}
void _shareToWhatsApp() async {
try {
// 要分享的消息
String message = "Hello, this is a test message from Flutter app!";
// 要分享的文件路径(可选)
// String? filePath = "path/to/your/file.jpg";
// 调用shareToWhatsApp方法
// 如果要分享文件,请取消下面注释并传入filePath参数
// await ShareWhatsAppPlus.shareToWhatsApp(message: message, filePath: filePath);
await ShareWhatsAppPlus.shareToWhatsApp(message: message);
// 显示分享成功的Snackbar(可选)
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Message shared successfully!'),
),
);
} catch (e) {
// 显示分享失败的Snackbar(可选)
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to share message: $e'),
backgroundColor: Colors.red,
),
);
}
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮。点击按钮时,会尝试将一条消息分享到WhatsApp。如果你还想分享文件,可以取消注释filePath
参数并传入文件的路径。
注意:
filePath
参数应该是一个本地文件路径,该文件需要存在于用户的设备上。- 分享文件时,请确保文件存在且WhatsApp支持该文件类型。
- 由于WhatsApp的限制,分享功能可能在某些情况下不可用或表现不一致。
这个示例应该能帮助你开始在Flutter应用中使用share_whatsapp_plus
插件。如果有任何其他问题或需要进一步的帮助,请随时询问。