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 2 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 @@ -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
248 changes: 140 additions & 108 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 @@ -195,9 +214,19 @@ class DemoAppState extends State<DemoApp> {
subject: subject,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
}
scaffoldMessenger.showSnackBar(SnackBar(
content: Text("Share result: ${result.status}"),
));
scaffoldMessenger.showSnackBar(
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}")
],
),
),
);
}

void _onShareXFileFromAssets(BuildContext context) async {
Expand All @@ -208,15 +237,18 @@ class DemoAppState extends State<DemoApp> {
final result = 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(
SnackBar(
content: Text("Share result: ${result.status}"),
),
);
miquelbeltran marked this conversation as resolved.
Show resolved Hide resolved
}
}