2020-08-18 00:12:28 +00:00
|
|
|
import 'dart:async';
|
2022-11-17 21:46:06 +00:00
|
|
|
import 'dart:io';
|
2020-08-18 00:12:28 +00:00
|
|
|
|
2023-05-15 20:12:24 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
2022-11-17 21:46:06 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:share_plus/share_plus.dart' as sp;
|
|
|
|
import 'package:path/path.dart' as p;
|
2020-08-18 00:12:28 +00:00
|
|
|
|
|
|
|
class Share {
|
2022-11-17 21:46:06 +00:00
|
|
|
/// Transforms a string of text into a file and shares that file
|
2020-08-18 00:12:28 +00:00
|
|
|
/// - title: Title of message or subject if sending an email
|
|
|
|
/// - text: The text to share
|
|
|
|
/// - filename: The filename to use if sending over airdrop for example
|
2023-05-15 20:12:24 +00:00
|
|
|
static Future<bool> share(
|
|
|
|
BuildContext context, {
|
2022-09-21 20:27:35 +00:00
|
|
|
required String title,
|
|
|
|
required String text,
|
|
|
|
required String filename,
|
|
|
|
}) async {
|
|
|
|
assert(title.isNotEmpty);
|
|
|
|
assert(text.isNotEmpty);
|
|
|
|
assert(filename.isNotEmpty);
|
|
|
|
|
2022-11-17 21:46:06 +00:00
|
|
|
final tmpDir = await getTemporaryDirectory();
|
|
|
|
final file = File(p.join(tmpDir.path, filename));
|
|
|
|
var res = false;
|
2020-08-18 00:12:28 +00:00
|
|
|
|
2022-11-17 21:46:06 +00:00
|
|
|
try {
|
|
|
|
file.writeAsStringSync(text, flush: true);
|
2023-05-15 20:12:24 +00:00
|
|
|
res = await Share.shareFile(context, title: title, filePath: file.path);
|
2022-11-17 21:46:06 +00:00
|
|
|
} catch (err) {
|
|
|
|
// Ignoring file write errors
|
|
|
|
}
|
2020-08-18 00:12:28 +00:00
|
|
|
|
2022-11-17 21:46:06 +00:00
|
|
|
file.delete();
|
|
|
|
return res;
|
2020-08-18 00:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Shares a local file
|
|
|
|
/// - title: Title of message or subject if sending an email
|
|
|
|
/// - filePath: Path to the file to share
|
|
|
|
/// - filename: An optional filename to override the existing file
|
2023-05-15 20:12:24 +00:00
|
|
|
static Future<bool> shareFile(BuildContext context,
|
|
|
|
{required String title,
|
|
|
|
required String filePath,
|
|
|
|
String? filename}) async {
|
2022-09-21 20:27:35 +00:00
|
|
|
assert(title.isNotEmpty);
|
|
|
|
assert(filePath.isNotEmpty);
|
|
|
|
|
2023-05-15 20:12:24 +00:00
|
|
|
final box = context.findRenderObject() as RenderBox?;
|
|
|
|
|
2022-11-17 21:46:06 +00:00
|
|
|
//NOTE: the filename used to specify the name of the file in gmail/slack/etc but no longer works that way
|
|
|
|
// If we want to support that again we will need to save the file to a temporary directory, share that,
|
|
|
|
// and then delete it
|
|
|
|
final xFile = sp.XFile(filePath, name: filename);
|
2023-05-15 20:12:24 +00:00
|
|
|
final result = await sp.Share.shareXFiles([xFile],
|
|
|
|
subject: title,
|
|
|
|
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
|
2022-11-17 21:46:06 +00:00
|
|
|
return result.status == sp.ShareResultStatus.success;
|
2020-08-18 00:12:28 +00:00
|
|
|
}
|
|
|
|
}
|