2021-09-12 22:47:05 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2021-03-18 19:24:29 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2021-09-12 22:47:05 +02:00
|
|
|
import 'package:share_plus/share_plus.dart';
|
2021-03-18 19:24:29 +01:00
|
|
|
|
|
|
|
/// A `package:share` wrapper that fallbacks to copying contents to the clipboard
|
|
|
|
/// on platforms that do not support native sharing
|
2021-03-20 15:50:49 +01:00
|
|
|
Future<void> share(
|
|
|
|
String text, {
|
2021-04-09 00:11:44 +02:00
|
|
|
String? subject,
|
|
|
|
Rect? sharePositionOrigin,
|
|
|
|
required BuildContext context,
|
2021-03-20 15:50:49 +01:00
|
|
|
}) async {
|
2021-09-12 22:47:05 +02:00
|
|
|
if (Platform.isLinux || Platform.isWindows) {
|
|
|
|
await _fallbackShare(text, context: context);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-20 15:50:49 +01:00
|
|
|
try {
|
2021-09-12 22:47:05 +02:00
|
|
|
await Share.share(
|
2021-03-20 15:50:49 +01:00
|
|
|
text,
|
|
|
|
subject: subject,
|
|
|
|
sharePositionOrigin: sharePositionOrigin,
|
|
|
|
);
|
|
|
|
} on MissingPluginException {
|
2021-09-12 22:47:05 +02:00
|
|
|
await _fallbackShare(text, context: context);
|
2021-03-18 19:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-12 22:47:05 +02:00
|
|
|
|
|
|
|
Future<void> _fallbackShare(
|
|
|
|
String text, {
|
|
|
|
required BuildContext context,
|
|
|
|
}) async {
|
|
|
|
await Clipboard.setData(ClipboardData(text: text));
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(content: Text('Copied data to clipboard!')),
|
|
|
|
);
|
|
|
|
}
|