UI
截屏2022-10-27 10.54.45.png
代码
wechat_assets_picker: ^7.1.2
wechat_camera_picker: ^3.1.0
///图片选择
class PictureSelection {
///获取照片,让用户选择来着哪里
static Future<List<AssetEntity>?> getPictureSwitch(
{AssetPickerConfig? assetPickerConfig,
CameraPickerConfig? cameraPickerConfig}) async {
int switchNum = await _getSwitch();
if (switchNum == 0) {
//拍摄照片
return getPictureWithCamera(cameraPickerConfig: cameraPickerConfig);
} else if (switchNum == 1) {
//相册选择
return getPictureWithAlbum(assetPickerConfig: assetPickerConfig);
} else {
//用户取消
return [];
}
}
///相册获取照片
static Future<List<AssetEntity>?> getPictureWithAlbum(
{AssetPickerConfig? assetPickerConfig}) async {
try {
if (await PHUS.storage && await PHUS.photos) {
return AssetPicker.pickAssets(
Get.context!,
pickerConfig: assetPickerConfig ??
const AssetPickerConfig(
themeColor: TCS.blue00b6,
requestType: RequestType.image,
textDelegate: AssetPickerTextDelegate(),
),
);
}
} catch (e) {
ET.err("获取失败,请检查是否授予权限");
}
return null;
}
///相机获取照片
static Future<List<AssetEntity>?> getPictureWithCamera(
{CameraPickerConfig? cameraPickerConfig}) async {
AssetEntity? assetEntity;
try {
if (await PHUS.location &&
await PHUS.photos &&
await PHUS.storage &&
await PHUS.camera) {
assetEntity = await CameraPicker.pickFromCamera(
Get.context!,
pickerConfig: cameraPickerConfig ?? const CameraPickerConfig(),
);
}
} catch (e) {
ET.err("获取失败,请检查是否授予权限");
}
return ObjectUtil.isNotEmpty(assetEntity) ? [assetEntity!] : [];
}
///选择相机拍摄,还是在相册选择提示框
static Future<int> _getSwitch() async {
return await Get.bottomSheet(
Container(
color: Colors.white,
child: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Material(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.r),
topRight: Radius.circular(15.r)),
color: Colors.white,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.r),
topRight: Radius.circular(15.r)),
),
child: Column(
children: [
InkWell(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.r),
topRight: Radius.circular(15.r)),
onTap: () {
Get.back(result: 0);
},
child: Container(
alignment: Alignment.center,
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 15.r),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 0.8.r,
color: const Color(0xffF2F2F2)))),
child: TU.d18("拍一张", bold: true),
),
),
InkWell(
onTap: () {
Get.back(result: 1);
},
child: Container(
alignment: Alignment.center,
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 15.r),
child: TU.d18("从相册选择", bold: true),
),
),
Container(
color: const Color(0xffF2F2F2),
height: 8.r,
),
InkWell(
onTap: () {
Get.back(result: -1);
},
child: Container(
alignment: Alignment.center,
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 15.r),
child: TU.d18(
"取消",
),
),
),
],
),
),
)
],
),
),
),
isDismissible: false) ??
-1;
}
///获取一张照片路径
///isCutting 是否裁剪
///aspectRatio 裁剪比例
static Future<String> getCutOneImage(
{isCutting = true, aspectRatio = 1 / 1}) async {
List<AssetEntity?>? list = [];
try {
list = await PictureSelection.getPictureSwitch(
assetPickerConfig: const AssetPickerConfig(
themeColor: TCS.blue00b6,
requestType: RequestType.image,
maxAssets: 1,
textDelegate: AssetPickerTextDelegate()));
} catch (e) {
ET.err("获取失败,请检查是否授予权限");
}
if (list != null && list.isNotEmpty) {
File? file = await list[0]?.originFile;
if (file == null) {
ET.err("获取图片失败,请重试");
return '';
}
File? editedImage = file;
///是否进行
if (isCutting) {
editedImage = await Get.to(() => ImageCurring(
image: file,
aspectRatio: aspectRatio,
));
if (editedImage != null) {
return editedImage.path;
} else {
return '';
}
}
return editedImage.path;
} else {
return '';
}
}
}