flutter_file_picker icon indicating copy to clipboard operation
flutter_file_picker copied to clipboard

add ofnNoChangeDir to make desktop consistent for windows

Open Tokenyet opened this issue 2 years ago • 4 comments

Before this changes, If we used pickFiles on Windows, the current directory would be changed. And the behavior is inconsistent with Linux and Macos, that those never modify current directory. Issue: https://github.com/miguelpruivo/flutter_file_picker/issues/804

For example (from example):

void _pickFiles() async {
    _resetState();
    try {
      _directoryPath = null;
      print(Directory.current.path);
      _paths = (await FilePicker.platform.pickFiles(
        type: _pickingType,
        allowMultiple: _multiPick,
        onFileLoading: (FilePickerStatus status) => print(status),
        allowedExtensions: (_extension?.isNotEmpty ?? false)
            ? _extension?.replaceAll(' ', '').split(',')
            : null,
      ))
          ?.files;
      print(Directory.current.path);
    } on PlatformException catch (e) {
      _logException('Unsupported operation' + e.toString());
    } catch (e) {
      _logException(e.toString());
    }
    if (!mounted) return;
    setState(() {
      _isLoading = false;
      _fileName =
          _paths != null ? _paths!.map((e) => e.name).toString() : '...';
      _userAborted = _paths == null;
    });
  }

Will output: (If I select file from C)

C:\flutter_picker_file\example
C:\

So I add the OFN_NOCHANGEDIR flag to prevent current directory from changing by default on Windows. Reference here.

Then using the sample code will output:

C:\flutter_picker_file\example
C:\flutter_picker_file\example

This make the same logic as MacOS and Linux.

Tokenyet avatar Apr 14 '22 08:04 Tokenyet

@philenius can you take a look?

miguelpruivo avatar May 03 '22 22:05 miguelpruivo

@Tokenyet would you mind to also update the pubspec and changelog?

Thank you!

miguelpruivo avatar May 17 '22 15:05 miguelpruivo

@Tokenyet would you mind to also update the pubspec and changelog?

Thank you!

Done! I think It's more like a bug fix rather than a new feature. If there is any problem, please Iet me know.

Tokenyet avatar May 18 '22 09:05 Tokenyet

I have the same problem

arthas1888 avatar Sep 21 '22 21:09 arthas1888

hey @Tokenyet, I'm sorry that I wasn't active last year when you initially created this PR. Thank you for your input. I included your changes in #1256 and gave you credit in our CHANGELOG.md. Thanks again for pointing me in the right direction and sorry for the delay.

philenius avatar Apr 06 '23 22:04 philenius