This commit is contained in:
2020-12-27 16:22:44 +01:00
parent 7ce7a9205d
commit 223feb8c5c
3 changed files with 70 additions and 20 deletions

View File

@@ -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<MapView> {
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<MapView> {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [Text("error...")],
children: [Text("Błąd...")],
),
);
} else {
@@ -81,7 +87,7 @@ class MapViewState extends State<MapView> {
),
Padding(
padding: EdgeInsets.only(top: 16),
child: Text('Awaiting result...'),
child: Text('Szukam restauracji...'),
)
],
),

View File

@@ -17,7 +17,7 @@ class MenuiSearchBarState extends State<MenuiSearchBar> {
final _controller = TextEditingController();
String previousText = '';
var suggestions = <String>[];
MenuiSuggestions suggestions;
bool suggestionsOpen = false;
@override
@@ -29,20 +29,19 @@ class MenuiSearchBarState extends State<MenuiSearchBar> {
Future<void> fetchAutocomplete() async {
if (_controller.text.isNotEmpty && _controller.text != previousText) {
previousText = _controller.text;
final List<String> 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<MenuiSearchBar> {
if (suggestionsOpen) {
_overlayEntry.remove();
setState(() {
suggestions = [];
suggestions = new MenuiSuggestions.empty();
suggestionsOpen = false;
});
}
@@ -102,19 +101,29 @@ class MenuiSearchBarState extends State<MenuiSearchBar> {
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,
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),
),
);

View File

@@ -8,7 +8,7 @@ class MenuiServices {
final String backendURL = 'https://api.menui.pl/';
final MenuiSettings settings = new MenuiSettings();
Future<List<String>> fetchAutocomplete(String text) async {
Future<MenuiSuggestions> 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<String> cities = citiesDynamic.cast<String>().toList();
final List<String> restaurants =
restaurantsDynamic.cast<String>().toList();
final List<String> 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<String> cities;
final List<String> 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<String> suggestions = new List<String>.from(this.cities)
..addAll(this.names);
return suggestions[index];
}
bool isCity(int index) {
if (index > (this.cities.length - 1)) {
return false;
} else {
return true;
}
}
}