pickImage method

Future<String?> pickImage(
  1. PickSource pickSource, {
  2. double? maxWidth,
  3. double? maxHeight,
  4. int? imageQuality,
  5. bool needCrop = true,
})

选择一个图片、或者纯文本

Implementation

Future<String?> pickImage(PickSource pickSource,
    {double? maxWidth,
    double? maxHeight,
    int? imageQuality,
    bool needCrop = true}) async {
  XFile? f;
  switch (pickSource) {
    case PickSource.camera:
      f = await imagePicker.pickImage(
          source: ImageSource.camera,
          maxWidth: maxWidth,
          maxHeight: maxHeight,
          imageQuality: imageQuality);
      break;
    case PickSource.gallery:
      f = await imagePicker.pickImage(
          source: ImageSource.gallery,
          maxWidth: maxWidth,
          maxHeight: maxHeight,
          imageQuality: imageQuality);
      break;
    case PickSource.plainText:
      final text =
          await Get.find<DialogService>().inputDialog(title: "输入选项文本");
      if (text.isEmpty) {
        return null;
      }
      return "text://$text";
    default:
      break;
  }
  if (f != null) {
    if (needCrop) {
      final data =
          await Get.find<ImageCropService>().cropImageWithPath(f.path);
      if (data == null) {
        // 可能是裁剪不了,返回原值
        return f.path;
      } else {
        // 先保存起来
        var tempFile = await Get.find<CacheService>()
            .getTempFile(p.basename(f.path), CacheFileType.pic);
        tempFile = await tempFile?.writeAsBytes(data);
        return tempFile?.path;
      }
    } else {
      return f.path;
    }
  }
  return null;
}