我需要在Flutter中导出.txt
文件。有没有一种对话框可以让用户选择保存文件的路径/位置? image_picker
之类的东西,仅用于路径。
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以同时使用https://pub.dev/packages/folder_picker和
https://pub.dev/packages/path_provider_ex
不要忘记添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
工作演示
完整代码
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:permission/permission.dart';
import 'package:folder_picker/folder_picker.dart';
import 'package:flutter/material.dart';
import 'package:path_provider_ex/path_provider_ex.dart';
import 'dart:async';
import 'package:flutter/services.dart';
class _FolderPickerDemoState extends State<FolderPickerDemo> {
Directory externalDirectory;
Directory pickedDirectory;
List<StorageInfo> _storageInfo = [];
Future<void> getPermissions() async {
final permissions =
await Permission.getPermissionsStatus([PermissionName.Storage]);
var request = true;
switch (permissions[0].permissionStatus) {
case PermissionStatus.allow:
request = false;
break;
case PermissionStatus.always:
request = false;
break;
default:
}
if (request) {
await Permission.requestPermissions([PermissionName.Storage]);
}
}
Future<void> getStorage() async {
final directory = await getExternalStorageDirectory();
print(" ${directory.path}");
setState(() => externalDirectory = directory);
}
Future<void> init() async {
await getPermissions();
await getStorage();
}
Future<void> initPlatformState() async {
List<StorageInfo> storageInfo;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
storageInfo = await PathProviderEx.getStorageInfo();
} on PlatformException {}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_storageInfo = storageInfo;
});
}
@override
void initState() {
super.initState();
init();
initPlatformState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: (_storageInfo.length > 0)
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child:
const Text("Pick a folder", textScaleFactor: 1.3),
onPressed: () => Navigator.of(context)
.push<FolderPickerPage>(MaterialPageRoute(
builder: (BuildContext context) {
return FolderPickerPage(
rootDirectory: Directory(_storageInfo[0].rootDir),
action: (BuildContext context,
Directory folder) async {
print("Picked directory $folder");
setState(() => pickedDirectory = folder);
Navigator.of(context).pop();
});
})),
),
(pickedDirectory != null)
? Padding(
padding: const EdgeInsets.only(top: 30.0),
child: Text("${pickedDirectory.path}"),
)
: const Text('')
])
: const CircularProgressIndicator()));
}
}
class FolderPickerDemo extends StatefulWidget {
@override
_FolderPickerDemoState createState() => _FolderPickerDemoState();
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Folder picker Demo',
home: FolderPickerDemo(),
theme: ThemeData.dark(),
);
}
}
void main() => runApp(MyApp());