Allow clicking on favorites

This commit is contained in:
Reimar 2024-08-27 11:48:11 +02:00
parent 7b884e3167
commit 39e0db1f5d
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268

View File

@ -50,7 +50,7 @@ class _MyHomePageState extends State<MyHomePage> {
List<Favorite> _favorites = []; List<Favorite> _favorites = [];
LatLng? _selectedPoint; LatLng? _selectedPoint;
void _showLocation(TapPosition _, LatLng point) async { void _onTap(TapPosition _, LatLng point) async {
setState(() => _selectedPoint = point); setState(() => _selectedPoint = point);
final dynamic location; final dynamic location;
@ -60,6 +60,11 @@ class _MyHomePageState extends State<MyHomePage> {
headers: {'User-Agent': 'SkanTravels/1.0'}, headers: {'User-Agent': 'SkanTravels/1.0'},
); );
if (mounted && response.statusCode != 200) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Unable to fetch information about this location (HTTP ${response.statusCode})')));
return;
}
location = jsonDecode(response.body); location = jsonDecode(response.body);
} catch (_) { } catch (_) {
if (!mounted) return; if (!mounted) return;
@ -73,6 +78,14 @@ class _MyHomePageState extends State<MyHomePage> {
if (!mounted) return; if (!mounted) return;
if (location['name'] != null && location['display_name'] != null) {
await _showLocation(point, location['name'], location['display_name']);
}
setState(() => _selectedPoint = null);
}
Future<void> _showLocation(LatLng point, String name, String description) async {
await showModalBottomSheet( await showModalBottomSheet(
barrierColor: Colors.black.withOpacity(0.3), barrierColor: Colors.black.withOpacity(0.3),
context: context, context: context,
@ -87,17 +100,17 @@ class _MyHomePageState extends State<MyHomePage> {
Expanded(child: Column( Expanded(child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text(location['name'], style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)), Text(name, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
const SizedBox(height: 10), const SizedBox(height: 10),
Text(location['display_name']), Text(description),
], ],
)), )),
Column(children: [ Column(children: [
IconButton( IconButton(
icon: const Icon(Icons.star), icon: const Icon(Icons.star),
iconSize: 32, iconSize: 32,
color: _favorites.where((fav) => fav.lat == point.latitude && fav.lng == point.longitude).isEmpty ? Colors.grey : Colors.yellow, color: _favorites.where((fav) => fav.lat == point.latitude && fav.lng == point.longitude).isEmpty ? Colors.grey : Colors.yellow,
onPressed: () => _toggleFavorite(point, location['name'], location['display_name'], setModalState, context) onPressed: () => _toggleFavorite(point, name, description, setModalState, context)
), ),
const IconButton(icon: Icon(Icons.rate_review), iconSize: 32, onPressed: null), const IconButton(icon: Icon(Icons.rate_review), iconSize: 32, onPressed: null),
]), ]),
@ -107,8 +120,6 @@ class _MyHomePageState extends State<MyHomePage> {
}); });
}, },
); );
setState(() => _selectedPoint = null);
} }
void _toggleFavorite(LatLng point, String name, String description, StateSetter setModalState, BuildContext context) async { void _toggleFavorite(LatLng point, String name, String description, StateSetter setModalState, BuildContext context) async {
@ -130,7 +141,7 @@ class _MyHomePageState extends State<MyHomePage> {
return; return;
} }
_fetchFavorites(); await _fetchFavorites();
setModalState(() {}); setModalState(() {});
return; return;
@ -147,7 +158,7 @@ class _MyHomePageState extends State<MyHomePage> {
setModalState(() {}); setModalState(() {});
} }
void _fetchFavorites() async { Future<void> _fetchFavorites() async {
final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null); final response = await api.request(context, api.ApiService.app, 'GET', '/favorites', null);
if (response == null) return; if (response == null) return;
@ -179,7 +190,7 @@ class _MyHomePageState extends State<MyHomePage> {
options: MapOptions( options: MapOptions(
initialCenter: const LatLng(55.9397, 9.5156), initialCenter: const LatLng(55.9397, 9.5156),
initialZoom: 7.0, initialZoom: 7.0,
onTap: _showLocation, onTap: _onTap,
), ),
children: [ children: [
openStreetMapTileLayer, openStreetMapTileLayer,
@ -205,10 +216,18 @@ class _MyHomePageState extends State<MyHomePage> {
width: 30, width: 30,
height: 50, height: 50,
alignment: Alignment.center, alignment: Alignment.center,
child: const Stack( child: Stack(
children: [ children: [
Icon(Icons.location_pin, size: 30, color: Colors.yellow), IconButton(
Icon(Icons.location_on_outlined, size: 30, color: Colors.black), padding: const EdgeInsets.only(bottom: 10),
icon: const Icon(Icons.location_pin, size: 30, color: Colors.yellow),
onPressed: () => _showLocation(LatLng(favorite.lat, favorite.lng), favorite.name, favorite.description),
),
IconButton(
padding: const EdgeInsets.only(bottom: 10),
icon: const Icon(Icons.location_on_outlined, size: 30, color: Colors.black),
onPressed: () => _showLocation(LatLng(favorite.lat, favorite.lng), favorite.name, favorite.description),
),
] ]
), ),
) )