Skip to content

Commit a67057e

Browse files
committed
refactor: improve code formatting and readability in DirectoryTreeViewer and styles
1 parent ac3dd4f commit a67057e

File tree

2 files changed

+82
-67
lines changed

2 files changed

+82
-67
lines changed

lib/file_tree_view.dart

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class DirectoryTreeViewer extends StatelessWidget {
5050
/// Callback function when a directory is right-clicked.
5151
final void Function(Directory, TapDownDetails)? onDirSecondaryTap;
5252

53-
5453
///Additional folder action widgets
5554
final List<Widget>? folderActions;
5655

@@ -61,32 +60,31 @@ class DirectoryTreeViewer extends StatelessWidget {
6160
final Widget Function(String fileExtension)? fileIconBuilder;
6261

6362
/// Constructs a [DirectoryTreeViewer] with the given properties.
64-
const DirectoryTreeViewer(
65-
{super.key,
66-
required this.rootPath,
67-
this.onFileTap,
68-
this.onFileSecondaryTap,
69-
this.onDirTap,
70-
this.onDirSecondaryTap,
71-
this.folderActions,
72-
this.fileActions,
73-
this.folderStyle,
74-
this.fileStyle,
75-
this.isUnfoldedFirst = true,
76-
this.editingFieldStyle,
77-
this.enableCreateFileOption = false,
78-
this.enableCreateFolderOption = false,
79-
this.enableDeleteFileOption = false,
80-
this.enableDeleteFolderOption = false,
81-
this.fileIconBuilder});
63+
const DirectoryTreeViewer({
64+
super.key,
65+
required this.rootPath,
66+
this.onFileTap,
67+
this.onFileSecondaryTap,
68+
this.onDirTap,
69+
this.onDirSecondaryTap,
70+
this.folderActions,
71+
this.fileActions,
72+
this.folderStyle,
73+
this.fileStyle,
74+
this.isUnfoldedFirst = true,
75+
this.editingFieldStyle,
76+
this.enableCreateFileOption = false,
77+
this.enableCreateFolderOption = false,
78+
this.enableDeleteFileOption = false,
79+
this.enableDeleteFolderOption = false,
80+
this.fileIconBuilder,
81+
});
8282

8383
@override
8484
Widget build(BuildContext context) {
8585
/// Check if the platform is Web or WASM, and display a message if it is.
8686
if (kIsWasm || kIsWeb) {
87-
return const AlertDialog(
88-
title: Text("Web platform is not supported"),
89-
);
87+
return const AlertDialog(title: Text("Web platform is not supported"));
9088
}
9189
isParentOpen = isUnfoldedFirst;
9290
return DirectoryTreeStateProvider(
@@ -229,7 +227,9 @@ class FoldableDirectoryTree extends StatefulWidget {
229227
/// Recursively builds the directory tree for a given [directory] using [stateNotifier] to manage folder states.
230228
class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
231229
Widget _buildDirectoryTree(
232-
Directory directory, DirectoryTreeStateNotifier stateNotifier) {
230+
Directory directory,
231+
DirectoryTreeStateNotifier stateNotifier,
232+
) {
233233
final entries = directory.listSync();
234234
entries.sort((a, b) {
235235
if (a is Directory && b is File) return -1;
@@ -266,30 +266,33 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
266266
children: [
267267
directory.path != widget.rootPath
268268
? (stateNotifier.isUnfolded(directory.path, widget.rootPath)
269-
? widget.folderStyle?.folderOpenedicon ??
270-
FolderStyle().folderOpenedicon
271-
: widget.folderStyle?.folderClosedicon ??
272-
FolderStyle().folderClosedicon)
269+
? widget.folderStyle?.folderOpenedicon ??
270+
FolderStyle().folderOpenedicon
271+
: widget.folderStyle?.folderClosedicon ??
272+
FolderStyle().folderClosedicon)
273273
: isParentOpen
274274
? widget.folderStyle?.rootFolderOpenedIcon ??
275-
FolderStyle().rootFolderOpenedIcon
275+
FolderStyle().rootFolderOpenedIcon
276276
: widget.folderStyle?.rootFolderClosedIcon ??
277-
FolderStyle().rootFolderClosedIcon,
277+
FolderStyle().rootFolderClosedIcon,
278278
const SizedBox(width: 8),
279279
Text(
280280
path.basename(directory.path),
281-
style: widget.folderStyle?.folderNameStyle ??
281+
style:
282+
widget.folderStyle?.folderNameStyle ??
282283
FolderStyle().folderNameStyle,
283284
),
284285
SizedBox(
285-
width: widget.folderStyle?.itemGap ?? FolderStyle().itemGap),
286+
width: widget.folderStyle?.itemGap ?? FolderStyle().itemGap,
287+
),
286288
if (widget.enableCreateFileOption &&
287289
stateNotifier.isUnfolded(directory.path, widget.rootPath) &&
288290
currentDir == directory.path)
289291
IconButton(
290292
onPressed: () =>
291293
stateNotifier.startCreating(directory.path, false),
292-
icon: widget.folderStyle?.iconForCreateFile ??
294+
icon:
295+
widget.folderStyle?.iconForCreateFile ??
293296
FolderStyle().iconForCreateFile,
294297
),
295298
if (widget.enableCreateFolderOption &&
@@ -298,7 +301,8 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
298301
IconButton(
299302
onPressed: () =>
300303
stateNotifier.startCreating(directory.path, true),
301-
icon: widget.folderStyle?.iconForCreateFolder ??
304+
icon:
305+
widget.folderStyle?.iconForCreateFolder ??
302306
FolderStyle().iconForCreateFolder,
303307
),
304308
if (widget.enableDeleteFolderOption &&
@@ -325,7 +329,9 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
325329
...entries.map((entry) {
326330
if (entry is Directory) {
327331
return _buildDirectoryTree(
328-
Directory(entry.path), stateNotifier);
332+
Directory(entry.path),
333+
stateNotifier,
334+
);
329335
} else if (entry is File) {
330336
return _buildFileItem(entry);
331337
}
@@ -341,15 +347,17 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
341347
}
342348

343349
Widget _buildNewEntryField(
344-
Directory parent, DirectoryTreeStateNotifier stateNotifier) {
350+
Directory parent,
351+
DirectoryTreeStateNotifier stateNotifier,
352+
) {
345353
TextEditingController controller = TextEditingController();
346354
return Row(
347355
children: [
348356
stateNotifier.isFolderCreation
349357
? widget.editingFieldStyle?.folderIcon ??
350-
EditingFieldStyle().folderIcon
358+
EditingFieldStyle().folderIcon
351359
: widget.editingFieldStyle?.fileIcon ??
352-
EditingFieldStyle().fileIcon,
360+
EditingFieldStyle().fileIcon,
353361
const SizedBox(width: 8),
354362
Expanded(
355363
child: SizedBox(
@@ -364,7 +372,8 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
364372
cursorColor: widget.editingFieldStyle?.cursorColor,
365373
controller: controller,
366374
autofocus: true,
367-
decoration: widget.editingFieldStyle?.textfieldDecoration ??
375+
decoration:
376+
widget.editingFieldStyle?.textfieldDecoration ??
368377
EditingFieldStyle().textfieldDecoration,
369378
onSubmitted: (value) {
370379
if (value.trim().isNotEmpty) {
@@ -381,7 +390,8 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
381390
),
382391
),
383392
IconButton(
384-
icon: widget.editingFieldStyle?.doneIcon ??
393+
icon:
394+
widget.editingFieldStyle?.doneIcon ??
385395
EditingFieldStyle().doneIcon,
386396
onPressed: () {
387397
if (controller.text.trim().isNotEmpty) {
@@ -396,7 +406,8 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
396406
},
397407
),
398408
IconButton(
399-
icon: widget.editingFieldStyle?.cancelIcon ??
409+
icon:
410+
widget.editingFieldStyle?.cancelIcon ??
400411
EditingFieldStyle().cancelIcon,
401412
onPressed: () {
402413
stateNotifier.stopCreating();
@@ -431,17 +442,20 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
431442
const SizedBox(width: 8),
432443
Text(
433444
path.basename(file.path),
434-
style: widget.fileStyle?.fileNameStyle ?? FileStyle().fileNameStyle,
445+
style:
446+
widget.fileStyle?.fileNameStyle ?? FileStyle().fileNameStyle,
435447
),
436448
...widget.fileActions ?? [],
437449
if (widget.enableDeleteFileOption)
438450
IconButton(
439-
onPressed: () {
440-
file.deleteSync(recursive: true);
441-
setState(() {});
442-
},
443-
icon: widget.fileStyle?.iconForDeleteFile ??
444-
FileStyle().iconForDeleteFile)
451+
onPressed: () {
452+
file.deleteSync(recursive: true);
453+
setState(() {});
454+
},
455+
icon:
456+
widget.fileStyle?.iconForDeleteFile ??
457+
FileStyle().iconForDeleteFile,
458+
),
445459
],
446460
),
447461
),
@@ -455,13 +469,11 @@ class _FoldableDirectoryTreeState extends State<FoldableDirectoryTree> {
455469
final rootDirectory = Directory(widget.rootPath);
456470

457471
if (!rootDirectory.existsSync()) {
458-
return const Center(
459-
child: Text('Directory does not exist'),
460-
);
472+
return const Center(child: Text('Directory does not exist'));
461473
}
462474

463475
return SingleChildScrollView(
464476
child: _buildDirectoryTree(rootDirectory, stateNotifier),
465477
);
466478
}
467-
}
479+
}

lib/style.dart

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class FolderStyle {
3737
this.rootFolderOpenedIcon = const Icon(Icons.keyboard_arrow_down_sharp),
3838
this.folderNameStyle = const TextStyle(),
3939
this.iconForCreateFolder = const Icon(Icons.create_new_folder),
40-
this.iconForCreateFile =
41-
const Icon(FontAwesomeIcons.fileCirclePlus, size: 20),
40+
this.iconForCreateFile = const Icon(
41+
FontAwesomeIcons.fileCirclePlus,
42+
size: 20,
43+
),
4244
this.iconForDeleteFolder = const Icon(Icons.delete),
4345
this.folderClosedicon = const Icon(Icons.folder),
4446
this.folderOpenedicon = const Icon(Icons.folder_open),
@@ -106,18 +108,19 @@ class EditingFieldStyle {
106108
final TextStyle? textStyle;
107109

108110
///Custom styling for the [TextField] for creating files/folders.
109-
EditingFieldStyle(
110-
{this.textFieldHeight = 30,
111-
this.textFieldWidth = double.infinity,
112-
this.cursorHeight = 20,
113-
this.cursorWidth = 2.0,
114-
this.cursorRadius,
115-
this.cursorColor,
116-
this.verticalTextAlign,
117-
this.textStyle,
118-
this.textfieldDecoration = const InputDecoration(),
119-
this.folderIcon = const Icon(Icons.folder),
120-
this.fileIcon = const Icon(Icons.edit_document),
121-
this.doneIcon = const Icon(Icons.check),
122-
this.cancelIcon = const Icon(Icons.close)});
111+
EditingFieldStyle({
112+
this.textFieldHeight = 30,
113+
this.textFieldWidth = double.infinity,
114+
this.cursorHeight = 20,
115+
this.cursorWidth = 2.0,
116+
this.cursorRadius,
117+
this.cursorColor,
118+
this.verticalTextAlign,
119+
this.textStyle,
120+
this.textfieldDecoration = const InputDecoration(),
121+
this.folderIcon = const Icon(Icons.folder),
122+
this.fileIcon = const Icon(Icons.edit_document),
123+
this.doneIcon = const Icon(Icons.check),
124+
this.cancelIcon = const Icon(Icons.close),
125+
});
123126
}

0 commit comments

Comments
 (0)