Flutter分享到WhatsApp插件share_to_whatsapp的使用
Flutter分享到WhatsApp插件share_to_whatsapp的使用
Whatsapp Share Plugin
这是一个为iOS和Android平台提供的Flutter插件,用于简单地将消息、链接或本地文件分享给特定的WhatsApp联系人。
特性
- 可以通过特定联系人分享消息或链接。
- 可以通过特定联系人分享本地文件。
安装
首先,在你的pubspec.yaml
文件中添加以下依赖:
dependencies:
whatsapp_share: ^1.1.1
然后在Dart代码中导入:
import 'package:whatsapp_share/whatsapp_share.dart';
安装(平台特定)
iOS
如果不存在,则在ios/podfile
文件中目标runner后添加一行:
...
target 'Runner' do
use_frameworks!
...
Android
如果你打算使用文件分享功能,需要配置文件提供器,这将授予文件访问权限,使得可以与其他应用程序共享。
在AndroidManifest.xml
中添加:
<application>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
注意:如果你有文件提供器的扩展名,可以更改android:name
。
在res/xml/provider_paths.xml
中添加:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
如何使用?
以下是一些示例代码,展示了两种WhatsApp分享方法。
WhatsApp是否已安装?
Future<void> isInstalled() async {
final val = await WhatsappShare.isInstalled(
package: Package.businessWhatsapp
);
print('WhatsApp Business is installed: $val');
}
如果WhatsApp未安装,请勿调用WhatsappShare.share()
和WhatsappShare.shareFile()
。
分享文本和链接
Future<void> share() async {
await WhatsappShare.share(
text: 'WhatsApp分享文本',
linkUrl: 'https://flutter.dev/',
phone: '911234567890',
);
}
分享图片和文件
Future<void> shareFile() async {
await WhatsappShare.shareFile(
text: 'WhatsApp分享文本',
phone: '911234567890',
filePath: [_image1.path, _image2.path],
);
}
示例代码
以下是完整的示例代码:
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:whatsapp_share/whatsapp_share.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:screenshot/screenshot.dart';
void main() => runApp(MyApp());
// ignore: must_be_immutable
class MyApp extends StatelessWidget {
final _controller = ScreenshotController();
File _image;
Future<void> share() async {
await WhatsappShare.share(
text: 'Example share text',
linkUrl: 'https://flutter.dev/',
phone: '9899950975',
package: Package.whatsapp
);
}
Future<void> shareFile() async {
await getImage();
print('${_image.path}');
await WhatsappShare.shareFile(
text: 'Hello',
phone: '919899950975',
filePath: ["${_image.path}"],
);
}
Future<void> isInstalled() async {
final val = await WhatsappShare.isInstalled();
print('WhatsApp is installed: $val');
}
Future<void> shareScreenShot() async {
Directory directory;
if (Platform.isAndroid) {
directory = await getExternalStorageDirectory();
} else {
directory = await getApplicationDocumentsDirectory();
}
final String localPath =
'${directory.path}/${DateTime.now().toIso8601String()}.png';
await Future.delayed(Duration(seconds: 1));
await WhatsappShare.shareFile(
text: 'WhatsApp message text',
phone: '911234567890',
filePath: [localPath],
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('WhatsApp Share'),
),
body: Center(
child: Screenshot(
controller: _controller,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: Text('分享文本和链接'),
onPressed: share,
),
ElevatedButton(
child: Text('分享图片'),
onPressed: shareFile,
),
ElevatedButton(
child: Text('分享截图'),
onPressed: shareScreenShot,
),
ElevatedButton(
child: Text('检查是否已安装'),
onPressed: isInstalled,
),
],
),
),
),
),
);
}
///从相册中选择图片
Future<void> getImage() async {
try {
XFile _pickedFile = await ImagePicker().pickImage(source: ImageSource.gallery);
if (_pickedFile != null) {
var file = File(_pickedFile.path);
// 获取保存目录
final directory = await getExternalStorageDirectory();
// 复制文件到新路径
_image = await file.copy('${directory.path}/image1.png');
} else {}
} catch (er) {
print(er);
}
}
}
更多关于Flutter分享到WhatsApp插件share_to_whatsapp的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter分享到WhatsApp插件share_to_whatsapp的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,share_to_whatsapp
插件可以帮助你轻松地将文本、图片或其他内容分享到 WhatsApp。以下是如何使用这个插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 share_to_whatsapp
插件的依赖:
dependencies:
flutter:
sdk: flutter
share_to_whatsapp: ^1.0.0 # 请检查最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入插件
在你的 Dart 文件中导入插件:
import 'package:share_to_whatsapp/share_to_whatsapp.dart';
3. 使用插件
share_to_whatsapp
插件提供了几种方法来分享内容到 WhatsApp。
分享文本
你可以使用 ShareToWhatsapp.shareText
方法来分享纯文本:
void shareTextToWhatsApp() async {
try {
await ShareToWhatsapp.shareText(
text: 'Hello, this is a message from my Flutter app!',
);
} catch (e) {
print('Error sharing text: $e');
}
}
分享图片
你可以使用 ShareToWhatsapp.shareFile
方法来分享图片:
void shareImageToWhatsApp() async {
try {
await ShareToWhatsapp.shareFile(
filePath: '/path/to/your/image.png',
);
} catch (e) {
print('Error sharing image: $e');
}
}
分享文本和图片
你可以结合使用 ShareToWhatsapp.shareTextAndFile
方法来同时分享文本和图片:
void shareTextAndImageToWhatsApp() async {
try {
await ShareToWhatsapp.shareTextAndFile(
text: 'Check out this image!',
filePath: '/path/to/your/image.png',
);
} catch (e) {
print('Error sharing text and image: $e');
}
}
4. 权限处理
在某些情况下,你可能需要处理文件访问权限。确保你的应用有读取文件的权限,尤其是在分享图片时。
5. 处理错误
在实际使用中,可能会出现各种错误(例如文件路径错误、权限问题等),因此最好在使用时添加错误处理逻辑。
6. 运行应用
现在你可以运行你的 Flutter 应用,并测试分享功能。当你调用上述方法时,系统会自动打开 WhatsApp 并填充你提供的内容。
示例代码
以下是一个完整的示例代码,展示了如何分享文本和图片到 WhatsApp:
import 'package:flutter/material.dart';
import 'package:share_to_whatsapp/share_to_whatsapp.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Share to WhatsApp Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: shareTextToWhatsApp,
child: Text('Share Text'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: shareImageToWhatsApp,
child: Text('Share Image'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: shareTextAndImageToWhatsApp,
child: Text('Share Text and Image'),
),
],
),
),
),
);
}
void shareTextToWhatsApp() async {
try {
await ShareToWhatsapp.shareText(
text: 'Hello, this is a message from my Flutter app!',
);
} catch (e) {
print('Error sharing text: $e');
}
}
void shareImageToWhatsApp() async {
try {
await ShareToWhatsapp.shareFile(
filePath: '/path/to/your/image.png',
);
} catch (e) {
print('Error sharing image: $e');
}
}
void shareTextAndImageToWhatsApp() async {
try {
await ShareToWhatsapp.shareTextAndFile(
text: 'Check out this image!',
filePath: '/path/to/your/image.png',
);
} catch (e) {
print('Error sharing text and image: $e');
}
}
}