提问人:Berkin 提问时间:10/5/2021 最后编辑:Berkin 更新时间:1/13/2023 访问量:3238
Flutter share plus:在 WhatsApp 上分享图片和文字
Flutter share plus: Sharing image and text on WhatsApp
问:
我正在尝试在我的 flutter 应用程序中实现一个功能,以便如果用户按下特定按钮,则应通过邮件、WhatsApp 等共享图像和文本。 我使用了share_plus插件和屏幕截图插件。 到目前为止,当我通过邮件共享它时,我设法让它工作,但是当我尝试通过 WhatsApp 共享图像+文本时,它不起作用。它只会共享文本,并且图像总是丢失。当我删除文本并仅分享图像时,WhatsApp 上的一切都很好。 你能帮帮我吗?非常感谢!
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:typed_data';
import 'package:screenshot/screenshot.dart';
import 'dart:io';
class InfoScreen extends StatelessWidget {
final controller = ScreenshotController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Info'),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFBD23E), Color(0xffF6BE03)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
),
),
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFEFDFD), Color(0xffBDBDB2)],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
),
child: Column(
children: <Widget>[
ButtonTheme(
minWidth: 10000,
height: 45,
child: Padding(
padding: EdgeInsets.fromLTRB(15, 2, 15, 15),
child: Align(
alignment: Alignment.topCenter,
child: RaisedButton(
onPressed: () async {
final image = await controller
.captureFromWidget(buildImageStartseite());
saveAndShare(image);
},
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(12.0),
bottomRight: Radius.circular(12.0),
),
),
highlightColor: Color(0xffB4B4B3),
splashColor: Colors.transparent,
child: Text(
'Press to share',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 17, color: Color(0xff232323)),
),
),
),
),
),
],
),
),
);
}
Future saveAndShare(Uint8List bytes) async {
final directory = await getApplicationDocumentsDirectory();
final image = File('${directory.path}/Shared_picture.png');
image.writeAsBytesSync(bytes);
final text =
'I'm sharing this with you!';
await Share.shareFiles([image.path], text: text);
}
}
答:
评论