繁体   English   中英

如何通过Flutter桌面打开特定的原生Mac OS、Windows和Linux系统文件夹?

[英]How to open a specific native Mac OS, Windows and Linux system folder through Flutter desktop?

我需要使用 Flutter 桌面打开类似于下图的本地系统特定文件夹:

FilePicker package 不起作用,因为它会打开一个“随机”文件夹,我需要它是一个特定的文件夹

在此处输入图像描述

这是一个 Mac OS 示例,但我还需要它在 Windows 和 Linux 上打开。

您可以使用 package file_picker ,它支持您需要的所有平台。

FilePickerResult? result = await FilePicker.platform.pickFiles();

if (result != null) {
  File file = File(result.files.single.path);
} else {
  // User canceled the picker
}
file_picker: ^4.4.0

pub.dev 文件选择器在此处输入图像描述

这个 package 在所有平台上都标有

单个文件

FilePickerResult? result = await FilePicker.platform.pickFiles();

if (result != null) {
  File file = File(result.files.single.path);
} else {
  // User canceled the picker
}

多个文件

FilePickerResult? result = await FilePicker.platform.pickFiles(allowMultiple: true);

if (result != null) {
  List<File> files = result.paths.map((path) => File(path)).toList();
} else {
  // User canceled the picker
}

在 Windows 中是这样的:

在此处输入图像描述

完整示例代码:

import 'dart:io';


import 'package:file_picker/file_picker.dart';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();




  runApp(MaterialApp(home: sta()));
}

// eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ==
class sta extends StatefulWidget {
  const sta({Key? key}) : super(key: key);

  @override
  State<sta> createState() => _staState();
}



class _staState extends State<sta> {
  @override
  Widget build(BuildContext context) {
    // MediaQuery(
    //     data: MediaQuery.of(context).copyWith(
    //       textScaleFactor: 1.0,
    //     ),
    return Scaffold(
        appBar: AppBar(),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    height: 100,
                    width: 100,
                    child: OutlinedButton(
                      onPressed: () {
                        Filepick();
                      },
                      child: Text("Pick File"),
                    ),
                  ),
                ],
              ),
            ),

          ],
        ));
  }

  Future<void> Filepick() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles();

    if (result != null) {
      File file = File(result.files.single.path ?? "");
    } else {
      // User canceled the picker
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM