tdrkDev
tdrkDev

Reputation: 89

How to open graphical files picker in Flutter on Linux?

I have problems with opening graphical files picker on Linux in Flutter, Flutter just cannot do it "from box" on Desktop. I am using Flutter 1.24.0-3.0.pre to get Desktop support in Dart.

So, how to open it on Linux?

(I've created this question after solving it, for sharing answer to you)

Upvotes: 4

Views: 1254

Answers (2)

smorgan
smorgan

Reputation: 21549

There is a plugin for this that works on all the desktop platforms, and supports both open and save panels.

Upvotes: 4

tdrkDev
tdrkDev

Reputation: 89

I've created class to do it:

class FilePicker {
  String pickFile(String extension) {
    bool _noExtension = false;
    String _file = "undefined";

    Process.run('which', ['zenity'], runInShell: true).then((pr) {
      if (pr.exitCode != 0) {
        print("zenity not found.");
        return null;
      }

      print("zenity found.");
    });

    if (extension == "undefined") {
      _noExtension = true;
      print("WARNING: extension not specified.");
    }

    Process.run(
            'zenity',
            [
              '--file-selection',
              !_noExtension ? '--file-filter=' + '*.' + extension + '' : '',
            ],
            runInShell: false)
        .then((pr) {
      if (pr.exitCode != 0) {
        print("user canceled choice.");
        print(pr.stderr.toString());
        print(pr.stdout.toString());
        return null;
      }

      _file = pr.stdout.toString();
      print("File: " + _file);
      return _file;
    });

    return null;
  }

  String pickDirectory() {
    String _dir = "undefined";

    Process.run('which', ['zenity'], runInShell: true).then((pr) {
      if (pr.exitCode != 0) {
        print("zenity not found.");
        return null;
      }

      print("zenity found.");
    });

    Process.run('zenity', ['--file-selection', '--directory'], runInShell: true)
        .then((pr) {
      if (pr.exitCode != 0) {
        print("user canceled choice.");
        print(pr.stderr.toString());
        print(pr.stdout.toString());
        return null;
      }

      _dir = pr.stdout.toString();
      print("Directory: " + _dir);
      return _dir;
    });

    return null;
  }
}

Methods:

FilePicker().pickFile("extension");

Pick file with GTK graphical picker, you can to not specify the extension, by typing "undefined" in function arguments

FilePicker().pickDirectory();

Pick directory with GTK graphical picker

Code can be poor, I am newbie in Dart and Flutter :)

Upvotes: 0

Related Questions