nativeFilePicker function

void nativeFilePicker({
  1. required FileType type,
  2. required BuildContext context,
  3. required SendMessage widget,
  4. required Function onUploadSuccess,
})

Implementation

void nativeFilePicker({
  required FileType type,
  required BuildContext context,
  required SendMessage widget,
  required Function onUploadSuccess,
}) async {
  try {
    final paths = (await FilePicker.platform.pickFiles(
      type: type,
    ))
        ?.files;
    if (paths?.first.path != null) {
      Alert.show(
        widget.props.translations.attachmentUploadingText,
        context,
        textStyle: widget.props.style.chatUploadingAlertTextStyle ??
            Theme.of(context).textTheme.bodyMedium,
        backgroundColor: widget.props.style.chatUploadingAlertBackgroundColor ??
            BottomAppBarTheme.of(context).color!,
        gravity: Alert.bottom,
        duration: Alert.lengthLong,
      );
      List<PapercupsAttachment> attachments = await uploadFile(
        widget.props,
        filePath: paths?.first.path,
        onUploadProgress: (sentBytes, totalBytes) {
          Alert.show(
            "${(sentBytes * 100 / totalBytes).toStringAsFixed(2)}% ${widget.props.translations.uploadedText}",
            context,
            textStyle: widget.props.style.chatUploadingAlertTextStyle ??
                Theme.of(context).textTheme.bodyMedium,
            backgroundColor:
                widget.props.style.chatUploadingAlertBackgroundColor ??
                    BottomAppBarTheme.of(context).color!,
            gravity: Alert.bottom,
            duration: Alert.lengthLong,
          );
        },
      );

      onUploadSuccess(attachments);
    }
  } on PlatformException catch (_) {
    Alert.show(
      widget.props.translations.attachmentUploadErrorText,
      context,
      textStyle: widget.props.style.chatUploadErrorAlertTextStyle ??
          Theme.of(context).textTheme.bodyMedium,
      backgroundColor: widget.props.style.chatUploadErrorAlertBackgroundColor ??
          BottomAppBarTheme.of(context).color!,
      gravity: Alert.bottom,
      duration: Alert.lengthLong,
    );
    rethrow;
  } catch (_) {
    Alert.show(
      widget.props.translations.attachmentUploadErrorText,
      context,
      textStyle: widget.props.style.chatUploadErrorAlertTextStyle ??
          Theme.of(context).textTheme.bodyMedium,
      backgroundColor: widget.props.style.chatUploadErrorAlertBackgroundColor ??
          BottomAppBarTheme.of(context).color!,
      gravity: Alert.bottom,
      duration: Alert.lengthLong,
    );
    rethrow;
  }
}