Skip to content

Commit 8fcb35a

Browse files
committed
added SortBy.size in SortBy
1 parent f72cdd5 commit 8fcb35a

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:io';
2+
13
import 'package:file_manager/file_manager.dart';
24
import 'package:flutter/material.dart';
35

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.date;
8+
SortBy _short = SortBy.size;
99

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

lib/enums/sort_by.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enum SortBy { name, type, date }
1+
enum SortBy { name, type, date, size}

lib/file_manager.dart

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ Future<List<FileSystemEntity>> _sortEntitysList(
6767
.last
6868
.compareTo(b.path.toLowerCase().split('.').last));
6969
return [...dirs, ...files];
70+
} else if (sortType == SortBy.size) {
71+
// create list of path and size
72+
Map<String, int> _sizeMap = {};
73+
for (FileSystemEntity e in list) {
74+
_sizeMap[e.path] = (await e.stat()).size;
75+
}
76+
77+
// making list of only folders.
78+
final dirs = list.where((element) => element is Directory).toList();
79+
// sorting folder list by name.
80+
dirs.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
81+
82+
// making list of only flies.
83+
final files = list.where((element) => element is File).toList();
84+
85+
// creating sorted list of [_sizeMapList] by size.
86+
final List<MapEntry<String, int>> _sizeMapList = _sizeMap.entries.toList();
87+
_sizeMapList.sort((b, a) => a.value.compareTo(b.value));
88+
89+
// sort [list] according to [_sizeMapList]
90+
files.sort((a, b) => _sizeMapList
91+
.indexWhere((element) => element.key == a.path)
92+
.compareTo(
93+
_sizeMapList.indexWhere((element) => element.key == b.path)));
94+
return [...dirs, ...files];
7095
}
7196
return [];
7297
}
@@ -109,9 +134,14 @@ Future<List<Directory>?> getStorageList() async {
109134
}).toList();
110135
return storages;
111136
} else if (Platform.isLinux) {
112-
print("HEre");
113-
Directory dir = await getApplicationDocumentsDirectory();
114-
return [dir.parent];
137+
final Directory dir = await getApplicationDocumentsDirectory();
138+
139+
// Gives the home directory.
140+
final Directory home = dir.parent;
141+
142+
// you may provide root directory.
143+
// final Directory root = dir.parent.parent.parent;
144+
return [home];
115145
}
116146
return [];
117147
}

0 commit comments

Comments
 (0)