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): Show destination for share with result in example, update example UI #1314

Merged
merged 3 commits into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,6 @@
<string>arm64</string>
</array>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>11.0</string>
</dict>
</plist>
Expand Up @@ -333,7 +333,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -383,7 +383,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down
Expand Up @@ -19,7 +19,7 @@ class ImagePreviews extends StatelessWidget {
@override
Widget build(BuildContext context) {
if (imagePaths.isEmpty) {
return Container();
return const SizedBox.shrink();
}

final imageWidgets = <Widget>[];
Expand Down
254 changes: 142 additions & 112 deletions packages/share_plus/share_plus/example/lib/main.dart
Expand Up @@ -37,110 +37,129 @@ class DemoAppState extends State<DemoApp> {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Share Plus Plugin Demo',
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: const Color(0x9f4376f8),
),
home: Scaffold(
appBar: AppBar(
title: const Text('Share Plus Plugin Demo'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextField(
decoration: const InputDecoration(
labelText: 'Share text:',
hintText: 'Enter some text and/or link to share',
appBar: AppBar(
title: const Text('Share Plus Plugin Demo'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Share text',
hintText: 'Enter some text and/or link to share',
),
maxLines: null,
onChanged: (String value) => setState(() {
text = value;
}),
),
const SizedBox(height: 16),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Share subject',
hintText: 'Enter subject to share (optional)',
),
maxLines: null,
onChanged: (String value) => setState(() {
subject = value;
}),
),
const SizedBox(height: 16),
ImagePreviews(imagePaths, onDelete: _onDeleteImage),
ElevatedButton.icon(
label: const Text('Add image'),
onPressed: () async {
// Using `package:image_picker` to get image from gallery.
if (Platform.isMacOS ||
Platform.isLinux ||
Platform.isWindows) {
// Using `package:file_selector` on windows, macos & Linux, since `package:image_picker` is not supported.
const XTypeGroup typeGroup = XTypeGroup(
label: 'images',
extensions: <String>['jpg', 'jpeg', 'png', 'gif'],
);
final file = await openFile(
acceptedTypeGroups: <XTypeGroup>[typeGroup]);
if (file != null) {
setState(() {
imagePaths.add(file.path);
imageNames.add(file.name);
});
}
} else {
final imagePicker = ImagePicker();
final pickedFile = await imagePicker.pickImage(
source: ImageSource.gallery,
);
if (pickedFile != null) {
setState(() {
imagePaths.add(pickedFile.path);
imageNames.add(pickedFile.name);
});
}
}
},
icon: const Icon(Icons.add),
),
const SizedBox(height: 32),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.onPrimary,
backgroundColor: Theme.of(context).colorScheme.primary,
),
maxLines: 2,
onChanged: (String value) => setState(() {
text = value;
}),
),
TextField(
decoration: const InputDecoration(
labelText: 'Share subject:',
hintText: 'Enter subject to share (optional)',
onPressed: text.isEmpty && imagePaths.isEmpty
? null
: () => _onShare(context),
child: const Text('Share'),
);
},
),
const SizedBox(height: 16),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.onPrimary,
backgroundColor: Theme.of(context).colorScheme.primary,
),
maxLines: 2,
onChanged: (String value) => setState(() {
subject = value;
}),
),
const Padding(padding: EdgeInsets.only(top: 12.0)),
ImagePreviews(imagePaths, onDelete: _onDeleteImage),
ListTile(
leading: const Icon(Icons.add),
title: const Text('Add image'),
onTap: () async {
// Using `package:image_picker` to get image from gallery.
if (Platform.isMacOS ||
Platform.isLinux ||
Platform.isWindows) {
// Using `package:file_selector` on windows, macos & Linux, since `package:image_picker` is not supported.
const XTypeGroup typeGroup = XTypeGroup(
label: 'images',
extensions: <String>['jpg', 'jpeg', 'png', 'gif'],
);
final file = await openFile(
acceptedTypeGroups: <XTypeGroup>[typeGroup]);
if (file != null) {
setState(() {
imagePaths.add(file.path);
imageNames.add(file.name);
});
}
} else {
final imagePicker = ImagePicker();
final pickedFile = await imagePicker.pickImage(
source: ImageSource.gallery,
);
if (pickedFile != null) {
setState(() {
imagePaths.add(pickedFile.path);
imageNames.add(pickedFile.name);
});
}
}
},
),
const Padding(padding: EdgeInsets.only(top: 12.0)),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
onPressed: text.isEmpty && imagePaths.isEmpty
? null
: () => _onShare(context),
child: const Text('Share'),
);
},
),
const Padding(padding: EdgeInsets.only(top: 12.0)),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
onPressed: text.isEmpty && imagePaths.isEmpty
? null
: () => _onShareWithResult(context),
child: const Text('Share With Result'),
);
},
),
const Padding(padding: EdgeInsets.only(top: 12.0)),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
onPressed: () {
_onShareXFileFromAssets(context);
},
child: const Text('Share XFile from Assets'),
);
onPressed: text.isEmpty && imagePaths.isEmpty
? null
: () => _onShareWithResult(context),
child: const Text('Share With Result'),
);
},
),
const SizedBox(height: 16),
Builder(
builder: (BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.onPrimary,
backgroundColor: Theme.of(context).colorScheme.primary,
),
onPressed: () {
_onShareXFileFromAssets(context);
},
),
],
child: const Text('Share XFile from Assets'),
);
},
),
),
)),
],
),
),
),
);
}

Expand Down Expand Up @@ -180,43 +199,54 @@ class DemoAppState extends State<DemoApp> {
void _onShareWithResult(BuildContext context) async {
final box = context.findRenderObject() as RenderBox?;
final scaffoldMessenger = ScaffoldMessenger.of(context);
ShareResult result;
ShareResult shareResult;
if (imagePaths.isNotEmpty) {
final files = <XFile>[];
for (var i = 0; i < imagePaths.length; i++) {
files.add(XFile(imagePaths[i], name: imageNames[i]));
}
result = await Share.shareXFiles(files,
shareResult = await Share.shareXFiles(files,
text: text,
subject: subject,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
} else {
result = await Share.shareWithResult(text,
shareResult = await Share.shareWithResult(text,
subject: subject,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
}
scaffoldMessenger.showSnackBar(SnackBar(
content: Text("Share result: ${result.status}"),
));
scaffoldMessenger.showSnackBar(getResultSnackBar(shareResult));
}

void _onShareXFileFromAssets(BuildContext context) async {
final box = context.findRenderObject() as RenderBox?;
final scaffoldMessenger = ScaffoldMessenger.of(context);
final data = await rootBundle.load('assets/flutter_logo.png');
final buffer = data.buffer;
final result = await Share.shareXFiles(
final shareResult = await Share.shareXFiles(
[
XFile.fromData(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes),
name: 'flutter_logo.png',
mimeType: 'image/png'),
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes),
name: 'flutter_logo.png',
mimeType: 'image/png',
),
],
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);

scaffoldMessenger.showSnackBar(SnackBar(
content: Text("Share result: ${result.status}"),
));
scaffoldMessenger.showSnackBar(getResultSnackBar(shareResult));
}

SnackBar getResultSnackBar(ShareResult result) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be private

return SnackBar(
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Share result: ${result.status}"),
if (result.status == ShareResultStatus.success)
Text("Shared to: ${result.raw}")
],
),
);
}
}