Skip to content

Commit 257845e

Browse files
committed
example ready
1 parent 82d1310 commit 257845e

File tree

3 files changed

+145
-10
lines changed

3 files changed

+145
-10
lines changed

example/lib/main.dart

Lines changed: 142 additions & 8 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

@@ -26,14 +28,19 @@ class HomePage extends StatelessWidget {
2628

2729
@override
2830
Widget build(BuildContext context) {
31+
// Creates a widget that registers a callback to veto attempts by the user to dismiss the enclosing
32+
// or controllers the system's back button
2933
return WillPopScope(
3034
onWillPop: () async => (await controller.goToParentDirectory()),
3135
child: Scaffold(
3236
appBar: AppBar(
3337
actions: [
3438
IconButton(
35-
onPressed: () async =>
36-
controller.openDirectory((await getStorageList())!.first),
39+
onPressed: () => sort(context),
40+
icon: Icon(Icons.sort_rounded),
41+
),
42+
IconButton(
43+
onPressed: () => selectStorage(context),
3744
icon: Icon(Icons.sd_storage_rounded),
3845
)
3946
],
@@ -49,18 +56,50 @@ class HomePage extends StatelessWidget {
4956
child: FileManager(
5057
controller: controller,
5158
builder: (context, snapshot) {
59+
final List<FileSystemEntity> entities = snapshot;
5260
return ListView.builder(
53-
itemCount: snapshot.length,
61+
itemCount: entities.length,
5462
itemBuilder: (context, index) {
63+
FileSystemEntity entity = entities[index];
5564
return Card(
5665
child: ListTile(
57-
leading: isFile(snapshot[index])
66+
leading: isFile(entity)
5867
? Icon(Icons.feed_outlined)
5968
: Icon(Icons.folder),
60-
title: Text(basename(snapshot[index])),
61-
onTap: () {
62-
if (isDirectory(snapshot[index]))
63-
controller.openDirectory(snapshot[index]);
69+
title: Text(basename(entity)),
70+
subtitle: subtitle(entity),
71+
onTap: () async {
72+
if (isDirectory(entity)) {
73+
// open the folder
74+
controller.openDirectory(entity);
75+
76+
// delete a folder
77+
// await entity.delete(recursive: true);
78+
79+
// rename a folder
80+
// await entity.rename("newPath");
81+
82+
// Check weather folder exists
83+
// entity.exists();
84+
85+
// get date of file
86+
// DateTime date = (await entity.stat()).modified;
87+
} else {
88+
// delete a file
89+
// await entity.delete();
90+
91+
// rename a file
92+
// await entity.rename("newPath");
93+
94+
// Check weather file exists
95+
// entity.exists();
96+
97+
// get date of file
98+
// DateTime date = (await entity.stat()).modified;
99+
100+
// get the size of the file
101+
// int size = (await entity.stat()).size;
102+
}
64103
},
65104
),
66105
);
@@ -71,4 +110,99 @@ class HomePage extends StatelessWidget {
71110
)),
72111
);
73112
}
113+
114+
Widget subtitle(FileSystemEntity entity) {
115+
return FutureBuilder<FileStat>(
116+
future: entity.stat(),
117+
builder: (context, snapshot) {
118+
if (snapshot.hasData) {
119+
if (entity is File) {
120+
int size = snapshot.data!.size;
121+
return Text(
122+
"$size",
123+
);
124+
}
125+
return Text(
126+
"${snapshot.data!.modified}",
127+
);
128+
} else {
129+
return Text("");
130+
}
131+
},
132+
);
133+
}
134+
135+
selectStorage(BuildContext context) {
136+
showDialog(
137+
context: context,
138+
builder: (context) => Dialog(
139+
child: FutureBuilder<List<Directory>>(
140+
future: getStorageList(),
141+
builder: (context, snapshot) {
142+
if (snapshot.hasData) {
143+
final List<FileSystemEntity> storageList = snapshot.data!;
144+
return Padding(
145+
padding: const EdgeInsets.all(10.0),
146+
child: Column(
147+
mainAxisSize: MainAxisSize.min,
148+
children: storageList
149+
.map((e) => ListTile(
150+
title: Text(
151+
"${basename(e)}",
152+
),
153+
onTap: () {
154+
controller.openDirectory(e);
155+
Navigator.pop(context);
156+
},
157+
))
158+
.toList()),
159+
);
160+
}
161+
return Dialog(
162+
child: CircularProgressIndicator(),
163+
);
164+
},
165+
),
166+
),
167+
);
168+
}
169+
170+
sort(BuildContext context) async {
171+
showDialog(
172+
context: context,
173+
builder: (context) => Dialog(
174+
child: Container(
175+
padding: EdgeInsets.all(10),
176+
child: Column(
177+
mainAxisSize: MainAxisSize.min,
178+
children: [
179+
ListTile(
180+
title: Text("Name"),
181+
onTap: () {
182+
controller.sortedBy = SortBy.name;
183+
Navigator.pop(context);
184+
}),
185+
ListTile(
186+
title: Text("Size"),
187+
onTap: () {
188+
controller.sortedBy = SortBy.size;
189+
Navigator.pop(context);
190+
}),
191+
ListTile(
192+
title: Text("Date"),
193+
onTap: () {
194+
controller.sortedBy = SortBy.date;
195+
Navigator.pop(context);
196+
}),
197+
ListTile(
198+
title: Text("type"),
199+
onTap: () {
200+
controller.sortedBy = SortBy.type;
201+
Navigator.pop(context);
202+
}),
203+
],
204+
),
205+
),
206+
));
207+
}
74208
}

lib/controller/file_manager_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class FileManagerController extends ChangeNotifier {
1212
/// [setSortedBy] is used to set the sorting type.
1313
///
1414
/// `SortBy{ name, type, date, size }`
15-
set setSortedBy(SortBy sortType) {
15+
set sortedBy(SortBy sortType) {
1616
_short = sortType;
1717
notifyListeners();
1818
}

lib/file_manager.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library file_manager;
22

33
import 'dart:io';
4+
import 'dart:math' as math;
45
import 'package:flutter/material.dart';
56
import 'package:path_provider/path_provider.dart';
67

@@ -132,7 +133,7 @@ String basename(dynamic entity, [bool showFileExtension = true]) {
132133

133134
/// Get list of available storage in the device
134135
/// returns an empty list if there is no storage
135-
Future<List<Directory>?> getStorageList() async {
136+
Future<List<Directory>> getStorageList() async {
136137
if (Platform.isAndroid) {
137138
List<Directory> storages = (await getExternalStorageDirectories())!;
138139
storages = storages.map((Directory e) {

0 commit comments

Comments
 (0)