From 223feb8c5c874e2dd866ba0197895d7ffa4dd061 Mon Sep 17 00:00:00 2001 From: Jonasz Bigda Date: Sun, 27 Dec 2020 16:22:44 +0100 Subject: [PATCH] fixes --- lib/components/mapView.dart | 10 ++++++-- lib/components/searchBar.dart | 35 +++++++++++++++++---------- lib/services.dart | 45 +++++++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 20 deletions(-) diff --git a/lib/components/mapView.dart b/lib/components/mapView.dart index 202c12b..479826e 100644 --- a/lib/components/mapView.dart +++ b/lib/components/mapView.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import '../services.dart'; import 'package:geolocator/geolocator.dart'; +import 'restaurantView.dart'; class MapView extends StatefulWidget { @override @@ -30,6 +31,11 @@ class MapViewState extends State { position: LatLng( thisRestaurant.coordinates[0], thisRestaurant.coordinates[1]), infoWindow: InfoWindow( + onTap: () => Navigator.push( + context, + MaterialPageRoute( + builder: (context) => + RestaurantView(id: thisRestaurant.id))), title: '${thisRestaurant.name}', snippet: 'Kuchnia: ${thisRestaurant.type}')); markers[markerId] = marker; @@ -65,7 +71,7 @@ class MapViewState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, - children: [Text("error...")], + children: [Text("Błąd...")], ), ); } else { @@ -81,7 +87,7 @@ class MapViewState extends State { ), Padding( padding: EdgeInsets.only(top: 16), - child: Text('Awaiting result...'), + child: Text('Szukam restauracji...'), ) ], ), diff --git a/lib/components/searchBar.dart b/lib/components/searchBar.dart index f52d8bf..f377795 100644 --- a/lib/components/searchBar.dart +++ b/lib/components/searchBar.dart @@ -17,7 +17,7 @@ class MenuiSearchBarState extends State { final _controller = TextEditingController(); String previousText = ''; - var suggestions = []; + MenuiSuggestions suggestions; bool suggestionsOpen = false; @override @@ -29,20 +29,19 @@ class MenuiSearchBarState extends State { Future fetchAutocomplete() async { if (_controller.text.isNotEmpty && _controller.text != previousText) { previousText = _controller.text; - final List results = + final MenuiSuggestions results = await services.fetchAutocomplete(_controller.text); - setState(() { suggestions = results; }); - if (!suggestionsOpen && results.isNotEmpty) { + if (!suggestionsOpen && !results.suggestionsEmpty()) { setState(() { suggestionsOpen = true; }); _overlayEntry = _createOverlayEntry(); Overlay.of(context).insert(_overlayEntry); - } else if (results.isEmpty) { + } else if (results.suggestionsEmpty()) { hideSuggestions(); } } else { @@ -63,7 +62,7 @@ class MenuiSearchBarState extends State { if (suggestionsOpen) { _overlayEntry.remove(); setState(() { - suggestions = []; + suggestions = new MenuiSuggestions.empty(); suggestionsOpen = false; }); } @@ -102,19 +101,29 @@ class MenuiSearchBarState extends State { child: ListView.builder( padding: EdgeInsets.zero, shrinkWrap: true, - itemCount: suggestions.length, + itemCount: suggestions.getLenght(), itemBuilder: (context, index) { return ListTile( onTap: () { - _controller.text = suggestions[index]; + _controller.text = + suggestions.getSuggestion(index); searchRestaurantsByString(); }, - leading: Icon( - Icons.search_rounded, - color: Colors.grey, - ), + leading: (() { + if (suggestions.isCity(index)) { + return Icon( + Icons.location_city_rounded, + color: Colors.grey, + ); + } else { + return Icon( + Icons.fastfood_rounded, + color: Colors.grey, + ); + } + }()), title: Text( - suggestions[index], + suggestions.getSuggestion(index), style: TextStyle(color: Colors.grey), ), ); diff --git a/lib/services.dart b/lib/services.dart index f6f4e02..45c84e6 100644 --- a/lib/services.dart +++ b/lib/services.dart @@ -8,7 +8,7 @@ class MenuiServices { final String backendURL = 'https://api.menui.pl/'; final MenuiSettings settings = new MenuiSettings(); - Future> fetchAutocomplete(String text) async { + Future fetchAutocomplete(String text) async { final response = await http.get('${backendURL}search/autocomplete?string=$text'); if (response.statusCode == 200 || response.statusCode == 304) { @@ -17,12 +17,11 @@ class MenuiServices { final List cities = citiesDynamic.cast().toList(); final List restaurants = restaurantsDynamic.cast().toList(); - final List result = [...cities, ...restaurants]; - + final MenuiSuggestions result = + new MenuiSuggestions(cities: cities, names: restaurants); return result; } else { - print("coś tu nie zagrało podczas pobierania sugestii"); - return []; + return new MenuiSuggestions(cities: [], names: []); } } @@ -460,3 +459,39 @@ class MarkersAndLocation { MarkersAndLocation({@required this.markers, @required this.coordinates}); } + +class MenuiSuggestions { + final List cities; + final List names; + + MenuiSuggestions({@required this.cities, @required this.names}); + + MenuiSuggestions.empty({this.cities = const [], this.names = const []}); + + bool suggestionsEmpty() { + if (this.cities.isEmpty && this.names.isEmpty) { + return true; + } else { + return false; + } + } + + int getLenght() { + int lenght = this.cities.length + this.names.length; + return lenght; + } + + String getSuggestion(int index) { + List suggestions = new List.from(this.cities) + ..addAll(this.names); + return suggestions[index]; + } + + bool isCity(int index) { + if (index > (this.cities.length - 1)) { + return false; + } else { + return true; + } + } +}