From 453a6c518dbfc66af53726dc4c537e2dc5751f41 Mon Sep 17 00:00:00 2001
From: Vishesh Handa <me@vhanda.in>
Date: Wed, 11 Dec 2019 21:18:12 +0100
Subject: [PATCH] Allow Sub Folders to be created

Fixes #34
---
 lib/screens/folder_listing.dart | 74 +++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 26 deletions(-)

diff --git a/lib/screens/folder_listing.dart b/lib/screens/folder_listing.dart
index b1d0105f..6f279677 100644
--- a/lib/screens/folder_listing.dart
+++ b/lib/screens/folder_listing.dart
@@ -52,20 +52,36 @@ class _FolderListingScreenState extends State<FolderListingScreen> {
           return [
             const PopupMenuItem<String>(
               child: Text("Rename Folder"),
-              value: "Rename Folder",
+              value: "Rename",
+            ),
+            const PopupMenuItem<String>(
+              child: Text("Create Sub-Folder"),
+              value: "Create",
             )
           ];
         },
-        onSelected: (String _) async {
-          var folderName = await showDialog(
-            context: context,
-            builder: (_) => RenameFolderDialog(selectedFolder),
-          );
-          if (folderName is String) {
-            final container = StateContainer.of(context);
-            container.renameFolder(selectedFolder, folderName);
-            _folderTreeViewKey.currentState.resetSelection();
+        onSelected: (String value) async {
+          if (value == "Rename") {
+            var folderName = await showDialog(
+              context: context,
+              builder: (_) => RenameFolderDialog(selectedFolder),
+            );
+            if (folderName is String) {
+              final container = StateContainer.of(context);
+              container.renameFolder(selectedFolder, folderName);
+            }
+          } else if (value == "Create") {
+            var folderName = await showDialog(
+              context: context,
+              builder: (_) => CreateFolderAlertDialog(),
+            );
+            if (folderName is String) {
+              final container = StateContainer.of(context);
+              container.createFolder(selectedFolder, folderName);
+            }
           }
+
+          _folderTreeViewKey.currentState.resetSelection();
         },
       );
     }
@@ -97,21 +113,16 @@ class _FolderListingScreenState extends State<FolderListingScreen> {
   }
 }
 
-class CreateFolderButton extends StatefulWidget {
-  @override
-  _CreateFolderButtonState createState() => _CreateFolderButtonState();
-}
-
-class _CreateFolderButtonState extends State<CreateFolderButton> {
-  final TextEditingController _textController = TextEditingController();
-
+class CreateFolderButton extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return FloatingActionButton(
       key: const ValueKey("FAB"),
       onPressed: () async {
-        var folderName =
-            await showDialog(context: context, builder: _buildAlert);
+        var folderName = await showDialog(
+          context: context,
+          builder: (_) => CreateFolderAlertDialog(),
+        );
         if (folderName is String) {
           final container = StateContainer.of(context);
           final notesFolder = Provider.of<NotesFolder>(context);
@@ -122,14 +133,19 @@ class _CreateFolderButtonState extends State<CreateFolderButton> {
       child: Icon(Icons.add),
     );
   }
+}
+
+class CreateFolderAlertDialog extends StatefulWidget {
+  @override
+  _CreateFolderAlertDialogState createState() =>
+      _CreateFolderAlertDialogState();
+}
+
+class _CreateFolderAlertDialogState extends State<CreateFolderAlertDialog> {
+  final TextEditingController _textController = TextEditingController();
 
   @override
-  void dispose() {
-    _textController.dispose();
-    super.dispose();
-  }
-
-  Widget _buildAlert(BuildContext context) {
+  Widget build(BuildContext context) {
     var form = Form(
       child: Column(
         mainAxisSize: MainAxisSize.min,
@@ -167,6 +183,12 @@ class _CreateFolderButtonState extends State<CreateFolderButton> {
       content: form,
     );
   }
+
+  @override
+  void dispose() {
+    _textController.dispose();
+    super.dispose();
+  }
 }
 
 class RenameFolderDialog extends StatefulWidget {