image_select_ohos_util.dart
3.49 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
import 'package:flutter/material.dart';
import 'package:image_picker_ohos/image_picker_ohos.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
class ImageSelectOhosUtil{
static Future<List<XFile>> onImageButtonPressed(ImageSource source,
{required BuildContext context,
bool isMultiImage = false,
bool isMedia = false,
bool isVideo = false,
int limit = 9}) async {
// if (_controller != null) {
// await _controller!.setVolume(0.0);
// }
final ImagePickerPlatform _picker = new ImagePickerOhos();
if (context.mounted) {
if (isVideo) {
List<XFile> temp = [];
final XFile? file = await _picker.getVideo(
source: source, maxDuration: const Duration(seconds: 10));
if (file != null && context.mounted) {
// _showPickedSnackBar(context, <XFile>[file]);
temp.add(file as XFile);
}
return temp;
} else if (isMultiImage) {
double? maxWidth = 300.00;
double? maxHeight = 500;
int? quality = 90;
try {
final ImageOptions imageOptions = ImageOptions(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
);
final List<XFile> pickedFileList = isMedia
? await _picker.getMedia(
options: MediaOptions(
allowMultiple: isMultiImage,
imageOptions: imageOptions,
limit: limit),
)
: await _picker.getMultiImageWithOptions(
options: MultiImagePickerOptions(
imageOptions: imageOptions, limit: limit),
);
if (pickedFileList.isNotEmpty && context.mounted) {
return pickedFileList;
} else {
return [];
}
} catch (e) {
print(e.toString());
}
} else if (isMedia) {
// double? maxWidth = 300.00;
// double? maxHeight = 500;
// int? quality = 90;
try {
final List<XFile> pickedFileList = <XFile>[];
final XFile? media = firstOrNull(await _picker.getMedia(
options: MediaOptions(
allowMultiple: isMultiImage,
// imageOptions: ImageOptions(
// maxWidth: maxWidth,
// maxHeight: maxHeight,
// imageQuality: quality,
// )
),
));
if (media != null) {
pickedFileList.add(media);
print(pickedFileList);
return pickedFileList;
}
} catch (e) {
print(e.toString());
}
} else {
double? maxWidth = 300.00;
double? maxHeight = 500;
int? quality = 90;
try {
final XFile? pickedFile = await _picker.getImageFromSource(
source: source,
options: ImagePickerOptions(
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: quality,
),
);
List<XFile> temp = [];
if (pickedFile != null && context.mounted) {
temp.add(pickedFile as XFile);
}
print(pickedFile);
return temp;
// <XFile>[pickedFile]
} catch (e) {
print(e);
}
}
}
return [];
}
static T? firstOrNull<T>(List<T> list) {
return list.isEmpty ? null : list.first;
}
}