如何在Linux上的Flutter中打开图形文件选择器?

问题描述

我在Flutter上打开Linux上的图形文件选择器时遇到问题,Flutter不能在桌面上“从盒子”完成它。我正在使用Flutter 1.24.0-3.0.pre在Dart中获得桌面支持。

那么,如何在Linux上打开它?

(我在解决此问题后创建了这个问题,以便与您分享答案)

解决方法

我已经创建了用于实现该目标的课程

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',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;
  }
}

方法:

FilePicker()。pickFile(“ extension”);

使用GTK图形选择器选择文件,您可以通过在函数参数中键入“ undefined”来指定扩展名

FilePicker()。pickDirectory();

使用GTK图形选择器选择目录

代码可能很差,我是Dart和Flutter的新手:)

,

a plugin可以在所有桌面平台上运行,并且支持打开和保存面板。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...