From 3775c77b23fc8fdea04e16a21fe7b40dcaddf4e7 Mon Sep 17 00:00:00 2001
From: Sara <sarasdj@stud.ntnu.no>
Date: Thu, 4 Apr 2024 12:58:19 +0200
Subject: [PATCH] update: new search bar approach

---
 app/lib/pages/default_page.dart  | 83 ++++++++++++++++++++++++++++++--
 app/lib/widgets/main_layout.dart | 71 +--------------------------
 app/lib/widgets/search_bar.dart  | 45 -----------------
 3 files changed, 80 insertions(+), 119 deletions(-)
 delete mode 100644 app/lib/widgets/search_bar.dart

diff --git a/app/lib/pages/default_page.dart b/app/lib/pages/default_page.dart
index e11983e5..8e9d3737 100644
--- a/app/lib/pages/default_page.dart
+++ b/app/lib/pages/default_page.dart
@@ -1,5 +1,6 @@
 import 'dart:async';
 import 'dart:typed_data';
+import 'package:fuzzy/fuzzy.dart';
 import 'package:flutter/material.dart';
 
 import '../consts.dart';
@@ -18,7 +19,6 @@ class DefaultPage extends StatefulWidget {
 
 class _DefaultPageState extends State<DefaultPage> {
   late Timer _timer;
-  bool showBar = false;
   bool serverConnection = true;
   bool dialogShown = false;
 
@@ -111,9 +111,12 @@ class _DefaultPageState extends State<DefaultPage> {
                   color: Colors.white54
               ),
               onPressed: () {
-                setState(() {
+                showSearch(
+                    context: context,
+                    delegate: _CustomSearchDelegate());
+                /*setState(() {
                   showBar = !showBar;
-                });
+                });*/
               },
             ),
           ],
@@ -132,7 +135,6 @@ class _DefaultPageState extends State<DefaultPage> {
                       markerList: markerList,
                       relation: relation,
                       serverConnection: serverConnection,
-                      showSearchBar: showBar,
                     ),
                   ],
                 ),
@@ -143,3 +145,76 @@ class _DefaultPageState extends State<DefaultPage> {
     );
   }
 }
+
+class _CustomSearchDelegate extends SearchDelegate {
+  List<String> searchItems = [ // NB temp values
+    "Mjøsa",
+    "Bogstadsvannet",
+    "Einavatnet",
+    "Femsjøen",
+    "Femunden",
+    "Fjellsjøen",
+    "Gjende",
+    "Gjersjøen"
+  ];
+
+  @override
+  List<Widget> buildActions(BuildContext context) {
+    return [
+      IconButton( // Clear query
+          icon: const Icon(Icons.clear),
+          onPressed: () {
+            query = '';
+          },
+      )
+    ];
+  }
+
+  @override
+  Widget buildLeading(BuildContext context) {
+    return IconButton( // Close search bar
+        icon: const Icon(Icons.arrow_back),
+        onPressed: () {
+          close(context, null);
+        },
+      );
+  }
+
+  @override
+  Widget buildResults(BuildContext context) {
+    List<String> searchResults = [];
+    final options = FuzzyOptions(threshold: 0.3, findAllMatches: true);
+    final matcher = Fuzzy(searchItems, options: options);
+    final results = matcher.search(query);
+    searchResults = results.map((result) => result.item as String).toList();
+
+    return ListView.builder(
+      itemCount: searchResults.length,
+      itemBuilder: (context, index) {
+        var result = searchResults[index];
+        return ListTile(
+          title: Text(result),
+        );
+      },
+    );
+  }
+
+  @override
+  Widget buildSuggestions(BuildContext context) {
+    List<String> searchResults = [];
+    final options = FuzzyOptions(threshold: 0.3, findAllMatches: true);
+    final matcher = Fuzzy(searchItems, options: options);
+    final results = matcher.search(query);
+    searchResults = results.map((result) => result.item as String).toList();
+
+    return ListView.builder(
+      itemCount: searchResults.length,
+      itemBuilder: (context, index) {
+        var result = searchResults[index];
+        return ListTile(
+          title: Text(result),
+        );
+      },
+    );
+  }
+}
\ No newline at end of file
diff --git a/app/lib/widgets/main_layout.dart b/app/lib/widgets/main_layout.dart
index 0a579dc4..a4391d8f 100644
--- a/app/lib/widgets/main_layout.dart
+++ b/app/lib/widgets/main_layout.dart
@@ -6,7 +6,6 @@ import 'package:google_fonts/google_fonts.dart';
 import 'package:shared_preferences/shared_preferences.dart';
 
 import 'osm_layer.dart';
-import 'search_bar.dart';
 import 'stat_charts.dart';
 import '../../consts.dart';
 import 'choropleth_map.dart';
@@ -20,13 +19,11 @@ class MapContainerWidget extends StatefulWidget {
   final List<Measurement> markerList;
   final  Uint8List relation;
   final bool serverConnection;
-  final bool showSearchBar;
 
   const MapContainerWidget({Key? key,
     required this.markerList,
     required this.relation,
     required this.serverConnection,
-    required this.showSearchBar
   }) : super(key: key);
 
   @override
@@ -91,17 +88,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
         double boxHeight = 1.4;
         return Column(
           children: [
-            const SizedBox(height: 12),
-            Padding(
-              padding: const EdgeInsets.symmetric(horizontal: 16.0),
-              child: AnimatedOpacity(
-                duration: const Duration(milliseconds: 300),
-                curve: Curves.easeInOut,
-                opacity: widget.showSearchBar ? 1.0 : 0.0,
-                child: const _SearchBar(width: 350), // NB temp hardcoded value
-              ),
-            ),
-            const SizedBox(height: 12),
+            const SizedBox(height: contPadding*1.5),
             ClipRRect(
               borderRadius: BorderRadius.circular(20),
               child: Stack( // Stack of quick view, map layer, satellite layer, and buttons
@@ -280,59 +267,3 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
     );
   }
 }
-class _SearchBar extends StatefulWidget {
-  final double width;
-  const _SearchBar({
-    Key? key,
-    required this.width,
-  }) : super(key: key);
-
-  @override
-  _SearchBarState createState() => _SearchBarState();
-}
-
-class _SearchBarState extends State<_SearchBar> {
-  final List<String> testSearchNames = [
-    "Mjøsa",
-    "Bogstadsvannet",
-    "Einavatnet",
-    "Femsjøen",
-    "Femunden",
-    "Fjellsjøen",
-    "Gjende",
-    "Gjersjøen"
-  ];
-
-  List<String> searchLakeNames(String query) {
-    final options = FuzzyOptions(threshold: 0.3, findAllMatches: true);
-    final matcher = Fuzzy(testSearchNames, options: options);
-    final results = matcher.search(query);
-    return results.map((result) => result.item as String).toList();
-  }
-
-  @override
-  Widget build(BuildContext context) {
-    return Padding(
-      padding: const EdgeInsets.symmetric(horizontal: 16.0),
-      child: SizedBox(
-        width: widget.width,
-        height: 32,
-        child: TextField(
-          decoration: InputDecoration(
-            contentPadding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
-            hintText: 'Search...',
-            filled: true,
-            fillColor: Colors.white,
-            border: OutlineInputBorder(
-              borderRadius: BorderRadius.circular(15.0),
-            ),
-          ),
-          onChanged: (value) {
-            setState(() {}); // Trigger a rebuild when the text changes
-            searchLakeNames(value);
-            },
-        ),
-      ),
-    );
-  }
-}
\ No newline at end of file
diff --git a/app/lib/widgets/search_bar.dart b/app/lib/widgets/search_bar.dart
deleted file mode 100644
index 7ac397e3..00000000
--- a/app/lib/widgets/search_bar.dart
+++ /dev/null
@@ -1,45 +0,0 @@
-import 'package:flutter/material.dart';
-import 'package:fuzzy/fuzzy.dart';
-
-/*
-class _SearchBar extends StatelessWidget {
-  final List<String> testSearchNames = [
-    "Mjøsa",
-    "Bogstadsvannet",
-    "Einavatnet",
-    "Femsjøen",
-    "Femunden",
-    "Fjellsjøen",
-    "Gjende",
-    "Gjersjøen"
-  ];
-  // Searching function for lake names using Fuzzy library
-  List<String> searchLakeNames(String query) {
-    final options = FuzzyOptions(threshold: 0.3, findAllMatches: true);
-    final matcher = Fuzzy(testSearchNames, options: options);
-    final results = matcher.search(query);
-
-    // Extracting lake names from the results and casting them to strings
-    return results.map((result) => result.item as String).toList();
-  }
-
-  @override
-  Widget build(BuildContext context) {
-    return Padding(
-      padding: const EdgeInsets.symmetric(horizontal: 16.0),
-      child: TextField(
-        decoration: InputDecoration(
-          hintText: 'Search...',
-          filled: true,
-          fillColor: Colors.white,
-          border: OutlineInputBorder(
-            borderRadius: BorderRadius.circular(10.0),
-          ),
-        ),
-        onChanged: (value) {
-          searchLakeNames(value);
-        },
-      ),
-    );
-  }
-}*/
\ No newline at end of file
-- 
GitLab