我正在尝试为我的Flutter应用程序实现一个功能,使得当用户点击特定按钮时,可以通过邮件、WhatsApp等分享图像和文本。
我使用了share_plus插件和screenshot插件。
到目前为止,当我通过电子邮件分享时,它可以正常工作,但是当我尝试通过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);
}
}