flutter_ 選擇照片

UI

截屏2022-10-27 10.54.45.png

代碼

  wechat_assets_picker: ^7.1.2
  wechat_camera_picker: ^3.1.0
///圖片選擇
class PictureSelection {
  ///獲取照片,讓用戶選擇來著哪里
  static Future<List<AssetEntity>?> getPictureSwitch(
      {AssetPickerConfig? assetPickerConfig,
      CameraPickerConfig? cameraPickerConfig}) async {
    int switchNum = await _getSwitch();
    if (switchNum == 0) {
      //拍攝照片
      return getPictureWithCamera(cameraPickerConfig: cameraPickerConfig);
    } else if (switchNum == 1) {
      //相冊選擇
      return getPictureWithAlbum(assetPickerConfig: assetPickerConfig);
    } else {
      //用戶取消
      return [];
    }
  }

  ///相冊獲取照片
  static Future<List<AssetEntity>?> getPictureWithAlbum(
      {AssetPickerConfig? assetPickerConfig}) async {
    try {
      if (await PHUS.storage && await PHUS.photos) {
        return AssetPicker.pickAssets(
          Get.context!,
          pickerConfig: assetPickerConfig ??
              const AssetPickerConfig(
                themeColor: TCS.blue00b6,
                requestType: RequestType.image,
                textDelegate: AssetPickerTextDelegate(),
              ),
        );
      }
    } catch (e) {
      ET.err("獲取失敗,請檢查是否授予權限");
    }
    return null;
  }

  ///相機獲取照片
  static Future<List<AssetEntity>?> getPictureWithCamera(
      {CameraPickerConfig? cameraPickerConfig}) async {
    AssetEntity? assetEntity;
    try {
      if (await PHUS.location &&
          await PHUS.photos &&
          await PHUS.storage &&
          await PHUS.camera) {
        assetEntity = await CameraPicker.pickFromCamera(
          Get.context!,
          pickerConfig: cameraPickerConfig ?? const CameraPickerConfig(),
        );
      }
    } catch (e) {
      ET.err("獲取失敗,請檢查是否授予權限");
    }
    return ObjectUtil.isNotEmpty(assetEntity) ? [assetEntity!] : [];
  }

  ///選擇相機拍攝,還是在相冊選擇提示框
  static Future<int> _getSwitch() async {
    return await Get.bottomSheet(
            Container(
              color: Colors.white,
              child: SafeArea(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Material(
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(15.r),
                          topRight: Radius.circular(15.r)),
                      color: Colors.white,
                      child: Container(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(15.r),
                              topRight: Radius.circular(15.r)),
                        ),
                        child: Column(
                          children: [
                            InkWell(
                              borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(15.r),
                                  topRight: Radius.circular(15.r)),
                              onTap: () {
                                Get.back(result: 0);
                              },
                              child: Container(
                                alignment: Alignment.center,
                                width: double.infinity,
                                padding: EdgeInsets.symmetric(vertical: 15.r),
                                decoration: BoxDecoration(
                                    border: Border(
                                        bottom: BorderSide(
                                            width: 0.8.r,
                                            color: const Color(0xffF2F2F2)))),
                                child: TU.d18("拍一張", bold: true),
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                Get.back(result: 1);
                              },
                              child: Container(
                                alignment: Alignment.center,
                                width: double.infinity,
                                padding: EdgeInsets.symmetric(vertical: 15.r),
                                child: TU.d18("從相冊選擇", bold: true),
                              ),
                            ),
                            Container(
                              color: const Color(0xffF2F2F2),
                              height: 8.r,
                            ),
                            InkWell(
                              onTap: () {
                                Get.back(result: -1);
                              },
                              child: Container(
                                alignment: Alignment.center,
                                width: double.infinity,
                                padding: EdgeInsets.symmetric(vertical: 15.r),
                                child: TU.d18(
                                  "取消",
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
            isDismissible: false) ??
        -1;
  }

  ///獲取一張照片路徑
  ///isCutting  是否裁剪
  ///aspectRatio 裁剪比例
  static Future<String> getCutOneImage(
      {isCutting = true, aspectRatio = 1 / 1}) async {
    List<AssetEntity?>? list = [];
    try {
      list = await PictureSelection.getPictureSwitch(
          assetPickerConfig: const AssetPickerConfig(
              themeColor: TCS.blue00b6,
              requestType: RequestType.image,
              maxAssets: 1,
              textDelegate: AssetPickerTextDelegate()));
    } catch (e) {
      ET.err("獲取失敗,請檢查是否授予權限");
    }
    if (list != null && list.isNotEmpty) {
      File? file = await list[0]?.originFile;
      if (file == null) {
        ET.err("獲取圖片失敗,請重試");
        return '';
      }
      File? editedImage = file;

      ///是否進行
      if (isCutting) {
        editedImage = await Get.to(() => ImageCurring(
              image: file,
              aspectRatio: aspectRatio,
            ));
        if (editedImage != null) {
          return editedImage.path;
        } else {
          return '';
        }
      }
      return editedImage.path;
    } else {
      return '';
    }
  }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容