image_select_ohos_util.dart 3.49 KB
import 'package:flutter/material.dart';
import 'package:image_picker_ohos/image_picker_ohos.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
class ImageSelectOhosUtil{

  static   Future<List<XFile>> onImageButtonPressed(ImageSource source,
      {required BuildContext context,
      bool isMultiImage = false,
      bool isMedia = false,
      bool isVideo = false,
      int limit = 9}) async {
    // if (_controller != null) {
    //   await _controller!.setVolume(0.0);
    // }
      final ImagePickerPlatform _picker = new ImagePickerOhos();
    if (context.mounted) {
      if (isVideo) {
        List<XFile> temp = [];
        final XFile? file = await _picker.getVideo(
            source: source, maxDuration: const Duration(seconds: 10));
        if (file != null && context.mounted) {
          // _showPickedSnackBar(context, <XFile>[file]);
          temp.add(file as XFile);
        }
        return temp;
      } else if (isMultiImage) {
        double? maxWidth = 300.00;
        double? maxHeight = 500;
        int? quality = 90;

        try {
          final ImageOptions imageOptions = ImageOptions(
            maxWidth: maxWidth,
            maxHeight: maxHeight,
            imageQuality: quality,
          );
          final List<XFile> pickedFileList = isMedia
              ? await _picker.getMedia(
                  options: MediaOptions(
                      allowMultiple: isMultiImage,
                      imageOptions: imageOptions,
                      limit: limit),
                )
              : await _picker.getMultiImageWithOptions(
                  options: MultiImagePickerOptions(
                      imageOptions: imageOptions, limit: limit),
                );

          if (pickedFileList.isNotEmpty && context.mounted) {
            return pickedFileList;
          } else {
            return [];
          }
        } catch (e) {
          print(e.toString());
        }
      } else if (isMedia) {
        // double? maxWidth = 300.00;
        // double? maxHeight = 500;
        // int? quality = 90;

        try {
          final List<XFile> pickedFileList = <XFile>[];
          final XFile? media = firstOrNull(await _picker.getMedia(
            options: MediaOptions(
              allowMultiple: isMultiImage,
              // imageOptions: ImageOptions(
              //   maxWidth: maxWidth,
              //   maxHeight: maxHeight,
              //   imageQuality: quality,
              // )
            ),
          ));

          if (media != null) {
            pickedFileList.add(media);
            print(pickedFileList);
            return pickedFileList;
          }
        } catch (e) {
          print(e.toString());
        }
      } else {
        double? maxWidth = 300.00;
        double? maxHeight = 500;
        int? quality = 90;

        try {
          final XFile? pickedFile = await _picker.getImageFromSource(
            source: source,
            options: ImagePickerOptions(
              maxWidth: maxWidth,
              maxHeight: maxHeight,
              imageQuality: quality,
            ),
          );
          List<XFile> temp = [];
          if (pickedFile != null && context.mounted) {
            temp.add(pickedFile as XFile);
          }

          print(pickedFile);
          return temp;

          // <XFile>[pickedFile]
        } catch (e) {
          print(e);
        }
      }
    }
    return [];
  }


  static   T? firstOrNull<T>(List<T> list) {
    return list.isEmpty ? null : list.first;
  }

}