UI
截屏2022-10-27 10.54.45.png
代碼
wechat_assets_picker: ^7.1.2
wechat_camera_picker: ^3.1.0
///圖片選擇
class PictureSelection {
///獲取照片,讓用戶(hù)選擇來(lái)著哪里
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) {
//相冊(cè)選擇
return getPictureWithAlbum(assetPickerConfig: assetPickerConfig);
} else {
//用戶(hù)取消
return [];
}
}
///相冊(cè)獲取照片
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("獲取失敗惫谤,請(qǐng)檢查是否授予權(quán)限");
}
return null;
}
///相機(jī)獲取照片
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("獲取失敗汗侵,請(qǐng)檢查是否授予權(quán)限");
}
return ObjectUtil.isNotEmpty(assetEntity) ? [assetEntity!] : [];
}
///選擇相機(jī)拍攝,還是在相冊(cè)選擇提示框
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("從相冊(cè)選擇", 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("獲取失敗,請(qǐng)檢查是否授予權(quán)限");
}
if (list != null && list.isNotEmpty) {
File? file = await list[0]?.originFile;
if (file == null) {
ET.err("獲取圖片失敗让歼,請(qǐng)重試");
return '';
}
File? editedImage = file;
///是否進(jìn)行
if (isCutting) {
editedImage = await Get.to(() => ImageCurring(
image: file,
aspectRatio: aspectRatio,
));
if (editedImage != null) {
return editedImage.path;
} else {
return '';
}
}
return editedImage.path;
} else {
return '';
}
}
}