Skip to content
Snippets Groups Projects
Commit 3775c77b authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: new search bar approach

parent 8571677d
No related branches found
No related tags found
1 merge request!10Clhp map
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
......@@ -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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment