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.

30 lines
901 B

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<File> selectedImage = Rxn<File>();
// 从相册选取并复制
Future<void> 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;
}
}