FolderListing: Show a leading arrow if the Folder has SubFolders

Also change the Arrow if the folder is expanded.
This commit is contained in:
Vishesh Handa
2019-12-04 00:51:41 +01:00
parent 56ccf00260
commit 3a3f9c7ad6
2 changed files with 47 additions and 44 deletions

View File

@ -37,6 +37,14 @@ class NoteFolder {
return basename(folderPath); return basename(folderPath);
} }
bool get hasSubFolders {
return entities.firstWhere((e) => e.isFolder, orElse: () => null) != null;
}
bool get hasNotes {
return entities.firstWhere((e) => e.isNote, orElse: () => null) != null;
}
// Recurisvely gets all Notes within this folder // Recurisvely gets all Notes within this folder
List<Note> getAllNotes() { List<Note> getAllNotes() {
var notes = <Note>[]; var notes = <Note>[];

View File

@ -9,10 +9,10 @@ import 'package:gitjournal/note_folder.dart';
typedef void ParentSelectChanged(bool isSelected); typedef void ParentSelectChanged(bool isSelected);
class TreeView extends StatelessWidget { class TreeView extends StatelessWidget {
final List<Parent> parentList; final List<FolderTile> parentList;
TreeView({ TreeView({
this.parentList = const <Parent>[], this.parentList = const <FolderTile>[],
}); });
@override @override
@ -26,38 +26,29 @@ class TreeView extends StatelessWidget {
} }
} }
/// # Parent widget class FolderTile extends StatefulWidget {
/// final NoteFolder folder;
/// The [Parent] widget holds the [Parent.parent] widget and
/// [Parent.childList] which is a [List] of child widgets.
///
/// The [Parent] widget is wrapped around a [Column]. The [Parent.childList]
/// is collapsed by default. When clicked the child widget is expanded.
class Parent extends StatefulWidget {
final Widget parent;
final ChildList childList; final ChildList childList;
final MainAxisSize mainAxisSize; final MainAxisSize mainAxisSize;
final CrossAxisAlignment crossAxisAlignment; final CrossAxisAlignment crossAxisAlignment;
final MainAxisAlignment mainAxisAlignment; final MainAxisAlignment mainAxisAlignment;
final ParentSelectChanged callback; final ParentSelectChanged callback;
final Key key;
Parent({ FolderTile({
@required this.parent, @required this.folder,
@required this.childList, @required this.childList,
this.mainAxisAlignment = MainAxisAlignment.center, this.mainAxisAlignment = MainAxisAlignment.center,
this.crossAxisAlignment = CrossAxisAlignment.start, this.crossAxisAlignment = CrossAxisAlignment.start,
this.mainAxisSize = MainAxisSize.min, this.mainAxisSize = MainAxisSize.min,
this.callback, this.callback,
this.key,
}); });
@override @override
ParentState createState() => ParentState(); FolderTileState createState() => FolderTileState();
} }
class ParentState extends State<Parent> { class FolderTileState extends State<FolderTile> {
bool _isSelected = false; bool _isExpanded = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -67,7 +58,7 @@ class ParentState extends State<Parent> {
mainAxisAlignment: widget.mainAxisAlignment, mainAxisAlignment: widget.mainAxisAlignment,
children: <Widget>[ children: <Widget>[
GestureDetector( GestureDetector(
child: widget.parent, child: _buildFolderTile(),
onTap: expand, onTap: expand,
), ),
_getChild(), _getChild(),
@ -75,10 +66,29 @@ class ParentState extends State<Parent> {
); );
} }
Widget _buildFolderTile() {
var folder = widget.folder;
var ic = _isExpanded ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down;
var trailling = folder.hasSubFolders
? IconButton(
icon: Icon(ic),
onPressed: expand,
)
: null;
return Card(
child: ListTile(
leading: Icon(Icons.folder),
title: Text(folder.name),
trailing: trailling,
),
);
}
void expand() { void expand() {
if (widget.callback != null) widget.callback(_isSelected); if (widget.callback != null) widget.callback(_isExpanded);
setState(() { setState(() {
_isSelected = _toggleBool(_isSelected); _isExpanded = _toggleBool(_isExpanded);
}); });
} }
@ -87,14 +97,14 @@ class ParentState extends State<Parent> {
} }
Widget _getChild() { Widget _getChild() {
return _isSelected ? widget.childList : Container(); return _isExpanded ? widget.childList : Container();
} }
} }
/// # ChildList widget /// # ChildList widget
/// ///
/// The [ChildList] widget holds a [List] of widget which will be displayed as /// The [ChildList] widget holds a [List] of widget which will be displayed as
/// children of the [Parent] widget /// children of the [FolderTile] widget
class ChildList extends StatelessWidget { class ChildList extends StatelessWidget {
final List<Widget> children; final List<Widget> children;
final MainAxisAlignment mainAxisAlignment; final MainAxisAlignment mainAxisAlignment;
@ -139,16 +149,16 @@ class FolderListingScreen extends StatelessWidget {
); );
} }
List<Parent> _constructParentList(List<NoteFSEntity> entities) { List<FolderTile> _constructParentList(List<NoteFSEntity> entities) {
var parents = <Parent>[]; var parents = <FolderTile>[];
entities.forEach((entity) { entities.forEach((entity) {
if (entity.isNote) { if (entity.isNote) {
return; return;
} }
var folder = entity.folder; var folder = entity.folder;
var p = Parent( var p = FolderTile(
parent: _buildFolderTile(folder.name), folder: folder,
childList: _constructChildList(folder.entities), childList: _constructChildList(folder.entities),
); );
@ -165,8 +175,8 @@ class FolderListingScreen extends StatelessWidget {
} }
var folder = entity.folder; var folder = entity.folder;
var p = Parent( var p = FolderTile(
parent: _buildFolderTile(folder.name), folder: folder,
childList: _constructChildList(folder.entities), childList: _constructChildList(folder.entities),
); );
@ -175,19 +185,4 @@ class FolderListingScreen extends StatelessWidget {
return ChildList(children: children); return ChildList(children: children);
} }
Widget _buildFolderTile(String name) {
// FIXME: The trailing icon should change based on if it is expanded or not
return Card(
child: ListTile(
leading: Icon(Icons.folder),
title: Text(name),
trailing: IconButton(
icon: Icon(Icons.navigate_next),
onPressed: () {},
),
),
);
}
} }