You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.8 KiB
131 lines
3.8 KiB
3 weeks ago
|
import 'package:flutter/material.dart';
|
||
|
import 'package:get/get.dart';
|
||
|
import 'package:image_picker/image_picker.dart';
|
||
|
import '../controllers/problem_controller.dart';
|
||
|
import 'package:permission_handler/permission_handler.dart';
|
||
|
|
||
|
class AddProblemController extends GetxController {
|
||
|
final ProblemController _problemController = Get.put(ProblemController());
|
||
|
|
||
|
final RxList<XFile> _selectedImages = <XFile>[].obs;
|
||
|
final TextEditingController descriptionController = TextEditingController();
|
||
|
final TextEditingController locationController = TextEditingController();
|
||
|
final RxBool isLoading = false.obs;
|
||
|
|
||
|
List<XFile> get selectedImages => _selectedImages;
|
||
|
|
||
|
// 改进的 pickImage 方法
|
||
|
Future<void> pickImage(ImageSource source) async {
|
||
|
try {
|
||
|
PermissionStatus status;
|
||
|
String permissionName;
|
||
|
String actionName;
|
||
|
|
||
|
if (source == ImageSource.camera) {
|
||
|
permissionName = "相机";
|
||
|
actionName = "拍照";
|
||
|
status = await Permission.camera.request();
|
||
|
} else {
|
||
|
permissionName = "相册";
|
||
|
actionName = "选择图片";
|
||
|
|
||
|
// 处理不同 Android 版本的相册权限
|
||
|
if (await Permission.photos.isGranted) {
|
||
|
status = PermissionStatus.granted;
|
||
|
} else if (await Permission.storage.isGranted) {
|
||
|
status = PermissionStatus.granted;
|
||
|
} else {
|
||
|
// 请求适当的权限
|
||
|
status = await Permission.photos.request();
|
||
|
|
||
|
// 如果 photos 权限被拒绝,尝试请求 storage 权限
|
||
|
if (status.isDenied || status.isPermanentlyDenied) {
|
||
|
status = await Permission.storage.request();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (status.isGranted) {
|
||
|
final ImagePicker picker = ImagePicker();
|
||
|
final XFile? image = await picker.pickImage(source: source);
|
||
|
|
||
|
if (image != null) {
|
||
|
_selectedImages.add(image);
|
||
|
}
|
||
|
} else if (status.isPermanentlyDenied) {
|
||
|
// 权限被永久拒绝,引导用户到设置
|
||
|
_showPermissionDeniedDialog(permissionName, actionName);
|
||
|
} else {
|
||
|
Get.snackbar('权限被拒绝', '需要$permissionName权限才能$actionName');
|
||
|
}
|
||
|
} catch (e) {
|
||
|
Get.snackbar('错误', '选择图片失败: $e');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 显示权限被拒绝的对话框
|
||
|
void _showPermissionDeniedDialog(String permissionName, String actionName) {
|
||
|
Get.dialog(
|
||
|
AlertDialog(
|
||
|
title: Text('权限被拒绝'),
|
||
|
content: Text('需要$permissionName权限才能$actionName。是否要前往设置开启权限?'),
|
||
|
actions: [
|
||
|
TextButton(onPressed: () => Get.back(), child: Text('取消')),
|
||
|
TextButton(
|
||
|
onPressed: () async {
|
||
|
Get.back();
|
||
|
await openAppSettings();
|
||
|
},
|
||
|
child: Text('去设置'),
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Future<void> removeImage(int index) async {
|
||
|
_selectedImages.removeAt(index);
|
||
|
}
|
||
|
|
||
|
bool get isFormValid {
|
||
|
return descriptionController.text.isNotEmpty &&
|
||
|
locationController.text.isNotEmpty;
|
||
|
}
|
||
|
|
||
|
Future<void> saveProblem() async {
|
||
|
if (descriptionController.text.isEmpty) {
|
||
|
Get.snackbar('错误', '请填写问题描述');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
isLoading.value = true;
|
||
|
|
||
|
try {
|
||
|
await _problemController.addProblem(
|
||
|
descriptionController.text,
|
||
|
locationController.text,
|
||
|
_selectedImages.toList(),
|
||
|
);
|
||
|
|
||
|
// 清空表单
|
||
|
descriptionController.clear();
|
||
|
locationController.clear();
|
||
|
_selectedImages.clear();
|
||
|
|
||
|
Get.back();
|
||
|
Get.snackbar('成功', '问题已保存');
|
||
|
} catch (e) {
|
||
|
Get.snackbar('错误', '保存问题失败: $e');
|
||
|
} finally {
|
||
|
isLoading.value = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void onClose() {
|
||
|
descriptionController.dispose();
|
||
|
locationController.dispose();
|
||
|
super.onClose();
|
||
|
}
|
||
|
}
|