Skip to content

Commit 49ee053

Browse files
committed
added pathStream titleStream
1 parent 97e3354 commit 49ee053

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Make sure to check out [examples](https://github.com/DevsOnFlutter/file_manager/
2626

2727
Give storage permission to application
2828

29-
Beside needing to add **WRITE_EXTERNAL_STORAGE** and **READ_EXTERNAL_STORAGE** to your android/app/src/main/AndroidManifest.xml.
29+
**Android:** Beside needing to add **WRITE_EXTERNAL_STORAGE** and **READ_EXTERNAL_STORAGE** to your android/app/src/main/AndroidManifest.xml.
3030

3131
```xml
3232
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
@@ -123,7 +123,8 @@ FileManager(
123123
| `getCurrentDirectory` | Get current Directory |
124124
| `getCurrentPath` | Get current path, similar to [getCurrentDirectory]. |
125125
| `setCurrentPath` | Set current directory path by providing `String` of path, similar to [openDirectory]. `List<FileSystemEntity>.` |
126-
| `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.. |
126+
| `isRootDirectory` | return true if current directory is the root. false, if the current directory not on root of the stogare. |
127+
| `goToParentDirectory` | Jumps to the parent directory of currently opened directory if the parent is accessible. |
127128
| `openDirectory` | Open directory by providing `Directory`. |
128129

129130
## Others

example/lib/main.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ class HomePage extends StatelessWidget {
3131
// Creates a widget that registers a callback to veto attempts by the user to dismiss the enclosing
3232
// or controllers the system's back button
3333
return WillPopScope(
34-
onWillPop: () async => (await controller.goToParentDirectory()),
34+
onWillPop: () async {
35+
if (await controller.isRootDirectory()) {
36+
return true;
37+
} else {
38+
controller.goToParentDirectory();
39+
return false;
40+
}
41+
},
3542
child: Scaffold(
3643
appBar: AppBar(
3744
actions: [
@@ -44,6 +51,12 @@ class HomePage extends StatelessWidget {
4451
icon: Icon(Icons.sd_storage_rounded),
4552
)
4653
],
54+
title: StreamBuilder<String>(
55+
stream: controller.titleStream.stream,
56+
builder: (context, snapshot) {
57+
return Text(snapshot.data!);
58+
},
59+
),
4760
leading: IconButton(
4861
icon: Icon(Icons.arrow_back),
4962
onPressed: () async {

lib/controller/file_manager_controller.dart

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
import 'dart:async';
12
import 'dart:io';
23
import 'package:file_manager/file_manager.dart';
34
import 'package:flutter/material.dart';
45

56
class FileManagerController extends ChangeNotifier {
7+
StreamController<String> pathStream = StreamController<String>.broadcast();
8+
StreamController<String> titleStream = StreamController<String>.broadcast();
69
String _path = "";
710
SortBy _short = SortBy.size;
811

12+
_updatePath(String path) {
13+
pathStream.add(path);
14+
_path = path;
15+
titleStream.add(path.split('/').last);
16+
}
17+
918
/// The sorting type that is currently in use is returned.
1019
SortBy get getSortedBy => _short;
1120

@@ -25,28 +34,37 @@ class FileManagerController extends ChangeNotifier {
2534

2635
/// Set current directory path by providing string of path, similar to [openDirectory].
2736
set setCurrentPath(String path) {
28-
_path = path;
37+
_updatePath(path);
2938
notifyListeners();
3039
}
3140

32-
/// [goToParentDirectory] returns [bool], goes to the parent directory of currently opened directory if the parent is accessible,
3341
/// return true if current directory is the root. false, if the current directory not on root of the stogare.
34-
Future<bool> goToParentDirectory() async {
35-
List<Directory> storageList = (await getStorageList());
36-
final bool willNotGoToParent = (storageList
42+
Future<bool> isRootDirectory() async {
43+
final List<Directory> storageList = (await getStorageList());
44+
return (storageList
3745
.where((element) => element.path == Directory(_path).path)
3846
.isNotEmpty);
39-
if (!willNotGoToParent) openDirectory(Directory(_path).parent);
40-
return willNotGoToParent;
47+
}
48+
49+
/// Jumps to the parent directory of currently opened directory if the parent is accessible.
50+
Future<void> goToParentDirectory() async {
51+
if (!(await isRootDirectory())) openDirectory(Directory(_path).parent);
4152
}
4253

4354
/// Open directory by providing [Directory].
4455
void openDirectory(FileSystemEntity entity) {
4556
if (entity is Directory) {
46-
_path = entity.path;
57+
_updatePath(entity.path);
4758
notifyListeners();
4859
} else {
4960
throw ("FileSystemEntity entity is File. Please provide a Directory(folder) to be opened not File");
5061
}
5162
}
63+
64+
@override
65+
void dispose() {
66+
super.dispose();
67+
pathStream.close();
68+
titleStream.close();
69+
}
5270
}

0 commit comments

Comments
 (0)