opt: pick images

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2024-11-10 15:19:09 +08:00
parent 59910e275e
commit 8919d82913
5 changed files with 190 additions and 169 deletions

View File

@@ -550,30 +550,34 @@ class _CreatePanelState extends State<CreatePanel> {
EasyThrottle.throttle( EasyThrottle.throttle(
'imagePicker', const Duration(milliseconds: 500), 'imagePicker', const Duration(milliseconds: 500),
() async { () async {
List<XFile> pickedFiles = try {
await _imagePicker.pickMultiImage( List<XFile> pickedFiles =
limit: _limit, await _imagePicker.pickMultiImage(
imageQuality: 100, limit: _limit,
); imageQuality: 100,
if (pickedFiles.isNotEmpty) { );
for (int i = 0; i < pickedFiles.length; i++) { if (pickedFiles.isNotEmpty) {
if (_pathList.length == _limit) { for (int i = 0; i < pickedFiles.length; i++) {
SmartDialog.showToast('最多选择$_limit张图片'); if (_pathList.length == _limit) {
if (i != 0) { SmartDialog.showToast('最多选择$_limit张图片');
_pathStream.add(_pathList); if (i != 0) {
} _pathStream.add(_pathList);
break; }
} else { break;
_pathList.add(pickedFiles[i].path); } else {
if (i == pickedFiles.length - 1) { _pathList.add(pickedFiles[i].path);
_pathStream.add(_pathList); if (i == pickedFiles.length - 1) {
_pathStream.add(_pathList);
}
} }
} }
if (_pathList.isNotEmpty && !_isEnable) {
_isEnable = true;
_isEnableStream.add(true);
}
} }
if (_pathList.isNotEmpty && !_isEnable) { } catch (e) {
_isEnable = true; SmartDialog.showToast(e.toString());
_isEnableStream.add(true);
}
} }
}); });
}, },

View File

@@ -448,67 +448,71 @@ class _EditProfilePageState extends State<EditProfilePage> {
} }
void _pickImg() async { void _pickImg() async {
XFile? pickedFile = await _imagePicker.pickImage( try {
source: ImageSource.gallery, XFile? pickedFile = await _imagePicker.pickImage(
imageQuality: 100, source: ImageSource.gallery,
); imageQuality: 100,
if (pickedFile != null && mounted) {
String? mimeType =
lookupMimeType(pickedFile.path)?.split('/').getOrNull(1);
if (mimeType == 'gif') {
SmartDialog.showToast('不能选GIF');
return;
}
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: pickedFile.path,
uiSettings: [
AndroidUiSettings(
toolbarTitle: '裁剪',
toolbarColor: Theme.of(context).colorScheme.primary,
toolbarWidgetColor: Colors.white,
aspectRatioPresets: [
CropAspectRatioPresetCustom(),
],
lockAspectRatio: true,
hideBottomControls: true,
cropStyle: CropStyle.circle,
initAspectRatio: CropAspectRatioPresetCustom(),
),
IOSUiSettings(
title: '裁剪',
aspectRatioPresets: [
CropAspectRatioPresetCustom(),
],
cropStyle: CropStyle.circle,
aspectRatioLockEnabled: true,
resetAspectRatioEnabled: false,
aspectRatioPickerButtonHidden: true,
),
],
); );
if (croppedFile != null) { if (pickedFile != null && mounted) {
Request() String? mimeType =
.post( lookupMimeType(pickedFile.path)?.split('/').getOrNull(1);
'/x/member/web/face/update', if (mimeType == 'gif') {
queryParameters: { SmartDialog.showToast('不能选GIF');
'csrf': await Request.getCsrf(), return;
}, }
data: FormData.fromMap({ CroppedFile? croppedFile = await ImageCropper().cropImage(
'dopost': 'save', sourcePath: pickedFile.path,
'DisplayRank': 10000, uiSettings: [
'face': await MultipartFile.fromFile(croppedFile.path), AndroidUiSettings(
}), toolbarTitle: '裁剪',
) toolbarColor: Theme.of(context).colorScheme.primary,
.then((data) { toolbarWidgetColor: Colors.white,
if (data.data['code'] == 0) { aspectRatioPresets: [
(_loadingState as Success).response['face'] = data.data['data']; CropAspectRatioPresetCustom(),
SmartDialog.showToast('修改成功'); ],
setState(() {}); lockAspectRatio: true,
} else { hideBottomControls: true,
SmartDialog.showToast(data.data['message']); cropStyle: CropStyle.circle,
} initAspectRatio: CropAspectRatioPresetCustom(),
}); ),
IOSUiSettings(
title: '裁剪',
aspectRatioPresets: [
CropAspectRatioPresetCustom(),
],
cropStyle: CropStyle.circle,
aspectRatioLockEnabled: true,
resetAspectRatioEnabled: false,
aspectRatioPickerButtonHidden: true,
),
],
);
if (croppedFile != null) {
Request()
.post(
'/x/member/web/face/update',
queryParameters: {
'csrf': await Request.getCsrf(),
},
data: FormData.fromMap({
'dopost': 'save',
'DisplayRank': 10000,
'face': await MultipartFile.fromFile(croppedFile.path),
}),
)
.then((data) {
if (data.data['code'] == 0) {
(_loadingState as Success).response['face'] = data.data['data'];
SmartDialog.showToast('修改成功');
setState(() {});
} else {
SmartDialog.showToast(data.data['message']);
}
});
}
} }
} catch (e) {
SmartDialog.showToast(e.toString());
} }
} }
} }

View File

@@ -112,50 +112,54 @@ class _CreateFavPageState extends State<CreateFavPage> {
} }
void _pickImg() async { void _pickImg() async {
XFile? pickedFile = await _imagePicker.pickImage( try {
source: ImageSource.gallery, XFile? pickedFile = await _imagePicker.pickImage(
imageQuality: 100, source: ImageSource.gallery,
); imageQuality: 100,
if (pickedFile != null && mounted) {
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: pickedFile.path,
uiSettings: [
AndroidUiSettings(
toolbarTitle: '裁剪',
toolbarColor: Theme.of(context).colorScheme.primary,
toolbarWidgetColor: Colors.white,
aspectRatioPresets: [
CropAspectRatioPreset.ratio16x9,
],
lockAspectRatio: true,
hideBottomControls: true,
initAspectRatio: CropAspectRatioPreset.ratio16x9,
),
IOSUiSettings(
title: '裁剪',
aspectRatioPresets: [
CropAspectRatioPreset.ratio16x9,
],
aspectRatioLockEnabled: true,
resetAspectRatioEnabled: false,
aspectRatioPickerButtonHidden: true,
),
],
); );
if (croppedFile != null) { if (pickedFile != null && mounted) {
MsgHttp.uploadImage( CroppedFile? croppedFile = await ImageCropper().cropImage(
path: croppedFile.path, sourcePath: pickedFile.path,
bucket: 'medialist', uiSettings: [
dir: 'cover', AndroidUiSettings(
).then((data) { toolbarTitle: '裁剪',
if (data['status']) { toolbarColor: Theme.of(context).colorScheme.primary,
_cover = data['data']['location']; toolbarWidgetColor: Colors.white,
setState(() {}); aspectRatioPresets: [
} else { CropAspectRatioPreset.ratio16x9,
SmartDialog.showToast(data['msg']); ],
} lockAspectRatio: true,
}); hideBottomControls: true,
initAspectRatio: CropAspectRatioPreset.ratio16x9,
),
IOSUiSettings(
title: '裁剪',
aspectRatioPresets: [
CropAspectRatioPreset.ratio16x9,
],
aspectRatioLockEnabled: true,
resetAspectRatioEnabled: false,
aspectRatioPickerButtonHidden: true,
),
],
);
if (croppedFile != null) {
MsgHttp.uploadImage(
path: croppedFile.path,
bucket: 'medialist',
dir: 'cover',
).then((data) {
if (data['status']) {
_cover = data['data']['location'];
setState(() {});
} else {
SmartDialog.showToast(data['msg']);
}
});
}
} }
} catch (e) {
SmartDialog.showToast(e.toString());
} }
} }

View File

@@ -361,27 +361,31 @@ class _ReplyPageState extends State<ReplyPage>
EasyThrottle.throttle( EasyThrottle.throttle(
'imagePicker', const Duration(milliseconds: 500), 'imagePicker', const Duration(milliseconds: 500),
() async { () async {
List<XFile> pickedFiles = try {
await _imagePicker.pickMultiImage( List<XFile> pickedFiles =
limit: _limit, await _imagePicker.pickMultiImage(
imageQuality: 100, limit: _limit,
); imageQuality: 100,
if (pickedFiles.isNotEmpty) { );
for (int i = 0; i < pickedFiles.length; i++) { if (pickedFiles.isNotEmpty) {
if (_pathList.length == _limit) { for (int i = 0; i < pickedFiles.length; i++) {
SmartDialog.showToast('最多选择$_limit张图片'); if (_pathList.length == _limit) {
if (i != 0) { SmartDialog.showToast('最多选择$_limit张图片');
_pathStream.add(_pathList); if (i != 0) {
} _pathStream.add(_pathList);
break; }
} else { break;
_pathList.add(pickedFiles[i].path); } else {
if (i == pickedFiles.length - 1) { _pathList.add(pickedFiles[i].path);
SmartDialog.dismiss(); if (i == pickedFiles.length - 1) {
_pathStream.add(_pathList); SmartDialog.dismiss();
_pathStream.add(_pathList);
}
} }
} }
} }
} catch (e) {
SmartDialog.showToast(e.toString());
} }
}); });
}, },

View File

@@ -310,37 +310,42 @@ class _WhisperDetailPageState extends State<WhisperDetailPage> {
if (snapshot.data == true) { if (snapshot.data == true) {
_whisperDetailController.sendMsg(); _whisperDetailController.sendMsg();
} else { } else {
XFile? pickedFile = await _imagePicker.pickImage( try {
source: ImageSource.gallery, XFile? pickedFile = await _imagePicker.pickImage(
imageQuality: 100, source: ImageSource.gallery,
); imageQuality: 100,
if (pickedFile != null) {
SmartDialog.showLoading(msg: '正在上传图片');
dynamic result = await MsgHttp.uploadBfs(
path: pickedFile.path,
biz: 'im',
); );
if (result['status']) { if (pickedFile != null) {
int imageSize = await File(pickedFile.path).length(); SmartDialog.showLoading(msg: '正在上传图片');
String mimeType = lookupMimeType(pickedFile.path) dynamic result = await MsgHttp.uploadBfs(
?.split('/') path: pickedFile.path,
.getOrNull(1) ?? biz: 'im',
'png'; );
dynamic picMsg = { if (result['status']) {
'url': result['data']['image_url'], int imageSize = await File(pickedFile.path).length();
'height': result['data']['image_height'], String mimeType = lookupMimeType(pickedFile.path)
'width': result['data']['image_width'], ?.split('/')
'imageType': mimeType, .getOrNull(1) ??
'original': 1, 'png';
'size': imageSize / 1024, dynamic picMsg = {
}; 'url': result['data']['image_url'],
SmartDialog.showLoading(msg: '正在发送'); 'height': result['data']['image_height'],
await _whisperDetailController.sendMsg(picMsg: picMsg); 'width': result['data']['image_width'],
} else { 'imageType': mimeType,
SmartDialog.dismiss(); 'original': 1,
SmartDialog.showToast(result['msg']); 'size': imageSize / 1024,
return; };
SmartDialog.showLoading(msg: '正在发送');
await _whisperDetailController.sendMsg(
picMsg: picMsg);
} else {
SmartDialog.dismiss();
SmartDialog.showToast(result['msg']);
return;
}
} }
} catch (e) {
SmartDialog.showToast(e.toString());
} }
} }
}, },