-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
169 lines (150 loc) · 4.69 KB
/
main.dart
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
// ignore_for_file: avoid_print, deprecated_member_use
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
const String normalJPEG1 = "test_normalJPEG_1.png";
const String normalJPEG2 = "test_normalJPEG_2.jpg";
const String noExtensionJPEG = "test_noExtensionJPEG";
const String pdfDocument = "test_pdfDocument.pdf";
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await downloadFile(
"https://docs.flutter.dev/assets/images/dash/Dash.png",
normalJPEG1,
);
await downloadFile(
"https://docs.flutter.dev/assets/images/dash/early-dash-sketches2.jpg",
normalJPEG2,
);
await downloadFile(
"https://docs.flutter.dev/assets/images/dash/early-dash-sketches5.jpg",
noExtensionJPEG,
);
await downloadFile(
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
pdfDocument,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter share tests',
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Flutter Share Tests"),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: const Text("Share plain text"),
onPressed: () async {
await sharePlainText();
},
),
ElevatedButton(
child: const Text("Share single image"),
onPressed: () async {
await shareSingleImage();
},
),
ElevatedButton(
child: const Text("Share multiple images"),
onPressed: () async {
await shareMultipleImages();
},
),
ElevatedButton(
child: const Text("Share without extension"),
onPressed: () async {
await shareNoExtension();
},
),
ElevatedButton(
child: const Text("Share PDF document"),
onPressed: () async {
await sharePdfDocument();
},
),
],
),
),
);
}
}
Future<File> downloadFile(String url, String filename) async {
var httpClient = HttpClient();
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getTemporaryDirectory()).path;
File file = File('$dir/$filename');
await file.create(recursive: true);
await file.writeAsBytes(bytes);
return file;
}
void printResult(ShareResult result) {
print('----- RESULT -----');
print('Status: ${result.status}');
print('Raw: ${result.raw}');
print('------------------');
}
Future<void> shareFilesWithResult(List<String> paths, {List<String> mimeTypes = const []}) async {
final res = await Share.shareFilesWithResult(paths, mimeTypes: mimeTypes);
printResult(res);
}
//* Plaintext
Future<void> sharePlainText() async {
const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eget ante molestie, euismod felis et.";
const subject = "Vestibulum velit magna, malesuada eu. ";
final res = await Share.shareWithResult(text, subject: subject);
printResult(res);
}
//* Single image
Future<void> shareSingleImage() async {
Directory cacheDir = await getTemporaryDirectory();
await shareFilesWithResult([
"${cacheDir.path}/$normalJPEG1",
]);
}
//* Multiple images
// Yes, the preview stacks the two previews - look closely
Future<void> shareMultipleImages() async {
Directory cacheDir = await getTemporaryDirectory();
await shareFilesWithResult([
"${cacheDir.path}/$normalJPEG1",
"${cacheDir.path}/$normalJPEG2",
]);
}
//* No Extension
// If you comment out the `mimeTypes` no preview picture will be shown
Future<void> shareNoExtension() async {
Directory cacheDir = await getTemporaryDirectory();
await shareFilesWithResult([
"${cacheDir.path}/$noExtensionJPEG",
], mimeTypes: [
"image/JPEG"
]);
}
//* PDF document (no image)
Future<void> sharePdfDocument() async {
Directory cacheDir = await getTemporaryDirectory();
await shareFilesWithResult([
"${cacheDir.path}/$pdfDocument",
]);
}