pickMedia method

Future pickMedia(
  1. bool isCamera,
  2. bool isAttachment,
  3. bool isAudio
)

Implementation

Future pickMedia(bool isCamera, bool isAttachment, bool isAudio) async {
  var mediaType = "image";
  var thumbnail = "";
  DateTime now = DateTime.now();
  int timestamp = now.millisecondsSinceEpoch;
  if (isCamera) {
    final XFile? image = await picker.pickImage(
      source: ImageSource.camera,
      imageQuality: 80,
    );
    conversationList.insert(
        0,
        Message(
          time: timestamp ~/ 1000,
          timeMilliSeconds: TimeMilliSeconds(
              seconds: timestamp ~/ 1000, nanoseconds: timestamp ~/ 1000),
          sentBy: AppStorages.myUserId,
          senderName: groupData!.userName,
          type: mediaType,
          msgId: "",
          message: "",
          contentType: "",
          replyMsg: "",
          replyMsgId: "",
          replyMsgType: "",
          replyUser: "",
          replyUserId: "",
          fileName: image!.name,
          filePath: image.path,
          name: "",
          thumbnailPath: thumbnail,
        ));
    conversationList[0].fileUploadLoader.value = true;
    uploadMedia(File(image.path.toString()), mediaType, image.path);
  } else {
    FilePickerResult? result = isAttachment
        ? await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['txt', 'pdf', 'doc', 'xlsx'],
    )
        : isAudio
        ? await FilePicker.platform.pickFiles(type: FileType.audio)
        : await FilePicker.platform.pickFiles(type: FileType.media);
    if (result != null) {
      File file = File(result.files.single.path!);
      PlatformFile fileData = result.files.first;
      if (isAttachment) {
        mediaType = "document";
      } else if (isAudio) {
        mediaType = "audio";
      } else {
        if (fileData.extension == 'avi' ||
            fileData.extension == 'flv' ||
            fileData.extension == 'mkv' ||
            fileData.extension == 'mov' ||
            fileData.extension == 'mp4') {
          mediaType = "video";
          // thumbnail = await VideoThumbnail.thumbnailFile(
          //         video: result.paths[0].toString(),
          //         imageFormat: ImageFormat.JPEG) ??
          //     "";
          await VideoThumbnail.thumbnailFile(
            video:  result.paths[0].toString(),
            thumbnailPath: (await getTemporaryDirectory()).path,
            imageFormat: ImageFormat.WEBP,
            maxHeight: 64, // specify the height of the thumbnail, let the width auto-scaled to keep the source aspect ratio
            quality: 75,
          ).then((v){
            thumbnail = v.path;
          });
        }


      }
      conversationList.insert(
          0,
          Message(
            time: timestamp ~/ 1000,
            timeMilliSeconds: TimeMilliSeconds(
                seconds: timestamp ~/ 1000, nanoseconds: timestamp ~/ 1000),
            sentBy: AppStorages.myUserId,
            senderName: groupData!.userName,
            type: mediaType,
            msgId: "",
            message: "",
            contentType: "",
            replyMsg: "",
            replyMsgId: "",
            replyMsgType: "",
            replyUser: "",
            replyUserId: "",
            fileName: fileData.name,
            filePath: file.path,
            name: "",
            thumbnailPath: thumbnail,
          ));
      conversationList[0].fileUploadLoader.value = true;
      uploadMedia(file, mediaType, file.path);
    }
  }
}