selectFileButtonHandler method

void selectFileButtonHandler()

Implementation

void selectFileButtonHandler() async {
  late FileSource source;
  if (widget.fileSource.length == 1) {
    source = widget.fileSource.first;
  } else {
    FileSource? selectedSource;
    var result = showModalBottomSheet(
        context: context,
        enableDrag: true,
        builder: (_) => GestureDetector(
              onTap: Navigator.of(context).pop,
              child: SafeArea(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    if (widget.fileSource.contains(FileSource.gallery))
                      ListTile(
                          title: const Text("Gallery"),
                          onTap: () {
                            selectedSource = FileSource.gallery;
                            Navigator.pop(_, FileSource.gallery);
                          }),
                    if (widget.fileSource.contains(FileSource.camera))
                      ListTile(
                          title: const Text("Camera"),
                          onTap: () {
                            selectedSource = FileSource.camera;
                            Navigator.pop(_, FileSource.camera);
                          }),
                    if (widget.fileSource.contains(FileSource.files))
                      ListTile(
                          title: const Text("Files"),
                          onTap: () {
                            selectedSource = FileSource.files;
                            Navigator.pop(_, FileSource.files);
                          }),
                  ],
                ),
              ),
            ));

    await result;
    if (selectedSource == null) {
      return;
    }
    source = selectedSource!;
  }

  try {
    var newFiles = await fileGrabAdaptors[source]!.grabFile(widget.settings);
    if (newFiles == null) return;

    if (widget.settings.maxFileCount != null &&
        files.length + newFiles.length > widget.settings.maxFileCount!) {
      widget.setError("Max file count exceeded");
      return;
    }

    setState(() {
      int expecteeAddedFiles = files.length + newFiles.length;
      files.addAll(newFiles);
      updateFieldValue();

      if (files.length != expecteeAddedFiles) {
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
          content: Text("Duplicate files were discarded"),
        ));
      }
    });
  } on String catch (msg) {
    if (mounted) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text(msg),
      ));
    }
  } catch (e) {
    widget.setError("Unexpected error occured");
  }
}