Skip to content

Commit f72cdd5

Browse files
committed
Added support for linux
1 parent 932df72 commit f72cdd5

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/controller/file_manager_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
55
class FileManagerController extends ChangeNotifier {
66
String _path = "";
77
int _currentStorage = 0;
8-
SortBy _short = SortBy.name;
8+
SortBy _short = SortBy.date;
99

1010
// TODO: [Documentation]
1111
SortBy get getSortedBy => _short;

lib/file_manager.dart

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,52 @@ typedef _Builder = Widget Function(
1515

1616
class _PathStat {
1717
final String path;
18-
final DateTime fileStat;
19-
_PathStat(this.path, this.fileStat);
18+
final DateTime dateTime;
19+
_PathStat(this.path, this.dateTime);
2020
}
2121

2222
Future<List<FileSystemEntity>> _sortEntitysList(
2323
String path, SortBy sortType) async {
2424
final List<FileSystemEntity> list = await Directory(path).list().toList();
2525
if (sortType == SortBy.name) {
26+
// making list of only folders.
2627
final dirs = list.where((element) => element is Directory).toList();
28+
// sorting folder list by name.
2729
dirs.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
30+
31+
// making list of only flies.
2832
final files = list.where((element) => element is File).toList();
33+
// sorting files list by name.
2934
files.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
35+
36+
// first folders will go to list (if available) then files will go to list.
3037
return [...dirs, ...files];
3138
} else if (sortType == SortBy.date) {
39+
// making the list of Path & DateTime
3240
List<_PathStat> _pathStat = [];
3341
for (FileSystemEntity e in list) {
3442
_pathStat.add(_PathStat(e.path, (await e.stat()).modified));
3543
}
44+
45+
// sort _pathStat according to date
46+
_pathStat.sort((b, a) => a.dateTime.compareTo(b.dateTime));
47+
48+
// sorting [list] accroding to [_pathStat]
3649
list.sort((a, b) => _pathStat
3750
.indexWhere((element) => element.path == a.path)
3851
.compareTo(_pathStat.indexWhere((element) => element.path == b.path)));
3952
return list;
4053
} else if (sortType == SortBy.type) {
54+
// making list of only folders.
4155
final dirs = list.where((element) => element is Directory).toList();
56+
57+
// sorting folders by name.
4258
dirs.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
59+
60+
// making the list of files
4361
final files = list.where((element) => element is File).toList();
62+
63+
// sorting files list by extension.
4464
files.sort((a, b) => a.path
4565
.toLowerCase()
4666
.split('.')
@@ -51,10 +71,12 @@ Future<List<FileSystemEntity>> _sortEntitysList(
5171
return [];
5272
}
5373

74+
// check weather FileSystemEntity is Fille
5475
bool isFile(FileSystemEntity entity) {
5576
return (entity is File);
5677
}
5778

79+
// check weather FileSystemEntity is Directory
5880
bool isDirectory(FileSystemEntity entity) {
5981
return (entity is Directory);
6082
}
@@ -75,18 +97,23 @@ String basename(dynamic entity, [bool showFileExtension = true]) {
7597
}
7698
}
7799

100+
// Get list of available storage directories
78101
Future<List<Directory>?> getStorageList() async {
79-
List<Directory>? storages = await getExternalStorageDirectories();
80102
if (Platform.isAndroid) {
81-
storages = storages!.map((Directory e) {
103+
List<Directory> storages = (await getExternalStorageDirectories())!;
104+
storages = storages.map((Directory e) {
82105
final List<String> splitedPath = e.path.split("/");
83106
return Directory(splitedPath
84107
.sublist(0, splitedPath.indexWhere((element) => element == "Android"))
85108
.join("/"));
86109
}).toList();
87110
return storages;
88-
} else
89-
return [];
111+
} else if (Platform.isLinux) {
112+
print("HEre");
113+
Directory dir = await getApplicationDocumentsDirectory();
114+
return [dir.parent];
115+
}
116+
return [];
90117
}
91118

92119
class FileManager extends StatefulWidget {
@@ -135,7 +162,8 @@ class _FileManagerState extends State<FileManager> {
135162
widget.controller.setCurrentPath = snapshot.data![0].path;
136163
return body(context);
137164
} else if (snapshot.hasError) {
138-
throw Exception(snapshot.error.toString());
165+
print(snapshot.error);
166+
return errorPage(snapshot.error.toString());
139167
} else {
140168
return loadingScreenWidget();
141169
}

0 commit comments

Comments
 (0)