Skip to content

Commit f63f780

Browse files
committed
formatBytes
1 parent 257845e commit f63f780

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File Manager
22

3-
File manager is a wonderful widget that allows you to manage files and folders, pick files and folders, and do a lot more.
3+
FileManager is a wonderful widget that allows you to manage files and folders, pick files and folders, and do a lot more.
44
Designed to feel like part of the Flutter framework.
55

66

@@ -80,6 +80,20 @@ FileManager(
8080
| `goToParentDirectory` | `goToParentDirectory` returns `bool`, goes to the parent directory of currently opened directory if the parent is accessible, return true if current directory is the root. false, if the current directory not on root of the stogare.. |
8181
| `openDirectory` | Open directory by providing `Directory`. |
8282

83+
## Otheres
84+
| Properties | Description |
85+
|--------------|-----------------|
86+
| `isFile` | check weather FileSystemEntity is File. |
87+
| `isDirectory` | check weather FileSystemEntity is Directory. |
88+
| `basename` | Get the basename of Directory or File. Provide `File`, `Directory` or `FileSystemEntity` and returns the name as a `String`. ie
89+
```dart
90+
controller.dirName(dir);
91+
```
92+
|
93+
| `formatBytes` | Convert bytes to human readable size.[getCurrentDirectory]. |
94+
| `setCurrentPath` | Set current directory path by providing `String` of path, similar to [openDirectory]. `List<FileSystemEntity>.` |
95+
| `getStorageList` | Get list of available storage in the device, returns an empty list if there is no storage `List<Directory>`|
96+
8397
## Show some :heart: and :star: the repo
8498

8599
[![GitHub followers](https://img.shields.io/github/followers/4-alok?style=social)](https://github.com/4-alok/)

example/lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ class HomePage extends StatelessWidget {
118118
if (snapshot.hasData) {
119119
if (entity is File) {
120120
int size = snapshot.data!.size;
121+
121122
return Text(
122-
"$size",
123+
"${formatBytes(size)}",
123124
);
124125
}
125126
return Text(

lib/controller/file_manager_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FileManagerController extends ChangeNotifier {
3232
/// [goToParentDirectory] returns [bool], goes to the parent directory of currently opened directory if the parent is accessible,
3333
/// return true if current directory is the root. false, if the current directory not on root of the stogare.
3434
Future<bool> goToParentDirectory() async {
35-
List<Directory> storageList = (await getStorageList())!;
35+
List<Directory> storageList = (await getStorageList());
3636
final bool willNotGoToParent = (storageList
3737
.where((element) => element.path == Directory(_path).path)
3838
.isNotEmpty);

lib/file_manager.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ String basename(dynamic entity, [bool showFileExtension = true]) {
131131
}
132132
}
133133

134+
/// Convert bytes to human readable size
135+
String formatBytes(int bytes, [precision = 2]) {
136+
if (bytes != 0) {
137+
final double base = math.log(bytes) / math.log(1024);
138+
final suffix = const ['B', 'KB', 'MB', 'GB', 'TB'][base.floor()];
139+
final size = math.pow(1024, base - base.floor());
140+
return '${size.toStringAsFixed(2)} $suffix';
141+
} else {
142+
return "0B";
143+
}
144+
}
145+
134146
/// Get list of available storage in the device
135147
/// returns an empty list if there is no storage
136148
Future<List<Directory>> getStorageList() async {
@@ -156,6 +168,39 @@ Future<List<Directory>> getStorageList() async {
156168
return [];
157169
}
158170

171+
/// FileManager is a wonderful widget that allows you to manage files and folders, pick files and folders, and do a lot more.
172+
/// Designed to feel like part of the Flutter framework.
173+
///
174+
/// Sample code
175+
///```dart
176+
///FileManager(
177+
/// controller: controller,
178+
/// builder: (context, snapshot) {
179+
/// final List<FileSystemEntity> entitis = snapshot;
180+
/// return ListView.builder(
181+
/// itemCount: entitis.length,
182+
/// itemBuilder: (context, index) {
183+
/// return Card(
184+
/// child: ListTile(
185+
/// leading: isFile(entitis[index])
186+
/// ? Icon(Icons.feed_outlined)
187+
/// : Icon(Icons.folder),
188+
/// title: Text(basename(entitis[index])),
189+
/// onTap: () {
190+
/// if (isDirectory(entitis[index])) {
191+
/// controller
192+
/// .openDirectory(entitis[index]);
193+
/// } else {
194+
/// // Perform file-related tasks.
195+
/// }
196+
/// },
197+
/// ),
198+
/// );
199+
/// },
200+
/// );
201+
/// },
202+
///),
203+
///```
159204
class FileManager extends StatefulWidget {
160205
/// For the loading screen, create a custom widget.
161206
/// Simple Centered CircularProgressIndicator is provided by default.

0 commit comments

Comments
 (0)