blob: 4a3ff6f1de0984368cddf95646f5b969f5290aa2 [file] [log] [blame]
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show visibleForTesting;
import 'package:mime/mime.dart' show lookupMimeType;
/// Plugin for summoning a platform share sheet.
class Share {
/// [MethodChannel] used to communicate with the platform side.
@visibleForTesting
static const MethodChannel channel =
MethodChannel('plugins.flutter.io/share');
/// Summons the platform's share sheet to share text.
///
/// Wraps the platform's native share dialog. Can share a text and/or a URL.
/// It uses the `ACTION_SEND` Intent on Android and `UIActivityViewController`
/// on iOS.
///
/// The optional [subject] parameter can be used to populate a subject if the
/// user chooses to send an email.
///
/// The optional [sharePositionOrigin] parameter can be used to specify a global
/// origin rect for the share sheet to popover from on iPads. It has no effect
/// on non-iPads.
///
/// May throw [PlatformException] or [FormatException]
/// from [MethodChannel].
static Future<void> share(
String text, {
String subject,
Rect sharePositionOrigin,
}) {
assert(text != null);
assert(text.isNotEmpty);
final Map<String, dynamic> params = <String, dynamic>{
'text': text,
'subject': subject,
};
if (sharePositionOrigin != null) {
params['originX'] = sharePositionOrigin.left;
params['originY'] = sharePositionOrigin.top;
params['originWidth'] = sharePositionOrigin.width;
params['originHeight'] = sharePositionOrigin.height;
}
return channel.invokeMethod<void>('share', params);
}
/// Summons the platform's share sheet to share multiple files.
///
/// Wraps the platform's native share dialog. Can share a file.
/// It uses the `ACTION_SEND` Intent on Android and `UIActivityViewController`
/// on iOS.
///
/// The optional `sharePositionOrigin` parameter can be used to specify a global
/// origin rect for the share sheet to popover from on iPads. It has no effect
/// on non-iPads.
///
/// May throw [PlatformException] or [FormatException]
/// from [MethodChannel].
static Future<void> shareFiles(
List<String> paths, {
List<String> mimeTypes,
String subject,
String text,
Rect sharePositionOrigin,
}) {
assert(paths != null);
assert(paths.isNotEmpty);
assert(paths.every((element) => element != null && element.isNotEmpty));
final Map<String, dynamic> params = <String, dynamic>{
'paths': paths,
'mimeTypes': mimeTypes ??
paths.map((String path) => _mimeTypeForPath(path)).toList(),
};
if (subject != null) params['subject'] = subject;
if (text != null) params['text'] = text;
if (sharePositionOrigin != null) {
params['originX'] = sharePositionOrigin.left;
params['originY'] = sharePositionOrigin.top;
params['originWidth'] = sharePositionOrigin.width;
params['originHeight'] = sharePositionOrigin.height;
}
return channel.invokeMethod('shareFiles', params);
}
static String _mimeTypeForPath(String path) {
assert(path != null);
return lookupMimeType(path) ?? 'application/octet-stream';
}
}