Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(share_plus): add option to add custom link title on Android #3270

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class MethodCallHandler(
call.argument<Any>("uri") as String,
subject = null,
withResult = isWithResult,
call.argument<Any>("title") as String?,
)
success(isWithResult, result)
}
Expand All @@ -38,6 +39,7 @@ internal class MethodCallHandler(
call.argument<Any>("text") as String,
call.argument<Any>("subject") as String?,
isWithResult,
null,
)
success(isWithResult, result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ internal class Share(
this.activity = activity
}

fun share(text: String, subject: String?, withResult: Boolean) {
fun share(text: String, subject: String?, withResult: Boolean, title: String?) {
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, text)
if (subject != null) {
putExtra(Intent.EXTRA_SUBJECT, subject)
}
if (title != null) {
putExtra(Intent.EXTRA_TITLE, title)
}
}
// If we dont want the result we use the old 'createChooser'
val chooserIntent =
Expand Down Expand Up @@ -97,7 +100,7 @@ internal class Share(
val shareIntent = Intent()
when {
(fileUris.isEmpty() && !text.isNullOrBlank()) -> {
share(text, subject, withResult)
share(text, subject, withResult, null)
return
}

Expand Down
5 changes: 5 additions & 0 deletions packages/share_plus/share_plus/lib/share_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,22 @@ class Share {
/// origin rect for the share sheet to popover from on iPads and Macs. It has no effect
/// on other devices.
///
/// The optional [title] parameter can be used to specify a custom title for
/// the uri on Android. It has no effect on other devices.
///
/// May throw [PlatformException]
/// from [MethodChannel].
///
/// See documentation about [ShareResult] on [share] method.
static Future<ShareResult> shareUri(
Uri uri, {
Rect? sharePositionOrigin,
String? title,
}) async {
return _platform.shareUri(
uri,
sharePositionOrigin: sharePositionOrigin,
title: title,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SharePlusLinuxPlugin extends SharePlatform {
String? subject,
String? text,
Rect? sharePositionOrigin,
String? title,
}) async {
throw UnimplementedError(
'shareUri() has not been implemented on Linux. Use share().');
Expand Down
1 change: 1 addition & 0 deletions packages/share_plus/share_plus/lib/src/share_plus_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SharePlusWebPlugin extends SharePlatform {
Future<ShareResult> shareUri(
Uri uri, {
Rect? sharePositionOrigin,
String? title,
}) async {
final data = ShareData(
url: uri.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SharePlusWindowsPlugin extends SharePlatform {
String? subject,
String? text,
Rect? sharePositionOrigin,
String? title,
}) async {
throw UnimplementedError(
'shareUri() has not been implemented on Windows. Use share().');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MethodChannelShare extends SharePlatform {
Future<ShareResult> shareUri(
Uri uri, {
Rect? sharePositionOrigin,
String? title,
}) async {
final params = <String, dynamic>{'uri': uri.toString()};

Expand All @@ -37,6 +38,10 @@ class MethodChannelShare extends SharePlatform {
params['originHeight'] = sharePositionOrigin.height;
}

if (title != null) {
params['title'] = title;
}

final result = await channel.invokeMethod<String>('shareUri', params) ??
'dev.fluttercommunity.plus/share/unavailable';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class SharePlatform extends PlatformInterface {
Future<ShareResult> shareUri(
Uri uri, {
Rect? sharePositionOrigin,
String? title,
}) {
return _instance.shareUri(
uri,
sharePositionOrigin: sharePositionOrigin,
title: title,
);
}

Expand Down
Loading