import 'dart:io'; import 'package:get/get.dart'; import 'package:image_picker/image_picker.dart'; import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; class ImageController extends GetxController { // Rxn 表示 nullable 的响应式类型 final Rxn selectedImage = Rxn(); // 从相册选取并复制 Future pickAndCopyImage() async { // 调用 ImagePicker 选取 final picked = await ImagePicker().pickImage(source: ImageSource.gallery); if (picked == null) return; final originalFile = File(picked.path); // 获取应用文档目录 final appDir = await getApplicationDocumentsDirectory(); final fileName = basename(picked.path); // 复制到应用目录 final copiedFile = await originalFile.copy('${appDir.path}/$fileName'); // 更新响应式状态 selectedImage.value = copiedFile; } }