mobile_skt/lib/util/photo_picker_util.dart

45 lines
1.6 KiB
Dart
Raw Normal View History

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
2024-04-12 09:56:00 +08:00
import 'package:sk_base_mobile/router/router.util.dart';
import 'package:sk_base_mobile/util/media_util.dart';
import 'package:sk_base_mobile/app_theme.dart';
class PhotoPickerUtil {
2024-03-21 14:09:49 +08:00
showPicker({Function? callback, String title = '请选择上传方式'}) async {
return showCupertinoModalPopup(
context: Get.context!,
builder: (BuildContext ctx) {
return CupertinoActionSheet(
2024-03-21 14:09:49 +08:00
title: Text(title),
cancelButton: CupertinoActionSheetAction(
onPressed: () {
2024-04-12 09:56:00 +08:00
RouterUtil.back();
},
child: const Text(
2024-03-21 14:09:49 +08:00
'取消',
style: TextStyle(color: Color(0xffcdcdcd)),
)),
2024-03-21 14:09:49 +08:00
actions: ['拍照', '相册']
.map((item) => CupertinoActionSheetAction(
onPressed: () async {
2024-04-12 09:56:00 +08:00
await RouterUtil.back();
2024-03-21 14:09:49 +08:00
XFile? pickedFile = item == '拍照'
? await MediaUtil().getImageFromCamera()
: await MediaUtil().getImageFromGallery();
if (pickedFile != null) {
if (callback != null) {
callback(pickedFile);
}
}
},
child: Text(
item,
style: const TextStyle(color: AppTheme.primaryColor),
)))
.toList(),
);
});
}
}