@@ -96,78 +96,6 @@ Future<List<FileSystemEntity>> _sortEntitysList(
9696 return [];
9797}
9898
99- /// check weather FileSystemEntity is File
100- /// return true if FileSystemEntity is File else returns false
101- bool isFile (FileSystemEntity entity) {
102- return (entity is File );
103- }
104-
105- // check weather FileSystemEntity is Directory
106- /// return true if FileSystemEntity is a Directory else returns Directory
107- bool isDirectory (FileSystemEntity entity) {
108- return (entity is Directory );
109- }
110-
111- /// Get the basename of Directory or File.
112- ///
113- /// Provide [File] , [Directory] or [FileSystemEntity] and returns the name as a [String] .
114- ///
115- /// ie:
116- /// ```dart
117- /// controller.basename(dir);
118- /// ```
119- /// to hide the extension of file, showFileExtension = flase
120- String basename (dynamic entity, [bool showFileExtension = true ]) {
121- if (entity is Directory ) {
122- return entity.path.split ('/' ).last;
123- } else if (entity is File ) {
124- return (showFileExtension)
125- ? entity.path.split ('/' ).last.split ('.' ).first
126- : entity.path.split ('/' ).last;
127- } else {
128- print (
129- "Please provide a Object of type File, Directory or FileSystemEntity" );
130- return "" ;
131- }
132- }
133-
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-
146- /// Get list of available storage in the device
147- /// returns an empty list if there is no storage
148- Future <List <Directory >> getStorageList () async {
149- if (Platform .isAndroid) {
150- List <Directory > storages = (await getExternalStorageDirectories ())! ;
151- storages = storages.map ((Directory e) {
152- final List <String > splitedPath = e.path.split ("/" );
153- return Directory (splitedPath
154- .sublist (0 , splitedPath.indexWhere ((element) => element == "Android" ))
155- .join ("/" ));
156- }).toList ();
157- return storages;
158- } else if (Platform .isLinux) {
159- final Directory dir = await getApplicationDocumentsDirectory ();
160-
161- // Gives the home directory.
162- final Directory home = dir.parent;
163-
164- // you may provide root directory.
165- // final Directory root = dir.parent.parent.parent;
166- return [home];
167- }
168- return [];
169- }
170-
17199/// FileManager is a wonderful widget that allows you to manage files and folders, pick files and folders, and do a lot more.
172100/// Designed to feel like part of the Flutter framework.
173101///
@@ -251,33 +179,92 @@ class FileManager extends StatefulWidget {
251179
252180 @override
253181 _FileManagerState createState () => _FileManagerState ();
254- }
255182
256- class _FileManagerState extends State <FileManager > {
257- final ValueNotifier <String > path = ValueNotifier <String >("" );
258- final ValueNotifier <SortBy > sort = ValueNotifier <SortBy >(SortBy .name);
183+ /// check weather FileSystemEntity is File
184+ /// return true if FileSystemEntity is File else returns false
185+ static bool isFile (FileSystemEntity entity) {
186+ return (entity is File );
187+ }
259188
260- @override
261- void initState () {
262- super .initState ();
189+ // check weather FileSystemEntity is Directory
190+ /// return true if FileSystemEntity is a Directory else returns Directory
191+ static bool isDirectory (FileSystemEntity entity) {
192+ return (entity is Directory );
193+ }
194+
195+ /// Get the basename of Directory or File.
196+ ///
197+ /// Provide [File] , [Directory] or [FileSystemEntity] and returns the name as a [String] .
198+ ///
199+ /// ie:
200+ /// ```dart
201+ /// controller.basename(dir);
202+ /// ```
203+ /// to hide the extension of file, showFileExtension = flase
204+ static String basename (dynamic entity, [bool showFileExtension = true ]) {
205+ if (entity is Directory ) {
206+ return entity.path.split ('/' ).last;
207+ } else if (entity is File ) {
208+ return (showFileExtension)
209+ ? entity.path.split ('/' ).last.split ('.' ).first
210+ : entity.path.split ('/' ).last;
211+ } else {
212+ print (
213+ "Please provide a Object of type File, Directory or FileSystemEntity" );
214+ return "" ;
215+ }
216+ }
263217
264- widget.controller.addListener (() {
265- path.value = widget.controller.getCurrentPath;
266- sort.value = widget.controller.getSortedBy;
267- });
218+ /// Convert bytes to human readable size
219+ static String formatBytes (int bytes, [precision = 2 ]) {
220+ if (bytes != 0 ) {
221+ final double base = math.log (bytes) / math.log (1024 );
222+ final suffix = const ['B' , 'KB' , 'MB' , 'GB' , 'TB' ][base .floor ()];
223+ final size = math.pow (1024 , base - base .floor ());
224+ return '${size .toStringAsFixed (2 )} $suffix ' ;
225+ } else {
226+ return "0B" ;
227+ }
268228 }
269229
230+ /// Get list of available storage in the device
231+ /// returns an empty list if there is no storage
232+ static Future <List <Directory >> getStorageList () async {
233+ if (Platform .isAndroid) {
234+ List <Directory > storages = (await getExternalStorageDirectories ())! ;
235+ storages = storages.map ((Directory e) {
236+ final List <String > splitedPath = e.path.split ("/" );
237+ return Directory (splitedPath
238+ .sublist (
239+ 0 , splitedPath.indexWhere ((element) => element == "Android" ))
240+ .join ("/" ));
241+ }).toList ();
242+ return storages;
243+ } else if (Platform .isLinux) {
244+ final Directory dir = await getApplicationDocumentsDirectory ();
245+
246+ // Gives the home directory.
247+ final Directory home = dir.parent;
248+
249+ // you may provide root directory.
250+ // final Directory root = dir.parent.parent.parent;
251+ return [home];
252+ }
253+ return [];
254+ }
255+ }
256+
257+ class _FileManagerState extends State <FileManager > {
270258 @override
271259 void dispose () {
272- path.dispose ();
273- sort.dispose ();
260+ widget.controller.dispose ();
274261 super .dispose ();
275262 }
276263
277264 @override
278265 Widget build (BuildContext context) {
279266 return FutureBuilder <List <Directory >?>(
280- future: getStorageList (),
267+ future: FileManager . getStorageList (),
281268 builder: (context, snapshot) {
282269 if (snapshot.hasData) {
283270 widget.controller.setCurrentPath = snapshot.data! .first.path;
@@ -294,13 +281,14 @@ class _FileManagerState extends State<FileManager> {
294281
295282 Widget _body (BuildContext context) {
296283 return ValueListenableBuilder <String >(
297- valueListenable: path ,
284+ valueListenable: widget.controller.getPathNotifier ,
298285 builder: (context, pathSnapshot, _) {
299286 return ValueListenableBuilder <SortBy >(
300- valueListenable: sort ,
287+ valueListenable: widget.controller.getSortedByNotifier ,
301288 builder: (context, snapshot, _) {
302289 return FutureBuilder <List <FileSystemEntity >>(
303- future: _sortEntitysList (pathSnapshot, sort.value),
290+ future: _sortEntitysList (pathSnapshot,
291+ widget.controller.getSortedByNotifier.value),
304292 builder: (context, snapshot) {
305293 if (snapshot.hasData) {
306294 List <FileSystemEntity > entitys = snapshot.data! ;
@@ -309,8 +297,8 @@ class _FileManagerState extends State<FileManager> {
309297 }
310298 if (widget.hideHiddenEntity) {
311299 entitys = entitys.where ((element) {
312- if (basename (element) == "" ||
313- basename (element).startsWith ('.' )) {
300+ if (FileManager . basename (element) == "" ||
301+ FileManager . basename (element).startsWith ('.' )) {
314302 return false ;
315303 } else {
316304 return true ;
0 commit comments