toSelectPic static method
Future<void>
toSelectPic(
- BuildContext context, {
- String language = "zh",
- int maxAssetsCount = 1,
- AssetPickerConfig? otherConfig,
- List<
AssetEntity> ? selectedAssets, - int gridCount = 4,
- String? themeColor,
- bool needCamera = true,
- bool isDrag2Select = false,
- Function? selectCallback,
- RequestType? requestType,
- int maximumRecordingDuration = 15,
- int minimumRecordingDuration = 2,
Implementation
static Future<void> toSelectPic(BuildContext context,
{String language = "zh",
int maxAssetsCount = 1,
AssetPickerConfig? otherConfig,
List<AssetEntity>? selectedAssets,
int gridCount = 4,
String? themeColor,
bool needCamera = true,
bool isDrag2Select = false,
Function? selectCallback,
RequestType? requestType,
int maximumRecordingDuration = 15,
int minimumRecordingDuration = 2}) async {
// String language = ProSPUtils.getSystemLanguage();
//官网的context的方式太复杂了,所有改成传递把
AssetPickerTextDelegate textDelegate;
switch (language) {
case "zh":
case "zh_CN":
textDelegate = const AssetPickerTextDelegate();
break;
case "zh_Hant":
textDelegate = const TraditionalChineseAssetPickerTextDelegate();
break;
case "he":
textDelegate = const HebrewAssetPickerTextDelegate();
break;
case "de":
textDelegate = const GermanAssetPickerTextDelegate();
break;
case "ru":
textDelegate = const RussianAssetPickerTextDelegate();
break;
case "ar":
textDelegate = const ArabicAssetPickerTextDelegate();
break;
case "vi":
textDelegate = const VietnameseAssetPickerTextDelegate();
break;
case "tr":
textDelegate = const TurkishAssetPickerTextDelegate();
break;
case "ja":
textDelegate = const JapaneseAssetPickerTextDelegate();
break;
case "fr":
textDelegate = const FrenchAssetPickerTextDelegate();
break;
default:
textDelegate = const EnglishAssetPickerTextDelegate();
break;
}
List<AssetEntity>? assets = await AssetPicker.pickAssets(
context,
pickerConfig: AssetPickerConfig(
selectedAssets: selectedAssets,
maxAssets: maxAssetsCount,
requestType: requestType ?? RequestType.image,
gridCount: gridCount,
themeColor: themeColor == null ? null : ColorsUtil.hex2Color(themeColor),
pickerTheme: otherConfig?.pickerTheme,
textDelegate: textDelegate,
shouldAutoplayPreview: true,
/* selectPredicate: (BuildContext c, AssetEntity a, bool isSelected) {
debugPrint('Asset title: ${a.title}');
return a.title?.endsWith('.gif') != true;
},*/
filterOptions: FilterOptionGroup(
videoOption: FilterOption(
durationConstraint: DurationConstraint(max: Duration(seconds: maximumRecordingDuration), min: Duration(seconds: minimumRecordingDuration)),
),
),
// textDelegate: const AssetPickerTextDelegate(),
// specialItemPosition: needCamera ? SpecialItemPosition.prepend : SpecialItemPosition.none,
// specialItems:!needCamera? []:[_createCamera(context, textDelegate, language: language,
// maximumRecordingDuration: maximumRecordingDuration,
// enableRecording: [RequestType.common, RequestType.video].contains(requestType))
// ],
// needCamera 为 false 时不加相机入口,避免 isNeedCamera: false 不生效(如签名页只需选图不需拍照)
specialItems: !needCamera
? []
: [
SpecialItem(
position: SpecialItemPosition.prepend,
builder: (
BuildContext context,
AssetPathEntity? path,
PermissionState permissionState,
) {
if (path?.isAll != true) {
return null;
}
return Semantics(
label: textDelegate.sActionUseCameraHint,
button: true,
onTapHint: textDelegate.sActionUseCameraHint,
child: _createCamera(context, textDelegate, language: language, maximumRecordingDuration: maximumRecordingDuration, enableRecording: [RequestType.common, RequestType.video].contains(requestType)));
},
),
],
dragToSelect: isDrag2Select,
/* specialItemBuilder: !needCamera
? null
: (BuildContext context,
AssetPathEntity? path,
int length,) {
if (path?.isAll != true) {
return null;
}
return _createCamera(context, textDelegate, language: language,
maximumRecordingDuration: maximumRecordingDuration,
enableRecording: [RequestType.common, RequestType.video].contains(requestType));
}*/
),
);
if (assets == null || assets.isEmpty) return;
/*
卡通项目中的处理heic 图片 暂时拿掉
AssetEntity _entity = assets[0];
File file = await _entity.file;
if (Platform.isIOS) {
String fileExtension = p.extension(file.path).replaceAll('.', '');
if (fileExtension.toLowerCase() == 'heic') {
print('convert to jpeg');
String jpegPath = await HeicToJpg.convert(file.path);
file = File(jpegPath);
}
}*/
// AssetEntity _entity = assets[0];
if (selectCallback != null) {
// selectCallback(file, _entity);
// selectCallback(null, _entity);
selectCallback(assets);
}
}