-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmain.dart
More file actions
361 lines (342 loc) · 12.5 KB
/
main.dart
File metadata and controls
361 lines (342 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 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.
// ignore_for_file: public_member_api_docs
import 'dart:convert';
import 'dart:io';
import 'package:file_selector/file_selector.dart'
hide XFile; // hides to test if share_plus exports XFile
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart'
hide XFile; // hides to test if share_plus exports XFile
import 'package:share_plus/share_plus.dart';
import 'excluded_activity_type.dart';
import 'image_previews.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Share Plus Plugin Demo',
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: const Color(0x9f4376f8),
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<StatefulWidget> createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
String text = '';
String subject = '';
String title = '';
String uri = '';
String fileName = '';
List<String> imageNames = [];
List<String> imagePaths = [];
List<CupertinoActivityType> excludedCupertinoActivityType = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Share Plus Plugin Demo'), elevation: 4),
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),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Share title',
hintText: 'Enter title to share (optional)',
),
maxLines: null,
onChanged: (String value) => setState(() {
title = value;
}),
),
const SizedBox(height: 16),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Share uri',
hintText: 'Enter the uri you want to share',
),
maxLines: null,
onChanged: (String value) {
setState(() => uri = value);
},
),
const SizedBox(height: 16),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Share Text as File',
hintText: 'Enter the filename you want to share your text as',
),
maxLines: null,
onChanged: (String value) {
setState(() => fileName = 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 (!kIsWeb &&
(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),
),
if (!kIsWeb && (Platform.isIOS || Platform.isMacOS))
const SizedBox(height: 16),
if (!kIsWeb && (Platform.isIOS || Platform.isMacOS))
ElevatedButton(
onPressed: _onSelectExcludedActivityType,
child: const Text('Add Excluded Activity Type'),
),
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,
),
onPressed: text.isEmpty && imagePaths.isEmpty
? null
: () => _onShareWithResult(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,
),
onPressed: () {
_onShareXFileFromAssets(context);
},
child: const Text('Share XFile from Assets'),
);
},
),
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: fileName.isEmpty || text.isEmpty
? null
: () => _onShareTextAsXFile(context),
child: const Text('Share text as XFile'),
);
},
),
],
),
),
);
}
void _onDeleteImage(int position) {
setState(() {
imagePaths.removeAt(position);
imageNames.removeAt(position);
});
}
void _onSelectExcludedActivityType() async {
final result = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ExcludedCupertinoActivityTypePage(
excludedActivityType: excludedCupertinoActivityType,
),
),
);
if (result != null) {
excludedCupertinoActivityType = result;
}
}
void _onShareWithResult(BuildContext context) async {
// A builder is used to retrieve the context immediately
// surrounding the ElevatedButton.
//
// The context's `findRenderObject` returns the first
// RenderObject in its descendent tree when it's not
// a RenderObjectWidget. The ElevatedButton's RenderObject
// has its position and size after it's built.
final box = context.findRenderObject() as RenderBox?;
final scaffoldMessenger = ScaffoldMessenger.of(context);
ShareResult shareResult;
if (imagePaths.isNotEmpty) {
final files = <XFile>[];
for (var i = 0; i < imagePaths.length; i++) {
files.add(XFile(imagePaths[i], name: imageNames[i]));
}
shareResult = await SharePlus.instance.share(
ShareParams(
text: text.isEmpty ? null : text,
subject: subject.isEmpty ? null : subject,
title: title.isEmpty ? null : title,
files: files,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
excludedCupertinoActivities: [CupertinoActivityType.airDrop],
),
);
} else if (uri.isNotEmpty) {
shareResult = await SharePlus.instance.share(
ShareParams(
uri: Uri.parse(uri),
subject: subject.isEmpty ? null : subject,
title: title.isEmpty ? null : title,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
excludedCupertinoActivities: excludedCupertinoActivityType,
),
);
} else {
shareResult = await SharePlus.instance.share(
ShareParams(
text: text.isEmpty ? null : text,
subject: subject.isEmpty ? null : subject,
title: title.isEmpty ? null : title,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
excludedCupertinoActivities: excludedCupertinoActivityType,
),
);
}
scaffoldMessenger.showSnackBar(getResultSnackBar(shareResult));
}
void _onShareXFileFromAssets(BuildContext context) async {
final box = context.findRenderObject() as RenderBox?;
final scaffoldMessenger = ScaffoldMessenger.of(context);
try {
final data = await rootBundle.load('assets/flutter_logo.png');
final buffer = data.buffer;
final shareResult = await SharePlus.instance.share(
ShareParams(
files: [
XFile.fromData(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes),
name: 'flutter_logo.png',
mimeType: 'image/png',
),
],
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
downloadFallbackEnabled: true,
excludedCupertinoActivities: excludedCupertinoActivityType,
),
);
scaffoldMessenger.showSnackBar(getResultSnackBar(shareResult));
} catch (e) {
scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e')));
}
}
void _onShareTextAsXFile(BuildContext context) async {
final box = context.findRenderObject() as RenderBox?;
final scaffoldMessenger = ScaffoldMessenger.of(context);
try {
final shareResult = await SharePlus.instance.share(
ShareParams(
files: [
XFile.fromData(
utf8.encode(text),
// name: fileName, // Notice, how setting the name here does not work.
mimeType: 'text/plain',
),
],
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
fileNameOverrides: [fileName],
downloadFallbackEnabled: true,
excludedCupertinoActivities: excludedCupertinoActivityType,
),
);
scaffoldMessenger.showSnackBar(getResultSnackBar(shareResult));
} catch (e) {
scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e')));
}
}
SnackBar getResultSnackBar(ShareResult result) {
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}"),
],
),
);
}
}