lemmur-app-android/lib/util/share.dart

40 lines
995 B
Dart
Raw Normal View History

import 'dart:io';
2021-03-18 19:24:29 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
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, {
String? subject,
Rect? sharePositionOrigin,
required BuildContext context,
2021-03-20 15:50:49 +01:00
}) async {
if (Platform.isLinux || Platform.isWindows) {
await _fallbackShare(text, context: context);
return;
}
2021-03-20 15:50:49 +01:00
try {
await Share.share(
2021-03-20 15:50:49 +01:00
text,
subject: subject,
sharePositionOrigin: sharePositionOrigin,
);
} on MissingPluginException {
await _fallbackShare(text, context: context);
2021-03-18 19:24:29 +01: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!')),
);
}