-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
292 lines (263 loc) · 8.93 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
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
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_picker_macos/image_picker_macos.dart';
import 'package:native_image_picker_macos/native_image_picker_macos.dart';
import 'package:video_player/video_player.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await NativeImagePickerMacOS.registerWithIfSupported();
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light(useMaterial3: true),
darkTheme: ThemeData.dark(useMaterial3: true),
themeMode: ThemeMode.system,
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Padding(
padding: EdgeInsets.all(64.0),
child: SingleChildScrollView(child: Buttons()),
),
),
),
);
}
}
class Buttons extends StatefulWidget {
const Buttons({super.key});
@override
State<Buttons> createState() => _ButtonsState();
}
class _ButtonsState extends State<Buttons> {
late bool _useMacOSNativePicker;
late Future<bool> _macOSNativePickerSupportedFuture;
final imagePicker = ImagePicker();
final TextEditingController _imageMaxWidthController =
TextEditingController();
final TextEditingController _imageMaxHeightController =
TextEditingController();
final TextEditingController _imageQualityController = TextEditingController();
final _mediaFiles = <XFile>[];
@override
void initState() {
super.initState();
_useMacOSNativePicker = NativeImagePickerMacOS.isRegistered();
_macOSNativePickerSupportedFuture = NativeImagePickerMacOS.isSupported();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
spacing: 12,
children: [
Form(
autovalidateMode: AutovalidateMode.always,
child: Column(
spacing: 8,
children: [
if (_useMacOSNativePicker)
ElevatedButton.icon(
label: Text('Open Photos App'),
icon: Icon(Icons.photo_album),
// Using instanceOrThrow since we assume the current instance
// is NativeImagePickerMacOS due to _useMacOSNativePicker check.
onPressed: () =>
NativeImagePickerMacOS.instanceOrThrow.openPhotosApp(),
),
TextFormField(
controller: _imageMaxWidthController,
decoration: InputDecoration(labelText: 'Image Max Width'),
textInputAction: TextInputAction.next,
validator: validateNumber,
),
TextFormField(
controller: _imageMaxHeightController,
decoration: InputDecoration(labelText: 'Image Max Height'),
textInputAction: TextInputAction.next,
validator: validateNumber,
),
TextFormField(
controller: _imageQualityController,
decoration: InputDecoration(labelText: 'Image Quality'),
textInputAction: TextInputAction.next,
validator: validateNumber,
),
],
),
),
ElevatedButton.icon(
icon: Icon(Icons.photo),
label: Text('Pick Image'),
onPressed: () async {
final imageFile = await imagePicker.pickImage(
source: ImageSource.gallery,
imageQuality: int.tryParse(_imageQualityController.text) ?? 100,
maxWidth: double.tryParse(_imageMaxWidthController.text),
maxHeight: double.tryParse(_imageMaxHeightController.text),
);
_setMediaFileListFromFile(imageFile);
},
),
ElevatedButton.icon(
icon: Icon(Icons.photo_library),
label: Text('Pick Images'),
onPressed: () async {
final imageFiles = await imagePicker.pickMultiImage(
imageQuality: int.tryParse(_imageQualityController.text) ?? 100,
maxWidth: double.tryParse(_imageMaxWidthController.text),
maxHeight: double.tryParse(_imageMaxHeightController.text),
);
_setMediaFileListFromFiles(imageFiles);
},
),
ElevatedButton.icon(
icon: Icon(Icons.movie),
label: Text('Pick Video'),
onPressed: () async {
final videoFile = await imagePicker.pickVideo(
source: ImageSource.gallery,
);
_setMediaFileListFromFile(videoFile);
},
),
ElevatedButton.icon(
icon: Icon(Icons.perm_media),
label: Text('Pick Media'),
onPressed: () async {
final mediaFiles = await imagePicker.pickMultipleMedia(
imageQuality: int.tryParse(_imageQualityController.text) ?? 100,
maxWidth: double.tryParse(_imageMaxWidthController.text),
maxHeight: double.tryParse(_imageMaxHeightController.text),
);
_setMediaFileListFromFiles(mediaFiles);
},
),
FutureBuilder(
future: _macOSNativePickerSupportedFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator.adaptive();
}
if (snapshot.hasError) {
return Text(
'Error while checking whether the native macOS picker is supported: ${snapshot.error.toString()}',
);
}
final bool nativeMacOSPickerSupported = snapshot.requireData;
if (nativeMacOSPickerSupported) {
return SwitchListTile.adaptive(
title: Text('Use native macOS picker'),
value: _useMacOSNativePicker,
onChanged: (bool value) {
if (value) {
NativeImagePickerMacOS.registerWith();
} else {
ImagePickerMacOS.registerWith();
}
setState(() => _useMacOSNativePicker = value);
},
);
} else {
return Text(
'The current macOS version does not supports PHPicker.');
}
},
),
..._mediaFiles.nonNulls.map(
(e) => isImage(e.path)
? Image.file(File(e.path))
: _buildInlineVideoPlayer(e.path),
)
],
);
}
void _setMediaFileListFromFile(XFile? file) {
setState(() {
_mediaFiles.clear();
if (file != null) {
_mediaFiles.add(file);
}
});
}
void _setMediaFileListFromFiles(List<XFile> files) {
setState(() {
_mediaFiles.clear();
_mediaFiles.addAll(files);
});
}
Widget _buildInlineVideoPlayer(String videoFilePath) {
final VideoPlayerController controller =
VideoPlayerController.file(File(videoFilePath));
const double volume = 1.0;
controller.setVolume(volume);
controller.initialize();
controller.setLooping(true);
controller.play();
return Center(child: AspectRatioVideo(controller));
}
String? validateNumber(String? value) {
if (value == null || value.trim().isEmpty) {
return null;
}
return int.tryParse(value) == null ? 'Invalid number.' : null;
}
bool isImage(String filePath) {
final ext = filePath.split('.').last.toLowerCase();
return {'jpg', 'jpeg', 'png', 'gif', 'bmp'}.contains(ext);
}
@override
void dispose() {
_imageMaxWidthController.dispose();
_imageMaxHeightController.dispose();
_imageQualityController.dispose();
super.dispose();
}
}
class AspectRatioVideo extends StatefulWidget {
const AspectRatioVideo(this.controller, {super.key});
final VideoPlayerController? controller;
@override
AspectRatioVideoState createState() => AspectRatioVideoState();
}
class AspectRatioVideoState extends State<AspectRatioVideo> {
VideoPlayerController? get controller => widget.controller;
bool initialized = false;
void _onVideoControllerUpdate() {
if (!mounted) {
return;
}
if (initialized != controller!.value.isInitialized) {
initialized = controller!.value.isInitialized;
setState(() {});
}
}
@override
void initState() {
super.initState();
controller!.addListener(_onVideoControllerUpdate);
}
@override
void dispose() {
controller!.removeListener(_onVideoControllerUpdate);
super.dispose();
}
@override
Widget build(BuildContext context) {
if (initialized) {
return Center(
child: AspectRatio(
aspectRatio: controller!.value.aspectRatio,
child: VideoPlayer(controller!),
),
);
} else {
return CircularProgressIndicator.adaptive();
}
}
}