Skip to content

Commit 2c1b22a

Browse files
authored
Merge pull request #3 from 4-alok/main
File-manager sorting competed and Linux support added
2 parents b8d4395 + 67bbcde commit 2c1b22a

File tree

4 files changed

+186
-112
lines changed

4 files changed

+186
-112
lines changed

example/lib/main.dart

Lines changed: 24 additions & 22 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

@@ -15,6 +17,7 @@ class _MyAppState extends State<MyApp> {
1517
Widget build(BuildContext context) {
1618
return MaterialApp(
1719
debugShowCheckedModeBanner: false,
20+
theme: ThemeData(brightness: Brightness.dark),
1821
home: HomePage(),
1922
);
2023
}
@@ -26,44 +29,43 @@ class HomePage extends StatelessWidget {
2629
@override
2730
Widget build(BuildContext context) {
2831
return WillPopScope(
29-
onWillPop: () async {
30-
return await controller.goToParentDirectory();
31-
},
32+
onWillPop: () async => (await controller.goToParentDirectory()),
3233
child: Scaffold(
3334
appBar: AppBar(
3435
actions: [
3536
IconButton(
36-
onPressed: () {
37-
// contoller.sortByName();
38-
},
39-
icon: Icon(Icons.sort),
37+
onPressed: () => controller.setCurrentStorage(strageIndex: 1),
38+
icon: Icon(Icons.sd_storage_rounded),
4039
)
4140
],
4241
leading: IconButton(
4342
icon: Icon(Icons.arrow_back),
4443
onPressed: () async {
45-
if (!(await controller.goToParentDirectory())) {
46-
Navigator.pop(context);
47-
}
44+
await controller.goToParentDirectory();
4845
},
4946
),
5047
),
5148
body: Container(
5249
margin: EdgeInsets.all(10),
5350
child: FileManager(
5451
controller: controller,
55-
tileBuilder: (context, entity) {
56-
// print(entity);
57-
return Card(
58-
child: ListTile(
59-
leading: isFile(entity)
60-
? Icon(Icons.feed_outlined)
61-
: Icon(Icons.folder),
62-
title: Text(basename(entity, false)),
63-
onTap: () {
64-
if (isDirectory(entity)) controller.openDirectory(entity);
65-
},
66-
),
52+
builder: (context, snapshot) {
53+
return ListView.builder(
54+
itemCount: snapshot.length,
55+
itemBuilder: (context, index) {
56+
return Card(
57+
child: ListTile(
58+
leading: isFile(snapshot[index])
59+
? Icon(Icons.feed_outlined)
60+
: Icon(Icons.folder),
61+
title: Text(basename(snapshot[index])),
62+
onTap: () {
63+
if (isDirectory(snapshot[index]))
64+
controller.openDirectory(snapshot[index]);
65+
},
66+
),
67+
);
68+
},
6769
);
6870
},
6971
),
Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import 'dart:io';
2-
32
import 'package:file_manager/file_manager.dart';
43
import 'package:flutter/material.dart';
54

65
class FileManagerController extends ChangeNotifier {
76
String _path = "";
87
int _currentStorage = 0;
8+
SortBy _short = SortBy.size;
9+
10+
// TODO: [Documentation]
11+
SortBy get getSortedBy => _short;
12+
// TODO: [Documentation]
13+
set setSortedBy(SortBy sortType) {
14+
_short = sortType;
15+
notifyListeners();
16+
}
917

1018
/// Get current directory path.
1119
Directory get getCurrentDirectory => Directory(_path);
@@ -19,26 +27,16 @@ class FileManagerController extends ChangeNotifier {
1927
notifyListeners();
2028
}
2129

30+
// TODO: [Documentation]
2231
Future<bool> goToParentDirectory() async {
2332
List<Directory> storageList = (await getStorageList())!;
24-
bool willNotGoToParent = storageList
33+
final bool willNotGoToParent = (storageList
2534
.where((element) => element.path == Directory(_path).path)
26-
.isEmpty;
27-
print(Directory(_path).parent);
28-
if (!willNotGoToParent) {
29-
openDirectory(Directory(_path).parent);
30-
}
35+
.isNotEmpty);
36+
if (!willNotGoToParent) openDirectory(Directory(_path).parent);
3137
return willNotGoToParent;
3238
}
3339

34-
// List<FileSystemEntity> sort(SortBy sort, List<FileSystemEntity> entitys) {
35-
// if (sort == SortBy.name) {
36-
// entitys
37-
// .sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
38-
// return entitys;
39-
// } else if (sort == SortBy.date) {}
40-
// }
41-
4240
/// Open directory by providing Directory.
4341
void openDirectory(FileSystemEntity entity) {
4442
if (entity is Directory) {
@@ -54,12 +52,9 @@ class FileManagerController extends ChangeNotifier {
5452
int get getCurrentStorage => _currentStorage;
5553

5654
/// Set current storege. ie: 0 is for internal storage. 1, 2 and so on, if any external storage is available.
57-
set setCurrentStorage(int index) {
58-
_currentStorage = index;
55+
Future<void> setCurrentStorage({required int strageIndex}) async {
56+
_currentStorage = strageIndex;
57+
_path = (await getStorageList())![strageIndex].path;
5958
notifyListeners();
6059
}
61-
62-
bool handleWillPopScope() {
63-
return false;
64-
}
6560
}

lib/enums/sort_by.dart

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

0 commit comments

Comments
 (0)