-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcamera_picker.dart
407 lines (366 loc) · 16.6 KB
/
camera_picker.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
library camera_picker;
import 'dart:async';
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:camera_picker/src/picker_store.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
export 'package:cross_file/cross_file.dart';
const _defaultPreviewHeight = 60.0;
const _defaultPreviewWidth = 80.0;
/// A CameraPicker.
class CameraPicker extends HookWidget {
/// Error callback when an error is throw on takePicture camera
final Function(dynamic error, dynamic stack)? onError;
/// Resolution preset of the camera
final ResolutionPreset resolutionPreset;
/// Max number of picture allowed, used to enable the continue button
final int? maxPicture;
/// Min number of picture allowed, used to enable the continue button
final int minPicture;
/// Show or not the cancel button
final bool showCancelButton;
/// Show or not the torch button
final bool showTorchButton;
/// Show or not the switch camera button
final bool showSwitchCameraButton;
/// Color to use for icons
final Color iconColor;
/// Height of the preview, default to 60
final double previewHeight;
/// Widget of the preview, default to 80
final double previewWidth;
/// Callback when an existing picture is asked to be delete, return true or false to continue deletion
final FutureOr<bool> Function(XFile file)? onDelete;
/// Initial selection of images to put in the preview
final List<XFile>? initialFiles;
/// Custom builder to show "no camera" widget
final WidgetBuilder? noCameraBuilder;
const CameraPicker({
Key? key,
this.initialFiles,
this.previewHeight = _defaultPreviewHeight,
this.previewWidth = _defaultPreviewWidth,
this.noCameraBuilder,
this.showSwitchCameraButton = true,
this.onDelete,
this.resolutionPreset = ResolutionPreset.high,
this.iconColor = Colors.white,
this.showTorchButton = true,
this.showCancelButton = true,
this.onError,
this.maxPicture,
this.minPicture = 1,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final store = useMemoized(() => PickerStore(filesData: List.from(initialFiles ?? []), minPicture: minPicture, maxPicture: maxPicture));
final availableCamerasFuture = useMemoized(() => availableCameras());
final cameras = useState<List<CameraDescription>?>(null);
return Material(
child: SizedBox(
width: double.infinity,
height: double.infinity,
child: ColoredBox(
color: Colors.black,
child: FutureBuilder<List<CameraDescription>>(
builder: (context, snapshot) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (snapshot.connectionState == ConnectionState.done) {
cameras.value ??= snapshot.data ?? [];
}
});
if (snapshot.connectionState == ConnectionState.waiting || cameras.value == null) {
return const Center(child: CircularProgressIndicator());
}
if (cameras.value!.isEmpty) {
return noCameraBuilder?.call(context) ??
Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'No camera available',
style: TextStyle(color: Theme.of(context).errorColor),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Back'))
],
),
);
}
return HookBuilder(builder: (context) {
final cameraControllerState = useState(CameraController(
cameras.value!.firstWhereOrNull((element) => element.lensDirection == CameraLensDirection.back) ?? cameras.value!.first,
resolutionPreset,
enableAudio: false,
));
final isBackCamera = useState(true);
final cameraController = cameraControllerState.value;
final initializeCamera = useMemoized(() => cameraController.initialize(), [cameraController]);
return WillPopScope(
onWillPop: () async {
cameraController.dispose();
return true;
},
child: FutureBuilder(
future: initializeCamera,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
return CameraPreview(
cameraController,
key: Key(cameraController.description.name),
child: SafeArea(
child: Stack(
fit: StackFit.expand,
children: [
if (showTorchButton && isBackCamera.value)
Positioned(
top: 10,
right: 10,
child: HookBuilder(builder: (context) {
final mode = useState(FlashMode.auto);
return IconButton(
onPressed: () {
if (mode.value == FlashMode.auto) {
mode.value = FlashMode.torch;
cameraController.setFlashMode(FlashMode.torch);
} else {
mode.value = FlashMode.auto;
cameraController.setFlashMode(FlashMode.auto);
}
},
icon: Icon(mode.value == FlashMode.auto ? Icons.flashlight_on_outlined : Icons.flashlight_on),
color: iconColor,
);
}),
),
if (showSwitchCameraButton && cameras.value!.length > 1)
Positioned(
top: 10,
left: 10,
child: IconButton(
onPressed: () {
if (isBackCamera.value) {
cameraControllerState.value = CameraController(
cameras.value!.firstWhereOrNull((element) => element.lensDirection == CameraLensDirection.front) ??
cameras.value!.last,
resolutionPreset,
enableAudio: false,
);
} else {
cameraControllerState.value = CameraController(
cameras.value!.firstWhereOrNull((element) => element.lensDirection == CameraLensDirection.back) ??
cameras.value!.first,
resolutionPreset,
enableAudio: false,
);
}
isBackCamera.value = !isBackCamera.value;
},
icon: Icon(isBackCamera.value ? Icons.camera_front_outlined : Icons.camera_rear_outlined),
color: iconColor,
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 12),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
HookBuilder(builder: (context) {
useListenable(store);
return ImagesPreview(
files: store.filesData,
iconColor: iconColor,
borderColor: iconColor,
previewWidth: previewWidth,
previewHeight: previewHeight,
onDelete: (index) async {
if (onDelete == null || await onDelete!(store.filesData[index])) {
store.removeFile(store.filesData[index]);
}
},
);
}),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (showCancelButton)
IconButton(
onPressed: () {
cameraController.dispose();
Navigator.of(context).pop();
},
tooltip: MaterialLocalizations.of(context).cancelButtonLabel,
color: iconColor,
enableFeedback: true,
icon: const Icon(Icons.close),
),
IconButton(
onPressed: () async {
try {
final file = await cameraController.takePicture();
store.addFile(file);
} catch (ex, stack) {
onError?.call(ex, stack);
}
},
enableFeedback: true,
color: iconColor,
iconSize: 40,
icon: const Icon(
Icons.photo_camera_outlined,
),
),
HookBuilder(builder: (context) {
useListenable(store);
return IconButton(
onPressed: store.canContinue
? () {
cameraController.dispose();
Navigator.of(context).pop(store.filesData);
}
: null,
enableFeedback: true,
tooltip: MaterialLocalizations.of(context).okButtonLabel,
icon: const Icon(Icons.check),
disabledColor: Colors.grey[600],
color: iconColor,
);
}),
],
),
],
),
),
],
),
),
);
}),
);
});
},
future: availableCamerasFuture,
),
),
),
);
}
}
/// ImagesPreview is a widget to show preview of files with
class ImagesPreview extends HookWidget {
/// Files to show in preview
final List<XFile> files;
/// Callback when delete button is pressed, but don't delete the file, that's on you to so it and update [files]
final Function(int index)? onDelete;
/// Height of the preview, default to 60
final double previewHeight;
/// Widget of the preview, default to 80
final double previewWidth;
/// Icon color
final Color iconColor;
/// Border color
final Color borderColor;
const ImagesPreview({
Key? key,
this.previewHeight = _defaultPreviewHeight,
this.previewWidth = _defaultPreviewWidth,
this.iconColor = Colors.white,
this.borderColor = Colors.white,
this.onDelete,
required this.files,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final ioFiles = useMemoized(() => files.map((e) => File(e.path)).toList(), [files, files.length]);
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
for (var i = 0; i < ioFiles.length; i++)
ImagePreview(
file: ioFiles[i],
previewHeight: previewHeight,
previewWidth: previewWidth,
borderColor: borderColor,
iconColor: iconColor,
onDelete: onDelete == null
? null
: () {
onDelete?.call(i);
},
),
],
),
);
}
}
/// Preview on one single image use in [ImagePreviews]
class ImagePreview extends StatelessWidget {
// File to preview
final File file;
/// Border color of the preview
final Color borderColor;
/// Icon color of the preview
final Color iconColor;
/// Delete button pressed callback, delegate the actual deletion to you
final VoidCallback? onDelete;
/// Height of the preview, default to 60
final double previewHeight;
/// Widget of the preview, default to 80
final double previewWidth;
const ImagePreview(
{Key? key,
this.previewHeight = 60,
this.previewWidth = 80,
this.onDelete,
required this.file,
this.iconColor = Colors.white,
this.borderColor = Colors.white})
: super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 2),
child: DecoratedBox(
decoration: BoxDecoration(border: Border.all(color: borderColor)),
child: Padding(
padding: const EdgeInsets.all(1),
child: Stack(
children: [
Image.file(
file,
height: previewHeight,
width: previewWidth,
fit: BoxFit.cover,
),
if (onDelete != null)
Positioned(
top: -10,
right: -10,
child: IconButton(
onPressed: onDelete,
color: iconColor,
iconSize: 18,
tooltip: MaterialLocalizations.of(context).deleteButtonTooltip,
icon: const Icon(Icons.cancel),
),
)
],
),
),
),
);
}
}