Skip to content

Api Documentation #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:file_manager/file_manager.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -34,7 +32,7 @@ class HomePage extends StatelessWidget {
appBar: AppBar(
actions: [
IconButton(
onPressed: () => controller.setCurrentStorage(strageIndex: 1),
onPressed: () => controller.setCurrentStorage(storageIndex: 1),
icon: Icon(Icons.sd_storage_rounded),
)
],
Expand Down
11 changes: 8 additions & 3 deletions lib/controller/file_manager_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ class FileManagerController extends ChangeNotifier {
SortBy _short = SortBy.size;

// TODO: [Documentation]

/// getSorted by returns the current sorting type of the FileManager
SortBy get getSortedBy => _short;
// TODO: [Documentation]
/// setSortedBy is used to set the sorting type.
set setSortedBy(SortBy sortType) {
_short = sortType;
notifyListeners();
Expand All @@ -28,6 +31,8 @@ class FileManagerController extends ChangeNotifier {
}

// TODO: [Documentation]
/// goToParentDirectory returns false and goes to the parent directory of currently opened directory if the parent is accessible,
/// it will return true and pops the screen if the parent of currently opened directory is not accessible.
Future<bool> goToParentDirectory() async {
List<Directory> storageList = (await getStorageList())!;
final bool willNotGoToParent = (storageList
Expand All @@ -52,9 +57,9 @@ class FileManagerController extends ChangeNotifier {
int get getCurrentStorage => _currentStorage;

/// Set current storege. ie: 0 is for internal storage. 1, 2 and so on, if any external storage is available.
Future<void> setCurrentStorage({required int strageIndex}) async {
_currentStorage = strageIndex;
_path = (await getStorageList())![strageIndex].path;
Future<void> setCurrentStorage({required int storageIndex}) async {
_currentStorage = storageIndex;
_path = (await getStorageList())![storageIndex].path;
notifyListeners();
}
}
56 changes: 54 additions & 2 deletions lib/file_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class _PathStat {
_PathStat(this.path, this.dateTime);
}

///sort the files and folders according to the SortBy type
///It will return a list of fileSystemEntity after sorting
Future<List<FileSystemEntity>> _sortEntitysList(
String path, SortBy sortType) async {
final List<FileSystemEntity> list = await Directory(path).list().toList();
Expand Down Expand Up @@ -96,18 +98,22 @@ Future<List<FileSystemEntity>> _sortEntitysList(
return [];
}

// check weather FileSystemEntity is Fille
/// check weather FileSystemEntity is File
/// return true if FileSystemEntity is a File else returns false
bool isFile(FileSystemEntity entity) {
return (entity is File);
}

// check weather FileSystemEntity is Directory
/// return true if FileSystemEntity is a Directory else returns Directory
bool isDirectory(FileSystemEntity entity) {
return (entity is Directory);
}

/// Get the basename of Directory or File.
/// takes the directory and returns the name of file or folder
/// ie: controller.dirName(dir);
/// to hide the extension of file, showFileExtension = flase
String basename(dynamic entity, [bool showFileExtension = true]) {
if (entity is Directory) {
return entity.path.split('/').last;
Expand All @@ -122,7 +128,8 @@ String basename(dynamic entity, [bool showFileExtension = true]) {
}
}

// Get list of available storage directories
/// Get list of available storage in the device
/// returns an empty list if there is no storage
Future<List<Directory>?> getStorageList() async {
if (Platform.isAndroid) {
List<Directory> storages = (await getExternalStorageDirectories())!;
Expand All @@ -148,10 +155,41 @@ Future<List<Directory>?> getStorageList() async {

class FileManager extends StatefulWidget {
/// Provide a custom widget for loading screen.
/// as default CircularProgressIndicator is provided.
final Widget? loadingScreen;

/// Provide a scroll Physics for scrolling behaviour.
final ScrollPhysics? physics;

///shrinkwrap will only occupy the space it need
final bool shrinkWrap;

final FileManagerController controller;

///Builder is a custom builder which takes an entity and bulds a widget around it
///
///
///```
/// builder: (context, snapshot) {
/// return ListView.builder(
/// itemCount: snapshot.length,
/// itemBuilder: (context, index) {
/// return Card(
/// child: ListTile(
/// leading: isFile(snapshot[index])
/// ? Icon(Icons.feed_outlined)
/// : Icon(Icons.folder),
/// title: Text(basename(snapshot[index])),
/// onTap: () {
/// if (isDirectory(snapshot[index]))
/// controller.openDirectory(snapshot[index]);
/// },
/// ),
/// );
/// },
/// );
/// },
/// ```
final _Builder builder;

/// Hide the hidden file and folder.
Expand All @@ -177,12 +215,22 @@ class _FileManagerState extends State<FileManager> {
@override
void initState() {
super.initState();

/// add the listner to the contoller
widget.controller.addListener(() {
path.value = widget.controller.getCurrentPath;
sort.value = widget.controller.getSortedBy;
});
}

/// dispose all the value notifiers
@override
void dispose() {
path.dispose();
sort.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return FutureBuilder<List<Directory>?>(
Expand All @@ -201,6 +249,7 @@ class _FileManagerState extends State<FileManager> {
);
}

/// main body of File Manager can fetch data from the device
Widget body(BuildContext context) {
return ValueListenableBuilder<String>(
valueListenable: path,
Expand Down Expand Up @@ -236,6 +285,7 @@ class _FileManagerState extends State<FileManager> {
);
}

/// error page that displays the error in the screen
Container errorPage(String error) {
return Container(
color: Colors.red,
Expand All @@ -245,6 +295,8 @@ class _FileManagerState extends State<FileManager> {
);
}

/// loading screen if the backend is currently fecthing data from the device.
/// It will display simple Circular Progress Indicator if no custom widget is passed.
Widget loadingScreenWidget() {
if ((widget.loadingScreen == null)) {
return Container(
Expand Down