Fix share button on iPad (#124)

This commit is contained in:
John Maguire 2023-05-15 16:12:24 -04:00 committed by GitHub
parent 5ae4c20c70
commit f7a7093879
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 41 deletions

View File

@ -84,23 +84,19 @@ class _SiteLogsScreenState extends State<SiteLogsScreen> {
), ),
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ child: Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Expanded( Expanded(
child: PlatformIconButton( child: Builder(
builder: (BuildContext context) {
return PlatformIconButton(
padding: padding, padding: padding,
icon: Icon(context.platformIcons.share, size: 30), icon: Icon(context.platformIcons.share, size: 30),
onPressed: () { onPressed: () {
Share.shareFile( Share.shareFile(context,
title: '${widget.site.name} logs', title: '${widget.site.name} logs',
filePath: widget.site.logFile, filePath: widget.site.logFile,
filename: '${widget.site.name}.log'); filename: '${widget.site.name}.log');
}, },
)), );
Expanded( }
child: PlatformIconButton(
padding: padding,
icon: Icon(context.platformIcons.delete, size: Platform.isIOS ? 38 : 30),
onPressed: () {
Utils.confirmDelete(context, 'Are you sure you want to clear all logs?', () => deleteLogs());
},
)), )),
Expanded( Expanded(
child: PlatformIconButton( child: PlatformIconButton(

View File

@ -92,10 +92,17 @@ class _AddCertificateScreenState extends State<AddCertificateScreen> {
labelWidth: 0, labelWidth: 0,
content: SelectableText(pubKey, style: TextStyle(fontFamily: 'RobotoMono', fontSize: 14)), content: SelectableText(pubKey, style: TextStyle(fontFamily: 'RobotoMono', fontSize: 14)),
), ),
ConfigButtonItem( Builder(
builder: (BuildContext context) {
return ConfigButtonItem(
content: Text('Share Public Key'), content: Text('Share Public Key'),
onPressed: () async { onPressed: () async {
await Share.share(title: 'Please sign and return a certificate', text: pubKey, filename: 'device.pub'); await Share.share(context,
title: 'Please sign and return a certificate',
text: pubKey,
filename: 'device.pub');
},
);
}, },
), ),
]) ])

View File

@ -19,11 +19,17 @@ class RenderedConfigScreen extends StatelessWidget {
title: Text('Rendered Site Config'), title: Text('Rendered Site Config'),
scrollable: SimpleScrollable.both, scrollable: SimpleScrollable.both,
trailingActions: <Widget>[ trailingActions: <Widget>[
PlatformIconButton( Builder(
builder: (BuildContext context) {
return PlatformIconButton(
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
icon: Icon(context.platformIcons.share, size: 28.0), icon: Icon(context.platformIcons.share, size: 28.0),
onPressed: () => Share.share(title: '$name.yaml', text: config, filename: '$name.yaml'), onPressed: () =>
) Share.share(context,
title: '$name.yaml', text: config, filename: '$name.yaml'),
);
}
),
], ],
child: Container( child: Container(
padding: EdgeInsets.all(5), padding: EdgeInsets.all(5),

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart' as sp; import 'package:share_plus/share_plus.dart' as sp;
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
@ -10,7 +11,8 @@ class Share {
/// - title: Title of message or subject if sending an email /// - title: Title of message or subject if sending an email
/// - text: The text to share /// - text: The text to share
/// - filename: The filename to use if sending over airdrop for example /// - filename: The filename to use if sending over airdrop for example
static Future<bool> share({ static Future<bool> share(
BuildContext context, {
required String title, required String title,
required String text, required String text,
required String filename, required String filename,
@ -25,7 +27,7 @@ class Share {
try { try {
file.writeAsStringSync(text, flush: true); file.writeAsStringSync(text, flush: true);
res = await Share.shareFile(title: title, filePath: file.path); res = await Share.shareFile(context, title: title, filePath: file.path);
} catch (err) { } catch (err) {
// Ignoring file write errors // Ignoring file write errors
} }
@ -38,19 +40,22 @@ class Share {
/// - title: Title of message or subject if sending an email /// - title: Title of message or subject if sending an email
/// - filePath: Path to the file to share /// - filePath: Path to the file to share
/// - filename: An optional filename to override the existing file /// - filename: An optional filename to override the existing file
static Future<bool> shareFile({ static Future<bool> shareFile(BuildContext context,
required String title, {required String title,
required String filePath, required String filePath,
String? filename String? filename}) async {
}) async {
assert(title.isNotEmpty); assert(title.isNotEmpty);
assert(filePath.isNotEmpty); assert(filePath.isNotEmpty);
final box = context.findRenderObject() as RenderBox?;
//NOTE: the filename used to specify the name of the file in gmail/slack/etc but no longer works that way //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, // If we want to support that again we will need to save the file to a temporary directory, share that,
// and then delete it // and then delete it
final xFile = sp.XFile(filePath, name: filename); final xFile = sp.XFile(filePath, name: filename);
final result = await sp.Share.shareXFiles([xFile], subject: title); final result = await sp.Share.shareXFiles([xFile],
subject: title,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
return result.status == sp.ShareResultStatus.success; return result.status == sp.ShareResultStatus.success;
} }
} }