import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_file_manager/flutter_file_manager.dart';
import 'package:path_provider_ex/path_provider_ex.dart';
//import package files
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyFileList(), //call MyFile List
);
}
}
//apply this class on home: attribute at MaterialApp()
class MyFileList extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return _MyFileList();
}
}
class _MyFileList extends State<MyFileList>{
var files;
void getFiles() async { //asyn function to get list of files
List<StorageInfo> storageInfo = await PathProviderEx.getStorageInfo();
var root = storageInfo[0].rootDir; //storageInfo[1] for SD card, geting the root directory
var fm = FileManager(root: Directory(root)); //
files = await fm.filesTree(
//set fm.dirsTree() for directory/folder tree list
excludedPaths: ["/storage/emulated/0/Android"],
extensions: ["png", "pdf"] //optional, to filter files, remove to list all,
//remove this if your are grabbing folder list
);
setState(() {}); //update the UI
}
@override
void initState() {
getFiles(); //call getFiles() function on initial state.
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("File/Folder list from SD Card"),
backgroundColor: Colors.redAccent
),
body:files == null? Text("Searching Files"):
ListView.builder( //if file/folder list is grabbed, then show here
itemCount: files?.length ?? 0,
itemBuilder: (context, index) {
return Card(
child:ListTile(
title: Text(files[index].path.split('/').last),
leading: Icon(Icons.image),
trailing: Icon(Icons.delete, color: Colors.redAccent,),
)
);
},
)
);
}
}
2条答案
按热度按时间jjhzyzn01#
要列出所有的文件或文件夹,必须使用flutter_file_manager、path和path_provider_ex flutter包。在pubspec.yaml文件中添加以下行,以便将此包添加到依赖项中。
在您的android/app/src/main/AndroidManifest.xml中的标签之前添加dd读/写权限。
如果仍然出现Permission Denied错误,请在AndroidManifest.xml文件中添加以下行。
完整代码:
pwuypxnk2#
可以这样写: