From 6bb2d45f8334e386bd0441d41efdb9bd389a9d6a Mon Sep 17 00:00:00 2001 From: Arpit-Sahu Date: Tue, 29 Jun 2021 12:13:07 +0530 Subject: [PATCH 1/2] Api Documentation --- example/lib/main.dart | 4 +- lib/controller/file_manager_controller.dart | 11 +++-- lib/file_manager.dart | 55 ++++++++++++++++++++- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ebf12d1..89f4496 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:file_manager/file_manager.dart'; import 'package:flutter/material.dart'; @@ -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), ) ], diff --git a/lib/controller/file_manager_controller.dart b/lib/controller/file_manager_controller.dart index 30c50e9..1337667 100644 --- a/lib/controller/file_manager_controller.dart +++ b/lib/controller/file_manager_controller.dart @@ -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(); @@ -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 goToParentDirectory() async { List storageList = (await getStorageList())!; final bool willNotGoToParent = (storageList @@ -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 setCurrentStorage({required int strageIndex}) async { - _currentStorage = strageIndex; - _path = (await getStorageList())![strageIndex].path; + Future setCurrentStorage({required int storageIndex}) async { + _currentStorage = storageIndex; + _path = (await getStorageList())![storageIndex].path; notifyListeners(); } } diff --git a/lib/file_manager.dart b/lib/file_manager.dart index 2de92ad..593f91f 100644 --- a/lib/file_manager.dart +++ b/lib/file_manager.dart @@ -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> _sortEntitysList( String path, SortBy sortType) async { final List list = await Directory(path).list().toList(); @@ -96,18 +98,22 @@ Future> _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; @@ -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?> getStorageList() async { if (Platform.isAndroid) { List storages = (await getExternalStorageDirectories())!; @@ -148,10 +155,41 @@ Future?> 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. @@ -177,12 +215,21 @@ class _FileManagerState extends State { @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?>( @@ -201,6 +248,7 @@ class _FileManagerState extends State { ); } + /// main body of File Manager can fetch data from the device Widget body(BuildContext context) { return ValueListenableBuilder( valueListenable: path, @@ -236,6 +284,7 @@ class _FileManagerState extends State { ); } + /// error page that displays the error in the screen Container errorPage(String error) { return Container( color: Colors.red, @@ -245,6 +294,8 @@ class _FileManagerState extends State { ); } + /// 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( From 6ecb17741b2218c492752591fd411e58fc3fd409 Mon Sep 17 00:00:00 2001 From: Arpit-Sahu Date: Tue, 29 Jun 2021 12:20:52 +0530 Subject: [PATCH 2/2] formatted --- lib/controller/file_manager_controller.dart | 2 +- lib/file_manager.dart | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/controller/file_manager_controller.dart b/lib/controller/file_manager_controller.dart index 1337667..cdcf269 100644 --- a/lib/controller/file_manager_controller.dart +++ b/lib/controller/file_manager_controller.dart @@ -12,7 +12,7 @@ class FileManagerController extends ChangeNotifier { /// getSorted by returns the current sorting type of the FileManager SortBy get getSortedBy => _short; // TODO: [Documentation] - /// setSortedBy is used to set the sorting type. + /// setSortedBy is used to set the sorting type. set setSortedBy(SortBy sortType) { _short = sortType; notifyListeners(); diff --git a/lib/file_manager.dart b/lib/file_manager.dart index 593f91f..b7e6d89 100644 --- a/lib/file_manager.dart +++ b/lib/file_manager.dart @@ -215,7 +215,8 @@ class _FileManagerState extends State { @override void initState() { super.initState(); - /// add the listner to the contoller + + /// add the listner to the contoller widget.controller.addListener(() { path.value = widget.controller.getCurrentPath; sort.value = widget.controller.getSortedBy;